forked from TencentBlueKing/bk-cmdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.go
144 lines (117 loc) · 3.38 KB
/
input.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* Tencent is pleased to support the open source community by making 蓝鲸 available.
* Copyright (C) 2017-2018 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*/
package input
import (
"configcenter/src/framework/common"
"configcenter/src/framework/core/log"
"context"
"sync"
"time"
)
// manager implements the Manager interface
type manager struct {
ctx InputerContext
inputerLock sync.RWMutex
inputers MapInputer
inputerChan chan *wrapInputer
}
func (cli *manager) AddInputer(params InputerParams) InputerKey {
key := makeInputerKey()
target := &wrapInputer{
frequency: params.Frequency,
isTiming: params.IsTiming,
inputer: params.Target,
status: NormalStatus,
kind: params.Kind,
putter: params.Putter,
}
cli.inputerLock.Lock()
cli.inputers[key] = target
cli.inputerLock.Unlock()
select {
case cli.inputerChan <- target:
default:
log.Fatal("failed to puth the inputer")
}
return key
}
// RemoveInputer remove the Inputer by a InputerKey
func (cli *manager) RemoveInputer(key InputerKey) {
cli.inputerLock.Lock()
defer cli.inputerLock.Unlock()
deleteInputer(cli.inputers, key)
}
// Stop used to stop the business cycles.
func (cli *manager) Stop() error {
// stop the all Inputers
cli.inputerLock.Lock()
for _, inputer := range cli.inputers {
inputer.Stop()
}
cli.inputerLock.Unlock()
return nil
}
// Run start the business cycle until the stop method is called.
func (cli *manager) Run(ctx context.Context, inputerCtx InputerContext) {
// catch the framework context
cli.ctx = inputerCtx
// check the stat of the Inputer regularly, and start it if there is any new
for {
select {
case <-ctx.Done():
log.Info("will exit from inputer main business cycle")
goto end
case target := <-cli.inputerChan:
common.GoRun(func() {
cli.executeInputer(ctx, target)
}, func() {
target.SetStatus(ExceptionExitStatus)
})
case <-time.After(time.Second * 10):
cli.inputerLock.RLock()
// scan the all Inputers and restart the stoped Inputer
for _, inputer := range cli.inputers {
switch inputer.GetStatus() {
case NormalStatus:
common.GoRun(func() {
cli.executeInputer(ctx, inputer)
}, func() {
inputer.SetStatus(ExceptionExitStatus)
})
case WaitingToRunStatus:
common.GoRun(func() {
cli.executeInputer(ctx, inputer)
}, func() {
inputer.SetStatus(ExceptionExitStatus)
})
case RunningStatus:
// pass
case StoppingStatus:
// pass
case StoppedStatus:
// pass
case ExceptionExitStatus:
common.GoRun(func() {
cli.executeInputer(ctx, inputer)
}, func() {
inputer.SetStatus(ExceptionExitStatus)
})
default:
log.Fatalf("unknown the Inputer status (%d)", inputer.GetStatus())
}
}
cli.inputerLock.RUnlock()
}
}
end:
log.Info("finish the inputer main business cycle")
}