Start runs monitor to detect application events and return after termination nolint:gocyclo
(ctx context.Context)
| 55 | // |
| 56 | //nolint:gocyclo |
| 57 | func (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) |
nothing calls this directly
no test coverage detected