Skip to content

Commit 32c3d0a

Browse files
committed
Enable service explicitly requested to be restarted
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 1fdbcb6 commit 32c3d0a

File tree

5 files changed

+40
-8
lines changed

5 files changed

+40
-8
lines changed

cmd/compose/restart.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"context"
2121
"time"
2222

23-
"github.com/compose-spec/compose-go/types"
2423
"github.com/spf13/cobra"
2524

2625
"github.com/docker/compose/v2/pkg/api"
@@ -61,22 +60,23 @@ func runRestart(ctx context.Context, backend api.Service, opts restartOptions, s
6160
return err
6261
}
6362

63+
if project != nil && len(services) > 0 {
64+
err := project.EnableServices(services...)
65+
if err != nil {
66+
return err
67+
}
68+
}
69+
6470
var timeout *time.Duration
6571
if opts.timeChanged {
6672
timeoutValue := time.Duration(opts.timeout) * time.Second
6773
timeout = &timeoutValue
6874
}
6975

70-
if opts.noDeps {
71-
err := project.ForServices(services, types.IgnoreDependencies)
72-
if err != nil {
73-
return err
74-
}
75-
}
76-
7776
return backend.Restart(ctx, name, api.RestartOptions{
7877
Timeout: timeout,
7978
Services: services,
8079
Project: project,
80+
NoDeps: opts.noDeps,
8181
})
8282
}

pkg/api/api.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,8 @@ type RestartOptions struct {
213213
Timeout *time.Duration
214214
// Services passed in the command line to be restarted
215215
Services []string
216+
// NoDeps ignores services dependencies
217+
NoDeps bool
216218
}
217219

218220
// StopOptions group options of the Stop API

pkg/compose/restart.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ func (s *composeService) restart(ctx context.Context, projectName string, option
4848
}
4949
}
5050

51+
if options.NoDeps {
52+
err := project.ForServices(options.Services, types.IgnoreDependencies)
53+
if err != nil {
54+
return err
55+
}
56+
}
57+
5158
// ignore depends_on relations which are not impacted by restarting service or not required
5259
for i, service := range project.Services {
5360
for name, r := range service.DependsOn {

pkg/e2e/fixtures/restart-test/compose.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,10 @@ services:
33
image: alpine
44
init: true
55
command: ash -c "if [[ -f /tmp/restart.lock ]] ; then sleep infinity; else touch /tmp/restart.lock; fi"
6+
7+
test:
8+
profiles:
9+
- test
10+
image: alpine
11+
init: true
12+
command: ash -c "if [[ -f /tmp/restart.lock ]] ; then sleep infinity; else touch /tmp/restart.lock; fi"

pkg/e2e/restart_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,19 @@ func TestRestartWithDependencies(t *testing.T) {
8484
assert.Assert(t, strings.Contains(res.Combined(), fmt.Sprintf("Container e2e-restart-deps-%s-1 Started", depWithRestart)), res.Combined())
8585
assert.Assert(t, !strings.Contains(res.Combined(), depNoRestart), res.Combined())
8686
}
87+
88+
func TestRestartWithProfiles(t *testing.T) {
89+
c := NewParallelCLI(t, WithEnv(
90+
"COMPOSE_PROJECT_NAME=e2e-restart-profiles",
91+
))
92+
93+
t.Cleanup(func() {
94+
c.RunDockerComposeCmd(t, "down", "--remove-orphans")
95+
})
96+
97+
c.RunDockerComposeCmd(t, "-f", "./fixtures/restart-test/compose.yaml", "--profile", "test", "up", "-d")
98+
99+
res := c.RunDockerComposeCmd(t, "restart", "test")
100+
fmt.Println(res.Combined())
101+
assert.Assert(t, strings.Contains(res.Combined(), "Container e2e-restart-profiles-test-1 Started"), res.Combined())
102+
}

0 commit comments

Comments
 (0)