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 /recording | |
parent | 1009e88ff752525966708c56190c2dfa32bc9537 (diff) |
Completely fix timing of goroutines. Implement graceful shutdown correctly.
Diffstat (limited to 'recording')
-rw-r--r-- | recording/runner.go | 22 |
1 files changed, 17 insertions, 5 deletions
diff --git a/recording/runner.go b/recording/runner.go index bcea9a7..7b82a5c 100644 --- a/recording/runner.go +++ b/recording/runner.go @@ -27,12 +27,14 @@ type TaskResult struct { // RunTask start a monitor&download task and // put its execution result into a channel. -func RunTask(ctx context.Context, wg *sync.WaitGroup, task *TaskConfig, chTaskResult chan<- TaskResult) { +func RunTask(ctx context.Context, wg *sync.WaitGroup, task *TaskConfig) { defer wg.Done() err := doTask(ctx, task) - chTaskResult <- TaskResult{ - Task: task, - Error: err, + logger := log.Default() + if err != nil && !errors.Is(err, context.Canceled) { + logger.Printf("A task stopped with an error (room %v): %v\n", task.RoomId, err) + } else { + logger.Printf("Task stopped (room %v): %v\n", task.RoomId, task.String()) } } @@ -108,6 +110,7 @@ func record( logger.Printf("INFO: Getting room profile...\n") profile, err := common.AutoRetry( + ctx, func() (bilibili.RoomProfileResponse, error) { return bi.GetRoomProfile(task.RoomId) }, @@ -115,6 +118,10 @@ func record( time.Duration(task.Transport.RetryIntervalSeconds)*time.Second, logger, ) + if errors.Is(err, context.Canceled) { + cancelled = true + return + } if err != nil { // still error, abort logger.Printf("ERROR: Cannot get room information: %v. Stopping current task.\n", err) @@ -123,6 +130,7 @@ func record( } urlInfo, err := common.AutoRetry( + ctx, func() (bilibili.RoomUrlInfoResponse, error) { return bi.GetStreamingInfo(task.RoomId) }, @@ -130,6 +138,10 @@ func record( time.Duration(task.Transport.RetryIntervalSeconds)*time.Second, logger, ) + if errors.Is(err, context.Canceled) { + cancelled = true + return + } if err != nil { logger.Printf("ERROR: Cannot get streaming info: %v", err) cancelled = true @@ -162,7 +174,7 @@ func record( logger.Printf("Recording live stream to file \"%v\"...", filePath) err = bi.CopyLiveStream(ctx, task.RoomId, streamSource, file) - cancelled = err == nil + cancelled = err == nil || errors.Is(err, context.Canceled) if !cancelled { // real error happens logger.Printf("Error when copying live stream: %v\n", err) |