diff options
author | Keuin <[email protected]> | 2022-09-08 01:44:58 +0800 |
---|---|---|
committer | Keuin <[email protected]> | 2022-09-08 01:44:58 +0800 |
commit | aced234f80489f10e678cbe8ab2d44fe50c8e376 (patch) | |
tree | 8542f8fc09b31288f048bdf655bb3bce3b82d4ca | |
parent | 493ad1a723f9ade3bd049b156f0dc4d194f8fd3e (diff) |
Correctly handle signals on exit.
-rw-r--r-- | main.go | 19 |
1 files changed, 15 insertions, 4 deletions
@@ -11,6 +11,7 @@ import ( "os" "os/signal" "sync" + "syscall" ) var globalConfig *GlobalConfig @@ -131,14 +132,24 @@ func main() { go recording.RunTask(ctx, &wg, &task) } - // listen Ctrl-C - chSigInt := make(chan os.Signal) - signal.Notify(chSigInt, os.Interrupt) + // listen on stop signals + chSigStop := make(chan os.Signal) + signal.Notify(chSigStop, + syscall.SIGHUP, + syscall.SIGINT, + syscall.SIGTERM) go func() { - <-chSigInt + <-chSigStop cancelTasks() }() + chSigQuit := make(chan os.Signal) + signal.Notify(chSigQuit, syscall.SIGQUIT) + go func() { + <-chSigQuit + os.Exit(0) + }() + // block main goroutine on task goroutines wg.Wait() } |