@@ -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 ),
@@ -84,16 +83,19 @@ func (c *monitor) Start(ctx context.Context) error {
8483 evtCh , errCh := c .api .Events (ctx , events.ListOptions {
8584 Filters : filters .NewArgs (
8685 filters .Arg ("type" , "container" ),
87- projectFilter (c .project . Name )),
86+ projectFilter (c .project )),
8887 })
8988 for {
89+ if len (containers ) == 0 {
90+ return nil
91+ }
9092 select {
9193 case <- ctx .Done ():
9294 return nil
9395 case err := <- errCh :
9496 return err
9597 case event := <- evtCh :
96- if ! c .services [event .Actor .Attributes [api .ServiceLabel ]] {
98+ if len ( c . services ) > 0 && ! c .services [event .Actor .Attributes [api .ServiceLabel ]] {
9799 continue
98100 }
99101 ctr , err := c .getContainerSummary (event )
@@ -103,24 +105,35 @@ func (c *monitor) Start(ctx context.Context) error {
103105
104106 switch event .Action {
105107 case events .ActionCreate :
106- containers .Add (ctr .ID )
108+ if len (c .services ) == 0 || c.services [ctr.Labels [api.ServiceLabel ]] {
109+ containers .Add (ctr .ID )
110+ }
111+ evtType := api .ContainerEventCreated
112+ if _ , ok := ctr .Labels [api .ContainerReplaceLabel ]; ok {
113+ evtType = api .ContainerEventRecreated
114+ }
107115 for _ , listener := range c .listeners {
108- listener (newContainerEvent (event .TimeNano , ctr , api . ContainerEventCreated ))
116+ listener (newContainerEvent (event .TimeNano , ctr , evtType ))
109117 }
110118 logrus .Debugf ("container %s created" , ctr .Name )
111119 case events .ActionStart :
112120 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- }
118121 if restarted {
119122 logrus .Debugf ("container %s restarted" , ctr .Name )
123+ for _ , listener := range c .listeners {
124+ listener (newContainerEvent (event .TimeNano , ctr , api .ContainerEventStarted , func (e * api.ContainerEvent ) {
125+ e .Restarting = restarted
126+ }))
127+ }
120128 } else {
121129 logrus .Debugf ("container %s started" , ctr .Name )
130+ for _ , listener := range c .listeners {
131+ listener (newContainerEvent (event .TimeNano , ctr , api .ContainerEventStarted ))
132+ }
133+ }
134+ if len (c .services ) == 0 || c.services [ctr.Labels [api.ServiceLabel ]] {
135+ containers .Add (ctr .ID )
122136 }
123- containers .Add (ctr .ID )
124137 case events .ActionRestart :
125138 for _ , listener := range c .listeners {
126139 listener (newContainerEvent (event .TimeNano , ctr , api .ContainerEventRestarted ))
@@ -159,9 +172,6 @@ func (c *monitor) Start(ctx context.Context) error {
159172 }
160173 }
161174 }
162- if len (containers ) == 0 {
163- return nil
164- }
165175 }
166176}
167177
@@ -192,7 +202,7 @@ func (c *monitor) getContainerSummary(event events.Message) (*api.ContainerSumma
192202 ctr := & api.ContainerSummary {
193203 ID : event .Actor .ID ,
194204 Name : event .Actor .Attributes ["name" ],
195- Project : c .project . Name ,
205+ Project : c .project ,
196206 Service : event .Actor .Attributes [api .ServiceLabel ],
197207 Labels : event .Actor .Attributes , // More than just labels, but that'c the closest the API gives us
198208 }
0 commit comments