MCPcopy
hub / github.com/docker/compose / Start

Method Start

pkg/compose/monitor.go:57–165  ·  view source on GitHub ↗

Start runs monitor to detect application events and return after termination nolint:gocyclo

(ctx context.Context)

Source from the content-addressed store, hash-verified

55//
56//nolint:gocyclo
57func (c *monitor) Start(ctx context.Context) error {
58 // collect initial application container
59 initialState, err := c.apiClient.ContainerList(ctx, client.ContainerListOptions{
60 All: true,
61 Filters: projectFilter(c.project).Add("label",
62 oneOffFilter(false),
63 api.ConfigHashLabel,
64 ),
65 })
66 if err != nil {
67 return err
68 }
69
70 // containers is the set if container IDs the application is based on
71 containers := utils.Set[string]{}
72 for _, ctr := range initialState.Items {
73 if len(c.services) == 0 || c.services[ctr.Labels[api.ServiceLabel]] {
74 containers.Add(ctr.ID)
75 }
76 }
77 restarting := utils.Set[string]{}
78
79 res := c.apiClient.Events(ctx, client.EventsListOptions{
80 Filters: projectFilter(c.project).Add("type", "container"),
81 })
82 for {
83 if len(containers) == 0 {
84 return nil
85 }
86 select {
87 case <-ctx.Done():
88 return nil
89 case err := <-res.Err:
90 return err
91 case event := <-res.Messages:
92 if len(c.services) > 0 && !c.services[event.Actor.Attributes[api.ServiceLabel]] {
93 continue
94 }
95 ctr, err := c.getContainerSummary(event)
96 if err != nil {
97 return err
98 }
99
100 switch event.Action {
101 case events.ActionCreate:
102 if len(c.services) == 0 || c.services[ctr.Labels[api.ServiceLabel]] {
103 containers.Add(ctr.ID)
104 }
105 evtType := api.ContainerEventCreated
106 if _, ok := ctr.Labels[api.ContainerReplaceLabel]; ok {
107 evtType = api.ContainerEventRecreated
108 }
109 for _, listener := range c.listeners {
110 listener(newContainerEvent(event.TimeNano, ctr, evtType))
111 }
112 logrus.Debugf("container %s created", ctr.Name)
113 case events.ActionStart:
114 restarted := restarting.Has(ctr.ID)

Callers

nothing calls this directly

Calls 11

getContainerSummaryMethod · 0.95
projectFilterFunction · 0.85
oneOffFilterFunction · 0.85
newContainerEventFunction · 0.85
HasMethod · 0.80
EventsMethod · 0.65
DoneMethod · 0.65
RemoveMethod · 0.65
ContainerListMethod · 0.45
AddMethod · 0.45
ContainerInspectMethod · 0.45

Tested by

no test coverage detected