@@ -53,46 +53,62 @@ func (p *Plugin) GetResultChan() <-chan cpmtypes.Result {
53
53
}
54
54
55
55
func (p * Plugin ) Run () {
56
+ defer func () {
57
+ glog .Info ("Stopping plugin execution" )
58
+ p .tomb .Done ()
59
+ }()
60
+
56
61
runTicker := time .NewTicker (* p .config .PluginGlobalConfig .InvokeInterval )
62
+ defer runTicker .Stop ()
57
63
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" )
62
66
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 )
66
70
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
+ }()
72
76
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 ()
76
80
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 ))
78
82
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
+ }
84
88
85
- p .resultChan <- result
89
+ p .resultChan <- result
86
90
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
+ }
90
98
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 ()
93
110
case <- p .tomb .Stopping ():
94
- glog .Info ("Stopping plugin execution" )
95
- p .tomb .Done ()
111
+ return
96
112
}
97
113
}
98
114
}
0 commit comments