From aced234f80489f10e678cbe8ab2d44fe50c8e376 Mon Sep 17 00:00:00 2001 From: Keuin Date: Thu, 8 Sep 2022 01:44:58 +0800 Subject: Correctly handle signals on exit. --- main.go | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index dc50251..db58eca 100644 --- a/main.go +++ b/main.go @@ -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() } -- cgit v1.2.3