Enable service explicitly requested to be restarted

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2023-08-31 09:43:39 +02:00 committed by Nicolas De loof
parent 1fdbcb6255
commit 32c3d0a3ff
5 changed files with 40 additions and 8 deletions

View File

@ -20,7 +20,6 @@ import (
"context"
"time"
"github.com/compose-spec/compose-go/types"
"github.com/spf13/cobra"
"github.com/docker/compose/v2/pkg/api"
@ -61,22 +60,23 @@ func runRestart(ctx context.Context, backend api.Service, opts restartOptions, s
return err
}
if project != nil && len(services) > 0 {
err := project.EnableServices(services...)
if err != nil {
return err
}
}
var timeout *time.Duration
if opts.timeChanged {
timeoutValue := time.Duration(opts.timeout) * time.Second
timeout = &timeoutValue
}
if opts.noDeps {
err := project.ForServices(services, types.IgnoreDependencies)
if err != nil {
return err
}
}
return backend.Restart(ctx, name, api.RestartOptions{
Timeout: timeout,
Services: services,
Project: project,
NoDeps: opts.noDeps,
})
}

View File

@ -213,6 +213,8 @@ type RestartOptions struct {
Timeout *time.Duration
// Services passed in the command line to be restarted
Services []string
// NoDeps ignores services dependencies
NoDeps bool
}
// StopOptions group options of the Stop API

View File

@ -48,6 +48,13 @@ func (s *composeService) restart(ctx context.Context, projectName string, option
}
}
if options.NoDeps {
err := project.ForServices(options.Services, types.IgnoreDependencies)
if err != nil {
return err
}
}
// ignore depends_on relations which are not impacted by restarting service or not required
for i, service := range project.Services {
for name, r := range service.DependsOn {

View File

@ -3,3 +3,10 @@ services:
image: alpine
init: true
command: ash -c "if [[ -f /tmp/restart.lock ]] ; then sleep infinity; else touch /tmp/restart.lock; fi"
test:
profiles:
- test
image: alpine
init: true
command: ash -c "if [[ -f /tmp/restart.lock ]] ; then sleep infinity; else touch /tmp/restart.lock; fi"

View File

@ -84,3 +84,19 @@ func TestRestartWithDependencies(t *testing.T) {
assert.Assert(t, strings.Contains(res.Combined(), fmt.Sprintf("Container e2e-restart-deps-%s-1 Started", depWithRestart)), res.Combined())
assert.Assert(t, !strings.Contains(res.Combined(), depNoRestart), res.Combined())
}
func TestRestartWithProfiles(t *testing.T) {
c := NewParallelCLI(t, WithEnv(
"COMPOSE_PROJECT_NAME=e2e-restart-profiles",
))
t.Cleanup(func() {
c.RunDockerComposeCmd(t, "down", "--remove-orphans")
})
c.RunDockerComposeCmd(t, "-f", "./fixtures/restart-test/compose.yaml", "--profile", "test", "up", "-d")
res := c.RunDockerComposeCmd(t, "restart", "test")
fmt.Println(res.Combined())
assert.Assert(t, strings.Contains(res.Combined(), "Container e2e-restart-profiles-test-1 Started"), res.Combined())
}