@@ -11,62 +11,47 @@ import (
1111 "golang.org/x/net/context"
1212)
1313
14+ func (c * Client ) dispatchTaskEvents (ctx context.Context , stream protocol.UI_NotificationsClient , notifId uint64 , results <- chan interface {}, errors <- chan error ) {
15+ for {
16+ select {
17+ case <- ctx .Done ():
18+ goto Exit
19+ case err := <- errors :
20+ c .sendNotificationReply (stream , notifId , "" , err )
21+ case temp := <- results :
22+ data , ok := temp .(string )
23+ if ! ok {
24+ goto Exit
25+ }
26+ c .sendNotificationReply (stream , notifId , data , nil )
27+ }
28+ }
29+ Exit:
30+ // task should have already been removed via TASK_STOP
31+ }
32+
1433func (c * Client ) monitorSockets (config interface {}, stream protocol.UI_NotificationsClient , notification * protocol.Notification ) {
1534 sockMonTask , err := socketsmonitor .New (config , true )
1635 if err != nil {
1736 c .sendNotificationReply (stream , notification .Id , "" , err )
1837 return
1938 }
20- ctxSock , err := TaskMgr .AddTask (socketsmonitor .Name , sockMonTask )
39+ ctx , err := TaskMgr .AddTask (socketsmonitor .Name , sockMonTask )
2140 if err != nil {
2241 c .sendNotificationReply (stream , notification .Id , "" , err )
2342 return
2443 }
25- go func (ctx context.Context ) {
26- for {
27- select {
28- case <- ctx .Done ():
29- goto Exit
30- case err := <- sockMonTask .Errors ():
31- c .sendNotificationReply (stream , notification .Id , "" , err )
32- case temp := <- sockMonTask .Results ():
33- data , ok := temp .(string )
34- if ! ok {
35- goto Exit
36- }
37- c .sendNotificationReply (stream , notification .Id , data , nil )
38- }
39- }
40- Exit:
41- // task should have already been removed via TASK_STOP
42- }(ctxSock )
44+ go c .dispatchTaskEvents (ctx , stream , notification .Id , sockMonTask .Results (), sockMonTask .Errors ())
4345}
4446
4547func (c * Client ) monitorNode (node , interval string , stream protocol.UI_NotificationsClient , notification * protocol.Notification ) {
4648 taskName , nodeMonTask := nodemonitor .New (node , interval , true )
47- ctxNode , err := TaskMgr .AddTask (taskName , nodeMonTask )
49+ ctx , err := TaskMgr .AddTask (taskName , nodeMonTask )
4850 if err != nil {
4951 c .sendNotificationReply (stream , notification .Id , "" , err )
5052 return
5153 }
52- go func (ctx context.Context ) {
53- for {
54- select {
55- case <- ctx .Done ():
56- goto Exit
57- case err := <- nodeMonTask .Errors ():
58- c .sendNotificationReply (stream , notification .Id , "" , err )
59- case temp := <- nodeMonTask .Results ():
60- data , ok := temp .(string )
61- if ! ok {
62- goto Exit
63- }
64- c .sendNotificationReply (stream , notification .Id , data , nil )
65- }
66- }
67- Exit:
68- TaskMgr .RemoveTask (taskName )
69- }(ctxNode )
54+ go c .dispatchTaskEvents (ctx , stream , notification .Id , nodeMonTask .Results (), nodeMonTask .Errors ())
7055}
7156
7257func (c * Client ) monitorProcessDetails (pid int , interval string , stream protocol.UI_NotificationsClient , notification * protocol.Notification ) {
@@ -81,23 +66,5 @@ func (c *Client) monitorProcessDetails(pid int, interval string, stream protocol
8166 c .sendNotificationReply (stream , notification .Id , "" , err )
8267 return
8368 }
84- go func (ctx context.Context ) {
85- for {
86- select {
87- case <- ctx .Done ():
88- goto Exit
89- case err := <- pidMonTask .Errors ():
90- c .sendNotificationReply (stream , notification .Id , "" , err )
91- case temp := <- pidMonTask .Results ():
92- data , ok := temp .(string )
93- if ! ok {
94- goto Exit
95- }
96- c .sendNotificationReply (stream , notification .Id , data , nil )
97- }
98- }
99- Exit:
100- TaskMgr .RemoveTask (taskName )
101- }(ctx )
102-
69+ go c .dispatchTaskEvents (ctx , stream , notification .Id , pidMonTask .Results (), pidMonTask .Errors ())
10370}
0 commit comments