diff options
author | Keuin <[email protected]> | 2022-09-08 01:14:20 +0800 |
---|---|---|
committer | Keuin <[email protected]> | 2022-09-08 01:14:20 +0800 |
commit | 493ad1a723f9ade3bd049b156f0dc4d194f8fd3e (patch) | |
tree | f9d76845a7ae5382e58b13936d732de2b3e98598 /main.go | |
parent | 1009e88ff752525966708c56190c2dfa32bc9537 (diff) |
Completely fix timing of goroutines. Implement graceful shutdown correctly.
Diffstat (limited to 'main.go')
-rw-r--r-- | main.go | 38 |
1 files changed, 13 insertions, 25 deletions
@@ -120,37 +120,25 @@ func main() { logger := log.Default() logger.Printf("Starting tasks...") - chResult := make(chan recording.TaskResult) wg := sync.WaitGroup{} + defer func() { + wg.Wait() + logger.Println("Stopping YABR...") + }() ctx, cancelTasks := context.WithCancel(context.Background()) for _, task := range tasks { wg.Add(1) - go recording.RunTask( - ctx, - &wg, - &task, - chResult, - ) + go recording.RunTask(ctx, &wg, &task) } + // listen Ctrl-C chSigInt := make(chan os.Signal) signal.Notify(chSigInt, os.Interrupt) -loop: - for { - select { - case <-chSigInt: - logger.Println("YABR is stopped.") - cancelTasks() - break loop - case result := <-chResult: - err := result.Error - if err != nil { - logger.Printf("A task stopped with an error (room %v): %v\n", - result.Task.RoomId, result.Error) - } else { - logger.Printf("Task stopped (room %v): %v\n", - result.Task.RoomId, result.Task.String()) - } - } - } + go func() { + <-chSigInt + cancelTasks() + }() + + // block main goroutine on task goroutines + wg.Wait() } |