refactor(compose): collapse trivial event-helper wrappers in progress.go

Assisted-By: docker-agent
Signed-off-by: Nicolas De loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De loof 2026-04-27 11:23:39 +00:00 committed by Guillaume Lours
parent 616adea1b1
commit da530c7233
6 changed files with 23 additions and 73 deletions

View file

@ -193,7 +193,7 @@ func (c *convergence) ensureService(ctx context.Context, project *types.Project,
name := getContainerProgressName(ctr) name := getContainerProgressName(ctr)
switch ctr.State { switch ctr.State {
case container.StateRunning: case container.StateRunning:
c.compose.events.On(runningEvent(name)) c.compose.events.On(newEvent(name, api.Done, api.StatusRunning))
case container.StateCreated: case container.StateCreated:
case container.StateRestarting: case container.StateRestarting:
case container.StateExited: case container.StateExited:
@ -927,7 +927,7 @@ func (s *composeService) startService(ctx context.Context,
} }
eventName := getContainerProgressName(ctr) eventName := getContainerProgressName(ctr)
s.events.On(startingEvent(eventName)) s.events.On(newEvent(eventName, api.Working, api.StatusStarting))
_, err = s.apiClient().ContainerStart(ctx, ctr.ID, client.ContainerStartOptions{}) _, err = s.apiClient().ContainerStart(ctx, ctr.ID, client.ContainerStartOptions{})
if err != nil { if err != nil {
return err return err
@ -940,7 +940,7 @@ func (s *composeService) startService(ctx context.Context,
} }
} }
s.events.On(startedEvent(eventName)) s.events.On(newEvent(eventName, api.Done, api.StatusStarted))
} }
return nil return nil
} }

View file

@ -299,7 +299,7 @@ func (s *composeService) removeVolume(ctx context.Context, id string) error {
func (s *composeService) stopContainer(ctx context.Context, service *types.ServiceConfig, ctr containerType.Summary, timeout *time.Duration, listener api.ContainerEventListener) error { func (s *composeService) stopContainer(ctx context.Context, service *types.ServiceConfig, ctr containerType.Summary, timeout *time.Duration, listener api.ContainerEventListener) error {
eventName := getContainerProgressName(ctr) eventName := getContainerProgressName(ctr)
s.events.On(stoppingEvent(eventName)) s.events.On(newEvent(eventName, api.Working, api.StatusStopping))
if service != nil { if service != nil {
for _, hook := range service.PreStop { for _, hook := range service.PreStop {
@ -321,7 +321,7 @@ func (s *composeService) stopContainer(ctx context.Context, service *types.Servi
s.events.On(errorEvent(eventName, "Error while Stopping")) s.events.On(errorEvent(eventName, "Error while Stopping"))
return err return err
} }
s.events.On(stoppedEvent(eventName)) s.events.On(newEvent(eventName, api.Done, api.StatusStopped))
return nil return nil
} }

View file

@ -61,7 +61,7 @@ func (s *composeService) kill(ctx context.Context, projectName string, options a
containers.forEach(func(ctr container.Summary) { containers.forEach(func(ctr container.Summary) {
eg.Go(func() error { eg.Go(func() error {
eventName := getContainerProgressName(ctr) eventName := getContainerProgressName(ctr)
s.events.On(killingEvent(eventName)) s.events.On(newEvent(eventName, api.Working, api.StatusKilling))
_, err := s.apiClient().ContainerKill(ctx, ctr.ID, client.ContainerKillOptions{ _, err := s.apiClient().ContainerKill(ctx, ctr.ID, client.ContainerKillOptions{
Signal: options.Signal, Signal: options.Signal,
}) })
@ -69,7 +69,7 @@ func (s *composeService) kill(ctx context.Context, projectName string, options a
s.events.On(errorEvent(eventName, "Error while Killing")) s.events.On(errorEvent(eventName, "Error while Killing"))
return err return err
} }
s.events.On(killedEvent(eventName)) s.events.On(newEvent(eventName, api.Done, api.StatusKilled))
return nil return nil
}) })
}) })

View file

@ -52,66 +52,11 @@ func creatingEvent(id string) api.Resource {
return newEvent(id, api.Working, api.StatusCreating) return newEvent(id, api.Working, api.StatusCreating)
} }
// startingEvent creates a new Starting in progress Resource
func startingEvent(id string) api.Resource {
return newEvent(id, api.Working, api.StatusStarting)
}
// startedEvent creates a new Started in progress Resource
func startedEvent(id string) api.Resource {
return newEvent(id, api.Done, api.StatusStarted)
}
// waiting creates a new waiting event
func waiting(id string) api.Resource {
return newEvent(id, api.Working, api.StatusWaiting)
}
// healthy creates a new healthy event
func healthy(id string) api.Resource {
return newEvent(id, api.Done, api.StatusHealthy)
}
// exited creates a new exited event
func exited(id string) api.Resource {
return newEvent(id, api.Done, api.StatusExited)
}
// restartingEvent creates a new Restarting in progress Resource
func restartingEvent(id string) api.Resource {
return newEvent(id, api.Working, api.StatusRestarting)
}
// runningEvent creates a new Running in progress Resource
func runningEvent(id string) api.Resource {
return newEvent(id, api.Done, api.StatusRunning)
}
// createdEvent creates a new Created (done) Resource // createdEvent creates a new Created (done) Resource
func createdEvent(id string) api.Resource { func createdEvent(id string) api.Resource {
return newEvent(id, api.Done, api.StatusCreated) return newEvent(id, api.Done, api.StatusCreated)
} }
// stoppingEvent creates a new Stopping in progress Resource
func stoppingEvent(id string) api.Resource {
return newEvent(id, api.Working, api.StatusStopping)
}
// stoppedEvent creates a new Stopping in progress Resource
func stoppedEvent(id string) api.Resource {
return newEvent(id, api.Done, api.StatusStopped)
}
// killingEvent creates a new Killing in progress Resource
func killingEvent(id string) api.Resource {
return newEvent(id, api.Working, api.StatusKilling)
}
// killedEvent creates a new Killed in progress Resource
func killedEvent(id string) api.Resource {
return newEvent(id, api.Done, api.StatusKilled)
}
// removingEvent creates a new Removing in progress Resource // removingEvent creates a new Removing in progress Resource
func removingEvent(id string) api.Resource { func removingEvent(id string) api.Resource {
return newEvent(id, api.Working, api.StatusRemoving) return newEvent(id, api.Working, api.StatusRemoving)
@ -132,17 +77,22 @@ func builtEvent(id string) api.Resource {
return newEvent("Image "+id, api.Done, api.StatusBuilt) return newEvent("Image "+id, api.Done, api.StatusBuilt)
} }
// pullingEvent creates a new pulling (in progress) Resource // waiting creates a new waiting event; kept as a named func for use as a function value.
func pullingEvent(id string) api.Resource { func waiting(id string) api.Resource {
return newEvent("Image "+id, api.Working, api.StatusPulling) return newEvent(id, api.Working, api.StatusWaiting)
} }
// pulledEvent creates a new pulled (done) Resource // healthy creates a new healthy event; kept as a named func for use as a function value.
func pulledEvent(id string) api.Resource { func healthy(id string) api.Resource {
return newEvent("Image "+id, api.Done, api.StatusPulled) return newEvent(id, api.Done, api.StatusHealthy)
} }
// skippedEvent creates a new Skipped Resource // exited creates a new exited event; kept as a named func for use as a function value.
func exited(id string) api.Resource {
return newEvent(id, api.Done, api.StatusExited)
}
// skippedEvent creates a new Skipped Resource; kept as a named func for use as a function value.
func skippedEvent(id string, reason string) api.Resource { func skippedEvent(id string, reason string) api.Resource {
return api.Resource{ return api.Resource{
ID: id, ID: id,

View file

@ -175,7 +175,7 @@ func getUnwrappedErrorMessage(err error) string {
func (s *composeService) pullServiceImage(ctx context.Context, service types.ServiceConfig, quietPull bool, defaultPlatform string) (string, error) { func (s *composeService) pullServiceImage(ctx context.Context, service types.ServiceConfig, quietPull bool, defaultPlatform string) (string, error) {
resource := "Image " + service.Image resource := "Image " + service.Image
s.events.On(pullingEvent(service.Image)) s.events.On(newEvent("Image "+service.Image, api.Working, api.StatusPulling))
ref, err := reference.ParseNormalizedNamed(service.Image) ref, err := reference.ParseNormalizedNamed(service.Image)
if err != nil { if err != nil {
return "", err return "", err
@ -246,7 +246,7 @@ func (s *composeService) pullServiceImage(ctx context.Context, service types.Ser
toPullProgressEvent(resource, jm, s.events) toPullProgressEvent(resource, jm, s.events)
} }
} }
s.events.On(pulledEvent(service.Image)) s.events.On(newEvent("Image "+service.Image, api.Done, api.StatusPulled))
inspected, err := s.apiClient().ImageInspect(ctx, service.Image) inspected, err := s.apiClient().ImageInspect(ctx, service.Image)
if err != nil { if err != nil {

View file

@ -93,14 +93,14 @@ func (s *composeService) restart(ctx context.Context, projectName string, option
} }
} }
eventName := getContainerProgressName(ctr) eventName := getContainerProgressName(ctr)
s.events.On(restartingEvent(eventName)) s.events.On(newEvent(eventName, api.Working, api.StatusRestarting))
_, err = s.apiClient().ContainerRestart(ctx, ctr.ID, client.ContainerRestartOptions{ _, err = s.apiClient().ContainerRestart(ctx, ctr.ID, client.ContainerRestartOptions{
Timeout: utils.DurationSecondToInt(options.Timeout), Timeout: utils.DurationSecondToInt(options.Timeout),
}) })
if err != nil { if err != nil {
return err return err
} }
s.events.On(startedEvent(eventName)) s.events.On(newEvent(eventName, api.Done, api.StatusStarted))
for _, hook := range def.PostStart { for _, hook := range def.PostStart {
err = s.runHook(ctx, ctr, def, hook, nil) err = s.runHook(ctx, ctr, def, hook, nil)
if err != nil { if err != nil {