@@ -2,6 +2,7 @@ package service
22
33import (
44 "context"
5+ "fmt"
56 "log"
67 "os"
78 "strings"
@@ -203,18 +204,17 @@ func (l *SwarmListener) processServiceEventCreate(event Event) {
203204 ctx := l .ServiceCreateRemoveCancelManager .AddEvent (event )
204205 defer l .ServiceCreateRemoveCancelManager .RemoveEvent (event )
205206
206- doneChan := make (chan struct {} )
207+ errChan := make (chan error )
207208
208209 go func () {
209210 service , err := l .SSClient .SwarmServiceInspect (ctx , event .ID , l .IncludeNodeInfo )
210211 if err != nil {
211- if ! strings .Contains (err .Error (), "context canceled" ) {
212- l .Log .Printf ("ERROR: %v" , err )
213- }
212+ errChan <- err
214213 return
215214 }
216215 // Ignored service (filtered by `com.df.notify`)
217216 if service == nil {
217+ errChan <- nil
218218 return
219219 }
220220 ssm := MinifySwarmService (* service , l .IgnoreKey , l .IncludeKey )
@@ -223,6 +223,7 @@ func (l *SwarmListener) processServiceEventCreate(event Event) {
223223 // Store in cache
224224 isUpdated := l .SSCache .InsertAndCheck (ssm )
225225 if ! isUpdated {
226+ errChan <- nil
226227 return
227228 }
228229 metrics .RecordService (l .SSCache .Len ())
@@ -231,12 +232,17 @@ func (l *SwarmListener) processServiceEventCreate(event Event) {
231232 params := GetSwarmServiceMiniCreateParameters (ssm )
232233 paramsEncoded := ConvertMapStringStringToURLValues (params ).Encode ()
233234 l .placeOnNotificationChan (
234- l .SSNotificationChan , event .Type , event .TimeNano , ssm .ID , paramsEncoded , doneChan )
235+ l .SSNotificationChan , event .Type , event .TimeNano , ssm .ID , paramsEncoded , errChan )
235236 }()
236237
237238 for {
238239 select {
239- case <- doneChan :
240+ case err := <- errChan :
241+ if err != nil {
242+ if ! strings .Contains (err .Error (), "context canceled" ) {
243+ l .Log .Printf ("ERROR: %v" , err )
244+ }
245+ }
240246 return
241247 case <- ctx .Done ():
242248 return
@@ -248,26 +254,32 @@ func (l *SwarmListener) processServiceEventRemove(event Event) {
248254 ctx := l .ServiceCreateRemoveCancelManager .AddEvent (event )
249255 defer l .ServiceCreateRemoveCancelManager .RemoveEvent (event )
250256
251- doneChan := make (chan struct {} )
257+ errChan := make (chan error )
252258
253259 go func () {
254260
255261 ssm , ok := l .SSCache .Get (event .ID )
256262 if ! ok {
263+ errChan <- fmt .Errorf ("%s not in cache" , event .ID )
257264 return
258265 }
259- l .SSCache .Delete (ssm .ID )
260- metrics .RecordService (l .SSCache .Len ())
261-
262266 params := GetSwarmServiceMiniRemoveParameters (ssm )
263267 paramsEncoded := ConvertMapStringStringToURLValues (params ).Encode ()
264268 l .placeOnNotificationChan (
265- l .SSNotificationChan , event .Type , event .TimeNano , ssm .ID , paramsEncoded , doneChan )
269+ l .SSNotificationChan , event .Type , event .TimeNano , ssm .ID , paramsEncoded , errChan )
266270 }()
267271
268272 for {
269273 select {
270- case <- doneChan :
274+ case err := <- errChan :
275+ if err != nil {
276+ if ! strings .Contains (err .Error (), "not in cache" ) {
277+ l .Log .Printf ("ERROR: %v" , err )
278+ }
279+ return
280+ }
281+ l .SSCache .Delete (event .ID )
282+ metrics .RecordService (l .SSCache .Len ())
271283 return
272284 case <- ctx .Done ():
273285 return
@@ -299,15 +311,13 @@ func (l *SwarmListener) processNodeEventCreate(event Event) {
299311 ctx := l .NodeCreateRemoveCancelManager .AddEvent (event )
300312 defer l .NodeCreateRemoveCancelManager .RemoveEvent (event )
301313
302- doneChan := make (chan struct {} )
314+ errChan := make (chan error )
303315
304316 go func () {
305317
306318 node , err := l .NodeClient .NodeInspect (event .ID )
307319 if err != nil {
308- if ! strings .Contains (err .Error (), "context canceled" ) {
309- l .Log .Printf ("ERROR: %v" , err )
310- }
320+ errChan <- err
311321 return
312322 }
313323 nm := MinifyNode (node )
@@ -316,17 +326,24 @@ func (l *SwarmListener) processNodeEventCreate(event Event) {
316326 // Store in cache
317327 isUpdated := l .NodeCache .InsertAndCheck (nm )
318328 if ! isUpdated {
329+ errChan <- nil
319330 return
320331 }
321332 }
322333 params := GetNodeMiniCreateParameters (nm )
323334 paramsEncoded := ConvertMapStringStringToURLValues (params ).Encode ()
324- l .placeOnNotificationChan (l .NodeNotificationChan , event .Type , event .TimeNano , nm .ID , paramsEncoded , doneChan )
335+ l .placeOnNotificationChan (l .NodeNotificationChan , event .Type , event .TimeNano , nm .ID , paramsEncoded , errChan )
325336 }()
326337
327338 for {
328339 select {
329- case <- doneChan :
340+ case err := <- errChan :
341+ if err != nil {
342+ if ! strings .Contains (err .Error (), "context canceled" ) {
343+ l .Log .Printf ("ERROR: %v" , err )
344+ }
345+ return
346+ }
330347 l .NotifyServices (true )
331348 return
332349 case <- ctx .Done ():
@@ -339,22 +356,29 @@ func (l *SwarmListener) processNodeEventRemove(event Event) {
339356 ctx := l .NodeCreateRemoveCancelManager .AddEvent (event )
340357 defer l .NodeCreateRemoveCancelManager .RemoveEvent (event )
341358
342- doneChan := make (chan struct {} )
359+ errChan := make (chan error )
343360 go func () {
344361 nm , ok := l .NodeCache .Get (event .ID )
345362 if ! ok {
363+ errChan <- fmt .Errorf ("%s not in cache" , event .ID )
346364 return
347365 }
348- l .NodeCache .Delete (nm .ID )
349366
350367 params := GetNodeMiniRemoveParameters (nm )
351368 paramsEncoded := ConvertMapStringStringToURLValues (params ).Encode ()
352- l .placeOnNotificationChan (l .NodeNotificationChan , event .Type , event .TimeNano , nm .ID , paramsEncoded , doneChan )
369+ l .placeOnNotificationChan (l .NodeNotificationChan , event .Type , event .TimeNano , nm .ID , paramsEncoded , errChan )
353370 }()
354371
355372 for {
356373 select {
357- case <- doneChan :
374+ case err := <- errChan :
375+ if err != nil {
376+ if ! strings .Contains (err .Error (), "not in cache" ) {
377+ l .Log .Printf ("ERROR: %v" , err )
378+ }
379+ return
380+ }
381+ l .NodeCache .Delete (event .ID )
358382 l .NotifyServices (true )
359383 return
360384 case <- ctx .Done ():
@@ -395,13 +419,13 @@ func (l SwarmListener) NotifyNodes(useCache bool) {
395419 }()
396420}
397421
398- func (l SwarmListener ) placeOnNotificationChan (notiChan chan <- Notification , eventType EventType , timeNano int64 , ID string , parameters string , doneChan chan struct {} ) {
422+ func (l SwarmListener ) placeOnNotificationChan (notiChan chan <- Notification , eventType EventType , timeNano int64 , ID string , parameters string , errorChan chan error ) {
399423 notiChan <- Notification {
400424 EventType : eventType ,
401425 ID : ID ,
402426 Parameters : parameters ,
403427 TimeNano : timeNano ,
404- Done : doneChan ,
428+ ErrorChan : errorChan ,
405429 }
406430}
407431
0 commit comments