@@ -61,58 +61,60 @@ func (p *Plugin) Run() {
61
61
runTicker := time .NewTicker (* p .config .PluginGlobalConfig .InvokeInterval )
62
62
defer runTicker .Stop ()
63
63
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
99
65
select {
100
66
case <- p .tomb .Stopping ():
101
67
return
102
68
default :
103
- runner ()
69
+ p . runRules ()
104
70
}
105
71
72
+ // run every InvokeInterval
106
73
for {
107
74
select {
108
75
case <- runTicker .C :
109
- runner ()
76
+ p . runRules ()
110
77
case <- p .tomb .Stopping ():
111
78
return
112
79
}
113
80
}
114
81
}
115
82
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
+
116
118
func (p * Plugin ) run (rule cpmtypes.CustomRule ) (exitStatus cpmtypes.Status , output string ) {
117
119
var ctx context.Context
118
120
var cancel context.CancelFunc
0 commit comments