Skip to content

Commit a999207

Browse files
authored
Merge pull request #367 from grosser/grosser/unwrap
untangle plugin runner a bit
2 parents 2c14eb1 + 3be50a0 commit a999207

File tree

1 file changed

+39
-37
lines changed

1 file changed

+39
-37
lines changed

pkg/custompluginmonitor/plugin/plugin.go

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -61,58 +61,60 @@ func (p *Plugin) Run() {
6161
runTicker := time.NewTicker(*p.config.PluginGlobalConfig.InvokeInterval)
6262
defer runTicker.Stop()
6363

64-
runner := func() {
65-
glog.Info("Start to run custom plugins")
66-
67-
for _, rule := range p.config.Rules {
68-
p.syncChan <- struct{}{}
69-
p.Add(1)
70-
71-
go func(rule *cpmtypes.CustomRule) {
72-
defer p.Done()
73-
defer func() {
74-
<-p.syncChan
75-
}()
76-
77-
start := time.Now()
78-
exitStatus, message := p.run(*rule)
79-
end := time.Now()
80-
81-
glog.V(3).Infof("Rule: %+v. Start time: %v. End time: %v. Duration: %v", rule, start, end, end.Sub(start))
82-
83-
result := cpmtypes.Result{
84-
Rule: rule,
85-
ExitStatus: exitStatus,
86-
Message: message,
87-
}
88-
89-
p.resultChan <- result
90-
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-
}
98-
64+
// on boot run once
9965
select {
10066
case <-p.tomb.Stopping():
10167
return
10268
default:
103-
runner()
69+
p.runRules()
10470
}
10571

72+
// run every InvokeInterval
10673
for {
10774
select {
10875
case <-runTicker.C:
109-
runner()
76+
p.runRules()
11077
case <-p.tomb.Stopping():
11178
return
11279
}
11380
}
11481
}
11582

83+
// run each rule in parallel and wait for them to complete
84+
func (p *Plugin) runRules() {
85+
glog.Info("Start to run custom plugins")
86+
87+
for _, rule := range p.config.Rules {
88+
p.syncChan <- struct{}{}
89+
p.Add(1)
90+
go func(rule *cpmtypes.CustomRule) {
91+
defer p.Done()
92+
defer func() {
93+
<-p.syncChan
94+
}()
95+
96+
start := time.Now()
97+
exitStatus, message := p.run(*rule)
98+
end := time.Now()
99+
100+
glog.V(3).Infof("Rule: %+v. Start time: %v. End time: %v. Duration: %v", rule, start, end, end.Sub(start))
101+
102+
result := cpmtypes.Result{
103+
Rule: rule,
104+
ExitStatus: exitStatus,
105+
Message: message,
106+
}
107+
108+
p.resultChan <- result
109+
110+
glog.Infof("Add check result %+v for rule %+v", result, rule)
111+
}(rule)
112+
}
113+
114+
p.Wait()
115+
glog.Info("Finish running custom plugins")
116+
}
117+
116118
func (p *Plugin) run(rule cpmtypes.CustomRule) (exitStatus cpmtypes.Status, output string) {
117119
var ctx context.Context
118120
var cancel context.CancelFunc

0 commit comments

Comments
 (0)