From e999937d75d8e8c40b06add376ebac423b0c2079 Mon Sep 17 00:00:00 2001 From: Keuin Date: Sat, 10 Sep 2022 16:05:10 +0800 Subject: Fix task is not properly restarted when the live is closed and started again. Use more friendly log format to replace golang's default `log.Logger`. (not completed) Cleaner task status management. --- main.go | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) (limited to 'main.go') diff --git a/main.go b/main.go index 6b68ee0..2b22cfc 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,13 @@ package main +/* +In this file we implement config file and command line arguments parsing. +Task lifecycle management are implemented in recording package. +*/ + import ( "bilibili-livestream-archiver/common" + "bilibili-livestream-archiver/logging" "bilibili-livestream-archiver/recording" "context" "fmt" @@ -127,23 +133,32 @@ func getTasks() (tasks []recording.TaskConfig) { } func main() { - tasks := getTasks() + logger := log.Default() + taskConfigs := getTasks() + tasks := make([]recording.RunningTask, len(taskConfigs)) + wg := sync.WaitGroup{} + ctxTasks, cancelTasks := context.WithCancel(context.Background()) fmt.Println("Record tasks:") - for i, task := range tasks { + for i, task := range taskConfigs { + tasks[i] = recording.NewRunningTask( + taskConfigs[i], + ctxTasks, + func() { wg.Add(1) }, + func() { wg.Done() }, + logging.NewWrappedLogger(logger, fmt.Sprintf("room %v", task.RoomId)), + ) fmt.Printf("[%2d] %s\n", i+1, task) } fmt.Println("") - logger := log.Default() - logger.Printf("Starting tasks...") - wg := sync.WaitGroup{} - ctx, cancelTasks := context.WithCancel(context.Background()) - for _, task := range tasks { - wg.Add(1) - go recording.RunTask(ctx, &wg, &task) + for i := range tasks { + err := tasks[i].StartTask() + if err != nil { + logger.Printf("Cannot start task %v (room %v): %v. Skip.", i, tasks[i].RoomId, err) + } } // listen on stop signals -- cgit v1.2.3