Skip to content

Commit e2afd71

Browse files
authored
Merge pull request #288 from yguo0905/master
Run custom plugins immediately on startup
2 parents 520b650 + 468a23d commit e2afd71

File tree

1 file changed

+45
-29
lines changed

1 file changed

+45
-29
lines changed

pkg/custompluginmonitor/plugin/plugin.go

Lines changed: 45 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -53,46 +53,62 @@ func (p *Plugin) GetResultChan() <-chan cpmtypes.Result {
5353
}
5454

5555
func (p *Plugin) Run() {
56+
defer func() {
57+
glog.Info("Stopping plugin execution")
58+
p.tomb.Done()
59+
}()
60+
5661
runTicker := time.NewTicker(*p.config.PluginGlobalConfig.InvokeInterval)
62+
defer runTicker.Stop()
5763

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

63-
for _, rule := range p.config.Rules {
64-
p.syncChan <- struct{}{}
65-
p.Add(1)
67+
for _, rule := range p.config.Rules {
68+
p.syncChan <- struct{}{}
69+
p.Add(1)
6670

67-
go func(rule *cpmtypes.CustomRule) {
68-
defer p.Done()
69-
defer func() {
70-
<-p.syncChan
71-
}()
71+
go func(rule *cpmtypes.CustomRule) {
72+
defer p.Done()
73+
defer func() {
74+
<-p.syncChan
75+
}()
7276

73-
start := time.Now()
74-
exitStatus, message := p.run(*rule)
75-
end := time.Now()
77+
start := time.Now()
78+
exitStatus, message := p.run(*rule)
79+
end := time.Now()
7680

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

79-
result := cpmtypes.Result{
80-
Rule: rule,
81-
ExitStatus: exitStatus,
82-
Message: message,
83-
}
83+
result := cpmtypes.Result{
84+
Rule: rule,
85+
ExitStatus: exitStatus,
86+
Message: message,
87+
}
8488

85-
p.resultChan <- result
89+
p.resultChan <- result
8690

87-
glog.Infof("Add check result %+v for rule %+v", result, rule)
88-
}(rule)
89-
}
91+
glog.Infof("Add check result %+v for rule %+v", result, rule)
92+
}(rule)
93+
}
94+
95+
p.Wait()
96+
glog.Info("Finish running custom plugins")
97+
}
9098

91-
p.Wait()
92-
glog.Info("Finish running custom plugins")
99+
select {
100+
case <-p.tomb.Stopping():
101+
return
102+
default:
103+
runner()
104+
}
105+
106+
for {
107+
select {
108+
case <-runTicker.C:
109+
runner()
93110
case <-p.tomb.Stopping():
94-
glog.Info("Stopping plugin execution")
95-
p.tomb.Done()
111+
return
96112
}
97113
}
98114
}

0 commit comments

Comments
 (0)