mirror of
https://github.com/docker/compose.git
synced 2025-07-19 19:54:28 +02:00
Enable service explicitly requested to be restarted
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
parent
1fdbcb6255
commit
32c3d0a3ff
@ -20,7 +20,6 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/compose-spec/compose-go/types"
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
"github.com/docker/compose/v2/pkg/api"
|
"github.com/docker/compose/v2/pkg/api"
|
||||||
@ -61,22 +60,23 @@ func runRestart(ctx context.Context, backend api.Service, opts restartOptions, s
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if project != nil && len(services) > 0 {
|
||||||
|
err := project.EnableServices(services...)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var timeout *time.Duration
|
var timeout *time.Duration
|
||||||
if opts.timeChanged {
|
if opts.timeChanged {
|
||||||
timeoutValue := time.Duration(opts.timeout) * time.Second
|
timeoutValue := time.Duration(opts.timeout) * time.Second
|
||||||
timeout = &timeoutValue
|
timeout = &timeoutValue
|
||||||
}
|
}
|
||||||
|
|
||||||
if opts.noDeps {
|
|
||||||
err := project.ForServices(services, types.IgnoreDependencies)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return backend.Restart(ctx, name, api.RestartOptions{
|
return backend.Restart(ctx, name, api.RestartOptions{
|
||||||
Timeout: timeout,
|
Timeout: timeout,
|
||||||
Services: services,
|
Services: services,
|
||||||
Project: project,
|
Project: project,
|
||||||
|
NoDeps: opts.noDeps,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -213,6 +213,8 @@ type RestartOptions struct {
|
|||||||
Timeout *time.Duration
|
Timeout *time.Duration
|
||||||
// Services passed in the command line to be restarted
|
// Services passed in the command line to be restarted
|
||||||
Services []string
|
Services []string
|
||||||
|
// NoDeps ignores services dependencies
|
||||||
|
NoDeps bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// StopOptions group options of the Stop API
|
// StopOptions group options of the Stop API
|
||||||
|
@ -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
|
// ignore depends_on relations which are not impacted by restarting service or not required
|
||||||
for i, service := range project.Services {
|
for i, service := range project.Services {
|
||||||
for name, r := range service.DependsOn {
|
for name, r := range service.DependsOn {
|
||||||
|
@ -3,3 +3,10 @@ services:
|
|||||||
image: alpine
|
image: alpine
|
||||||
init: true
|
init: true
|
||||||
command: ash -c "if [[ -f /tmp/restart.lock ]] ; then sleep infinity; else touch /tmp/restart.lock; fi"
|
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"
|
||||||
|
@ -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(), fmt.Sprintf("Container e2e-restart-deps-%s-1 Started", depWithRestart)), res.Combined())
|
||||||
assert.Assert(t, !strings.Contains(res.Combined(), depNoRestart), 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())
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user