untangle plugin runner a bit

add some docs and make it clearer what is actually going on
(parallel rule execution on start and then on timer)
This commit is contained in:
Michael Grosser
2019-10-10 15:46:04 -07:00
parent 219b408222
commit 3be50a088a
+21 -19
View File
@@ -61,13 +61,32 @@ func (p *Plugin) Run() {
runTicker := time.NewTicker(*p.config.PluginGlobalConfig.InvokeInterval) runTicker := time.NewTicker(*p.config.PluginGlobalConfig.InvokeInterval)
defer runTicker.Stop() defer runTicker.Stop()
runner := func() { // on boot run once
select {
case <-p.tomb.Stopping():
return
default:
p.runRules()
}
// run every InvokeInterval
for {
select {
case <-runTicker.C:
p.runRules()
case <-p.tomb.Stopping():
return
}
}
}
// run each rule in parallel and wait for them to complete
func (p *Plugin) runRules() {
glog.Info("Start to run custom plugins") glog.Info("Start to run custom plugins")
for _, rule := range p.config.Rules { for _, rule := range p.config.Rules {
p.syncChan <- struct{}{} p.syncChan <- struct{}{}
p.Add(1) p.Add(1)
go func(rule *cpmtypes.CustomRule) { go func(rule *cpmtypes.CustomRule) {
defer p.Done() defer p.Done()
defer func() { defer func() {
@@ -96,23 +115,6 @@ func (p *Plugin) Run() {
glog.Info("Finish running custom plugins") glog.Info("Finish running custom plugins")
} }
select {
case <-p.tomb.Stopping():
return
default:
runner()
}
for {
select {
case <-runTicker.C:
runner()
case <-p.tomb.Stopping():
return
}
}
}
func (p *Plugin) run(rule cpmtypes.CustomRule) (exitStatus cpmtypes.Status, output string) { func (p *Plugin) run(rule cpmtypes.CustomRule) (exitStatus cpmtypes.Status, output string) {
var ctx context.Context var ctx context.Context
var cancel context.CancelFunc var cancel context.CancelFunc