Skip to content

Commit

Permalink
fix: add sigterm handler to compose services
Browse files Browse the repository at this point in the history
This change adds a sigterm handler to compose services to efficiently
allow compose down to cleanup. This change also reworks compose kill
--signal test such that the services are stopped when sending SIGTERM to
be more aligned with the commands functionality.

Signed-off-by: Austin Vazquez <[email protected]>
  • Loading branch information
austinvazquez committed Dec 27, 2024
1 parent c08fc48 commit 1aa7813
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 10 deletions.
15 changes: 13 additions & 2 deletions tests/compose_down.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,30 @@ func createComposeYmlForDownCmd(serviceNames []string, containerNames []string)
gomega.Expect(serviceNames).Should(gomega.HaveLen(2))
gomega.Expect(containerNames).Should(gomega.HaveLen(2))

// Service commands should have SIGTERM handlers so graceful shutdown is quick.
composeYmlContent := fmt.Sprintf(
`
services:
%[1]s:
image: "%[3]s"
container_name: "%[4]s"
command: sleep infinity
command: |
sh -c "
trap 'echo shutting down; exit 0' SIGTERM
sleep infinity &
wait
"
volumes:
- compose_data_volume:/usr/local/data
%[2]s:
image: "%[3]s"
container_name: "%[5]s"
command: sleep infinity
command: |
sh -c "
trap 'echo shutting down; exit 0' SIGTERM
sleep infinity &
wait
"
volumes:
- compose_data_volume:/usr/local/data
volumes:
Expand Down
25 changes: 19 additions & 6 deletions tests/compose_kill.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,11 @@ func ComposeKill(o *option.Option) {
containerShouldNotBeRunning(o, containerNames...)
})

// With PID=1, `sleep infinity` will only exit when receiving SIGKILL. Default signal for kill is SIGKILL.
// https://stackoverflow.com/questions/45148381/why-cant-i-ctrl-c-a-sleep-infinity-in-docker-when-it-runs-as-pid-1
for _, signal := range []string{"-s", "--signal"} {
for _, term := range []string{"SIGTERM", "TERM"} {
ginkgo.It(fmt.Sprintf("should not kill running containers with %s %s", signal, term), func() {
ginkgo.It(fmt.Sprintf("should send %s to containers when using %s", term, signal), func() {
command.Run(o, "compose", "kill", signal, term, "--file", composeFilePath)
containerShouldBeRunning(o, containerNames...)
containerShouldNotBeRunning(o, containerNames...)
})
}
}
Expand All @@ -58,17 +56,32 @@ func createComposeYmlForKillCmd(serviceNames []string, containerNames []string)
gomega.Expect(serviceNames).Should(gomega.HaveLen(2))
gomega.Expect(containerNames).Should(gomega.HaveLen(2))

// Service commands implement SIGTERM handler to test compose kill
// can send non-default signals.
//
// With PID=1, `sleep infinity` would only exit when receiving SIGKILL.
// https://stackoverflow.com/questions/45148381/why-cant-i-ctrl-c-a-sleep-infinity-in-docker-when-it-runs-as-pid-1
composeYmlContent := fmt.Sprintf(
`
services:
%[1]s:
image: "%[3]s"
container_name: "%[4]s"
command: sleep infinity
command: |
sh -c "
trap 'echo shutting down; exit 0' SIGTERM
sleep infinity &
wait
"
%[2]s:
image: "%[3]s"
container_name: "%[5]s"
command: sleep infinity
command: |
sh -c "
trap 'echo shutting down; exit 0' SIGTERM
sleep infinity &
wait
"
`, serviceNames[0], serviceNames[1], localImages[defaultImage], containerNames[0], containerNames[1])
return ffs.CreateComposeYmlContext(composeYmlContent)
}
19 changes: 17 additions & 2 deletions tests/compose_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,32 @@ func createComposeYmlForLogsCmd(serviceNames []string, imageNames []string, cont
gomega.Expect(imageNames).Should(gomega.HaveLen(2))
gomega.Expect(containerNames).Should(gomega.HaveLen(2))

// Service commands should have SIGTERM handlers so graceful shutdown is quick.
composeYmlContent := fmt.Sprintf(
`
services:
%[1]s:
image: "%[3]s"
container_name: "%[5]s"
command: sh -c 'echo "hello from service 1"; echo "again hello"; sleep infinity'
command: |
sh -c "
trap 'echo shutting down; exit 0' SIGTERM
echo 'hello from service 1'
echo 'again hello'
sleep infinity &
wait
"
%[2]s:
image: "%[4]s"
container_name: "%[6]s"
command: sh -c 'echo "hello from service 2"; echo "again hello"; sleep infinity'
command: |
sh -c "
trap 'echo shutting down; exit 0' SIGTERM
echo 'hello from service 2'
echo 'again hello'
sleep infinity &
wait
"
`, serviceNames[0], serviceNames[1], imageNames[0], imageNames[1], containerNames[0], containerNames[1])
return ffs.CreateComposeYmlContext(composeYmlContent)
}

0 comments on commit 1aa7813

Please sign in to comment.