Skip to content

Commit 2bce2dc

Browse files
committed
Run custom plugins immediately on startup
1 parent 3688897 commit 2bce2dc

File tree

1 file changed

+39
-27
lines changed

1 file changed

+39
-27
lines changed

pkg/custompluginmonitor/plugin/plugin.go

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -55,41 +55,53 @@ func (p *Plugin) GetResultChan() <-chan cpmtypes.Result {
5555
func (p *Plugin) Run() {
5656
runTicker := time.NewTicker(*p.config.PluginGlobalConfig.InvokeInterval)
5757

58-
for {
59-
select {
60-
case <-runTicker.C:
61-
glog.Info("Start to run custom plugins")
58+
runner := func() {
59+
glog.Info("Start to run custom plugins")
6260

63-
for _, rule := range p.config.Rules {
64-
p.syncChan <- struct{}{}
65-
p.Add(1)
61+
for _, rule := range p.config.Rules {
62+
p.syncChan <- struct{}{}
63+
p.Add(1)
6664

67-
go func(rule *cpmtypes.CustomRule) {
68-
defer p.Done()
69-
defer func() {
70-
<-p.syncChan
71-
}()
65+
go func(rule *cpmtypes.CustomRule) {
66+
defer p.Done()
67+
defer func() {
68+
<-p.syncChan
69+
}()
7270

73-
start := time.Now()
74-
exitStatus, message := p.run(*rule)
75-
end := time.Now()
71+
start := time.Now()
72+
exitStatus, message := p.run(*rule)
73+
end := time.Now()
7674

77-
glog.V(3).Infof("Rule: %+v. Start time: %v. End time: %v. Duration: %v", rule, start, end, end.Sub(start))
75+
glog.V(3).Infof("Rule: %+v. Start time: %v. End time: %v. Duration: %v", rule, start, end, end.Sub(start))
7876

79-
result := cpmtypes.Result{
80-
Rule: rule,
81-
ExitStatus: exitStatus,
82-
Message: message,
83-
}
77+
result := cpmtypes.Result{
78+
Rule: rule,
79+
ExitStatus: exitStatus,
80+
Message: message,
81+
}
82+
83+
p.resultChan <- result
84+
85+
glog.Infof("Add check result %+v for rule %+v", result, rule)
86+
}(rule)
87+
}
8488

85-
p.resultChan <- result
89+
p.Wait()
90+
glog.Info("Finish running custom plugins")
91+
}
8692

87-
glog.Infof("Add check result %+v for rule %+v", result, rule)
88-
}(rule)
89-
}
93+
select {
94+
case <-p.tomb.Stopping():
95+
glog.Info("Stopping plugin execution")
96+
p.tomb.Done()
97+
default:
98+
runner()
99+
}
90100

91-
p.Wait()
92-
glog.Info("Finish running custom plugins")
101+
for {
102+
select {
103+
case <-runTicker.C:
104+
runner()
93105
case <-p.tomb.Stopping():
94106
glog.Info("Stopping plugin execution")
95107
p.tomb.Done()

0 commit comments

Comments
 (0)