Skip to content

Commit

Permalink
contrib: Docker forward entrypoint signals.
Browse files Browse the repository at this point in the history
This modifies the entrypoint app used by the Docker image to forward
SIGTERM to the running instance so it shuts down gracefully when a
container is stopped.
  • Loading branch information
davecgh committed Jan 19, 2023
1 parent 04205dc commit 7cb3091
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions contrib/docker/entrypoint/entrypoint.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2021 The Decred developers
// Copyright (c) 2021-2023 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

Expand All @@ -8,7 +8,9 @@ import (
"fmt"
"os"
"os/exec"
"os/signal"
"path/filepath"
"syscall"
)

const (
Expand Down Expand Up @@ -101,11 +103,20 @@ func main() {
}

// Run the command with the given arguments while redirecting stdin, stdout,
// and stderr to the parent process.
// and stderr to the parent process. Also, listen for SIGTERM and forward
// it to ensure the child process has the opportunity to perform a graceful
// shutdown.
cmd := exec.Command(arg0, args...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
go func() {
interruptChannel := make(chan os.Signal, 1)
signal.Notify(interruptChannel, syscall.SIGTERM)
for sig := range interruptChannel {
cmd.Process.Signal(sig)
}
}()
if err := cmd.Run(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(cmd.ProcessState.ExitCode())
Expand Down

0 comments on commit 7cb3091

Please sign in to comment.