@@ -176,23 +176,30 @@ func (s *ContainerStore) FindContainer(id string, labels ContainerLabels) (Conta
176176 defer cancel ()
177177 if newContainer , err := s .client .FindContainer (ctx , id ); err == nil {
178178 return & newContainer , false
179+ } else {
180+ log .Error ().Err (err ).Msg ("failed to fetch container" )
181+ return c , false
179182 }
180183 }
181184 return c , false
182185 }); ok {
183- event := ContainerEvent {
184- Name : "update" ,
185- Host : s .client .Host ().ID ,
186- ActorID : id ,
187- }
188- s .subscribers .Range (func (c context.Context , events chan <- ContainerEvent ) bool {
189- select {
190- case events <- event :
191- case <- c .Done ():
192- s .subscribers .Delete (c )
186+ go func () {
187+ event := ContainerEvent {
188+ Name : "update" ,
189+ Host : newContainer .Host ,
190+ ActorID : id ,
191+ Container : newContainer ,
193192 }
194- return true
195- })
193+
194+ s .subscribers .Range (func (c context.Context , events chan <- ContainerEvent ) bool {
195+ select {
196+ case events <- event :
197+ case <- c .Done ():
198+ s .subscribers .Delete (c )
199+ }
200+ return true
201+ })
202+ }()
196203 return * newContainer , nil
197204 }
198205 }
@@ -248,7 +255,7 @@ func (s *ContainerStore) init() {
248255 for {
249256 select {
250257 case event := <- s .events :
251- log .Trace ().Str ("event" , event .Name ).Str ("id" , event .ActorID ).Msg ("received container event" )
258+ log .Debug ().Str ("event" , event .Name ).Str ("id" , event .ActorID ).Msg ("received container event" )
252259 switch event .Name {
253260 case "create" :
254261 ctx , cancel := context .WithTimeout (context .Background (), 3 * time .Second )
@@ -262,7 +269,6 @@ func (s *ContainerStore) init() {
262269 })
263270
264271 if valid {
265- log .Debug ().Str ("id" , container .ID ).Msg ("container started" )
266272 s .containers .Store (container .ID , & container )
267273 s .newContainerSubscribers .Range (func (c context.Context , containers chan <- Container ) bool {
268274 select {
@@ -287,7 +293,6 @@ func (s *ContainerStore) init() {
287293 })
288294
289295 if valid {
290- log .Debug ().Str ("id" , container .ID ).Msg ("container started" )
291296 s .containers .Store (container .ID , & container )
292297 s .newContainerSubscribers .Range (func (c context.Context , containers chan <- Container ) bool {
293298 select {
@@ -304,42 +309,41 @@ func (s *ContainerStore) init() {
304309 s .containers .Delete (event .ActorID )
305310
306311 case "update" :
307- s .containers .Compute (event .ActorID , func (c * Container , loaded bool ) (* Container , bool ) {
312+ started := false
313+ updatedContainer , _ := s .containers .Compute (event .ActorID , func (c * Container , loaded bool ) (* Container , bool ) {
308314 if loaded {
309- log .Debug ().Str ("id" , c .ID ).Msg ("container updated" )
310- started := false
311- if newContainer , err := s .client .FindContainer (context .Background (), c .ID ); err == nil {
312- if newContainer .State == "running" && c .State != "running" {
313- started = true
314- }
315- c .Name = newContainer .Name
316- c .State = newContainer .State
317- c .Labels = newContainer .Labels
318- c .StartedAt = newContainer .StartedAt
319- c .FinishedAt = newContainer .FinishedAt
320- c .Created = newContainer .Created
321- } else {
322- log .Error ().Err (err ).Str ("id" , c .ID ).Msg ("failed to update container" )
323- }
324- if started {
325- s .subscribers .Range (func (ctx context.Context , events chan <- ContainerEvent ) bool {
326- select {
327- case events <- ContainerEvent {
328- Name : "start" ,
329- ActorID : c .ID ,
330- }:
331- case <- ctx .Done ():
332- s .subscribers .Delete (ctx )
333- }
334- return true
335- })
315+ newContainer := event .Container
316+ if newContainer .State == "running" && c .State != "running" {
317+ started = true
336318 }
319+ c .Name = newContainer .Name
320+ c .State = newContainer .State
321+ c .Labels = newContainer .Labels
322+ c .StartedAt = newContainer .StartedAt
323+ c .FinishedAt = newContainer .FinishedAt
324+ c .Created = newContainer .Created
325+ c .Host = newContainer .Host
337326 return c , false
338327 } else {
339328 return c , true
340329 }
341330 })
342331
332+ if started {
333+ s .subscribers .Range (func (ctx context.Context , events chan <- ContainerEvent ) bool {
334+ select {
335+ case events <- ContainerEvent {
336+ Name : "start" ,
337+ ActorID : updatedContainer .ID ,
338+ Host : updatedContainer .Host ,
339+ }:
340+ case <- ctx .Done ():
341+ s .subscribers .Delete (ctx )
342+ }
343+ return true
344+ })
345+ }
346+
343347 case "die" :
344348 s .containers .Compute (event .ActorID , func (c * Container , loaded bool ) (* Container , bool ) {
345349 if loaded {
0 commit comments