From dd6a1483bfd9ab61b6bc5331d23ec1e849f69a58 Mon Sep 17 00:00:00 2001 From: Erwin van Eyk Date: Sat, 9 Jun 2018 12:13:24 +0200 Subject: [PATCH] Handle system termination signals (#152) Currently termination can only be forced (as the context that is used to determine cancellation is never actually cancelled). This PR closes the context on termination signals from the OS. This should reduce the runtime of tests especially (as a significant amount of time is spent on waiting for workflows process to finish after a SIGTERM. --- cmd/fission-workflows-bundle/main.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/cmd/fission-workflows-bundle/main.go b/cmd/fission-workflows-bundle/main.go index 0350ccad..8ea0e5a0 100644 --- a/cmd/fission-workflows-bundle/main.go +++ b/cmd/fission-workflows-bundle/main.go @@ -4,6 +4,9 @@ import ( "context" "fmt" "os" + "os/signal" + "syscall" + "time" "github.com/fission/fission-workflows/cmd/fission-workflows-bundle/bundle" "github.com/fission/fission-workflows/pkg/fes/backend/nats" @@ -14,7 +17,21 @@ import ( ) func main() { - ctx := context.Background() + ctx, cancelFn := context.WithCancel(context.Background()) + c := make(chan os.Signal, 1) + signal.Notify(c, os.Interrupt, os.Kill, syscall.SIGTERM) + go func() { + for sig := range c { + fmt.Println("Received signal: ", sig) + go func() { + time.Sleep(30 * time.Second) + fmt.Println("Deadline exceeded; forcing shutdown.") + os.Exit(0) + }() + cancelFn() + break + } + }() cliApp := createCli() cliApp.Action = func(c *cli.Context) error {