@@ -20,7 +20,6 @@ import (
2020 "context"
2121 "strconv"
2222
23- "github.com/compose-spec/compose-go/v2/types"
2423 "github.com/containerd/errdefs"
2524 "github.com/docker/docker/api/types/container"
2625 "github.com/docker/docker/api/types/events"
@@ -34,23 +33,23 @@ import (
3433
3534type monitor struct {
3635 api client.APIClient
37- project * types. Project
36+ project string
3837 // services tells us which service to consider and those we can ignore, maybe ran by a concurrent compose command
3938 services map [string ]bool
4039 listeners []api.ContainerEventListener
4140}
4241
43- func newMonitor (api client.APIClient , project * types.Project ) * monitor {
44- services := map [string ]bool {}
45- if project != nil {
46- for name := range project .Services {
47- services [name ] = true
48- }
49- }
42+ func newMonitor (api client.APIClient , project string ) * monitor {
5043 return & monitor {
5144 api : api ,
5245 project : project ,
53- services : services ,
46+ services : map [string ]bool {},
47+ }
48+ }
49+
50+ func (c * monitor ) withServices (services []string ) {
51+ for _ , name := range services {
52+ c .services [name ] = true
5453 }
5554}
5655
@@ -62,7 +61,7 @@ func (c *monitor) Start(ctx context.Context) error {
6261 initialState , err := c .api .ContainerList (ctx , container.ListOptions {
6362 All : true ,
6463 Filters : filters .NewArgs (
65- projectFilter (c .project . Name ),
64+ projectFilter (c .project ),
6665 oneOffFilter (false ),
6766 hasConfigHashLabel (),
6867 ),
@@ -78,22 +77,24 @@ func (c *monitor) Start(ctx context.Context) error {
7877 containers .Add (ctr .ID )
7978 }
8079 }
81-
8280 restarting := utils.Set [string ]{}
8381
8482 evtCh , errCh := c .api .Events (ctx , events.ListOptions {
8583 Filters : filters .NewArgs (
8684 filters .Arg ("type" , "container" ),
87- projectFilter (c .project . Name )),
85+ projectFilter (c .project )),
8886 })
8987 for {
88+ if len (containers ) == 0 {
89+ return nil
90+ }
9091 select {
9192 case <- ctx .Done ():
9293 return nil
9394 case err := <- errCh :
9495 return err
9596 case event := <- evtCh :
96- if ! c .services [event .Actor .Attributes [api .ServiceLabel ]] {
97+ if len ( c . services ) > 0 && ! c .services [event .Actor .Attributes [api .ServiceLabel ]] {
9798 continue
9899 }
99100 ctr , err := c .getContainerSummary (event )
@@ -103,24 +104,35 @@ func (c *monitor) Start(ctx context.Context) error {
103104
104105 switch event .Action {
105106 case events .ActionCreate :
106- containers .Add (ctr .ID )
107+ if len (c .services ) == 0 || c.services [ctr.Labels [api.ServiceLabel ]] {
108+ containers .Add (ctr .ID )
109+ }
110+ evtType := api .ContainerEventCreated
111+ if _ , ok := ctr .Labels [api .ContainerReplaceLabel ]; ok {
112+ evtType = api .ContainerEventRecreated
113+ }
107114 for _ , listener := range c .listeners {
108- listener (newContainerEvent (event .TimeNano , ctr , api . ContainerEventCreated ))
115+ listener (newContainerEvent (event .TimeNano , ctr , evtType ))
109116 }
110117 logrus .Debugf ("container %s created" , ctr .Name )
111118 case events .ActionStart :
112119 restarted := restarting .Has (ctr .ID )
113- for _ , listener := range c .listeners {
114- listener (newContainerEvent (event .TimeNano , ctr , api .ContainerEventStarted , func (e * api.ContainerEvent ) {
115- e .Restarting = restarted
116- }))
117- }
118120 if restarted {
119121 logrus .Debugf ("container %s restarted" , ctr .Name )
122+ for _ , listener := range c .listeners {
123+ listener (newContainerEvent (event .TimeNano , ctr , api .ContainerEventStarted , func (e * api.ContainerEvent ) {
124+ e .Restarting = restarted
125+ }))
126+ }
120127 } else {
121128 logrus .Debugf ("container %s started" , ctr .Name )
129+ for _ , listener := range c .listeners {
130+ listener (newContainerEvent (event .TimeNano , ctr , api .ContainerEventStarted ))
131+ }
132+ }
133+ if len (c .services ) == 0 || c.services [ctr.Labels [api.ServiceLabel ]] {
134+ containers .Add (ctr .ID )
122135 }
123- containers .Add (ctr .ID )
124136 case events .ActionRestart :
125137 for _ , listener := range c .listeners {
126138 listener (newContainerEvent (event .TimeNano , ctr , api .ContainerEventRestarted ))
@@ -159,9 +171,6 @@ func (c *monitor) Start(ctx context.Context) error {
159171 }
160172 }
161173 }
162- if len (containers ) == 0 {
163- return nil
164- }
165174 }
166175}
167176
@@ -192,7 +201,7 @@ func (c *monitor) getContainerSummary(event events.Message) (*api.ContainerSumma
192201 ctr := & api.ContainerSummary {
193202 ID : event .Actor .ID ,
194203 Name : event .Actor .Attributes ["name" ],
195- Project : c .project . Name ,
204+ Project : c .project ,
196205 Service : event .Actor .Attributes [api .ServiceLabel ],
197206 Labels : event .Actor .Attributes , // More than just labels, but that'c the closest the API gives us
198207 }
0 commit comments