From 21dd5d2d24413c67e19fdcce9a6bedf3ce7d22e9 Mon Sep 17 00:00:00 2001 From: Keuin Date: Mon, 12 Sep 2022 12:24:26 +0800 Subject: Fix record restarting logic. --- recording/runner.go | 62 ++++++++++++++++++++++++----------------------------- 1 file changed, 28 insertions(+), 34 deletions(-) diff --git a/recording/runner.go b/recording/runner.go index e3735cb..58bcbc9 100644 --- a/recording/runner.go +++ b/recording/runner.go @@ -37,7 +37,13 @@ func (t *RunningTask) runTaskWithAutoRestart() error { for { t.status = StRunning err := tryRunTask(t) - if errors.Is(err, bilibili.ErrRoomIsClosed) { + if err == nil || + errors.Is(err, bilibili.ErrRoomIsClosed) || + errors.Is(err, io.EOF) || + errors.Is(err, io.ErrUnexpectedEOF) { + if !errors.Is(err, io.EOF) { + t.logger.Error("Task stopped because of an error: %v", err) + } t.status = StRestarting t.logger.Info("Restarting task...") continue @@ -93,40 +99,28 @@ func tryRunTask(t *RunningTask) error { recorderCtx, stopRecorder := context.WithCancel(t.ctx) defer stopRecorder() - for { - select { - case <-t.ctx.Done(): - t.logger.Info("Task is stopped.") - return nil - case <-chWatcherDown: - // watcher is down and unrecoverable, stop this task - return fmt.Errorf("task (room %v) stopped: watcher is down and unrecoverable", t.RoomId) - case ev := <-chWatcherEvent: - switch ev { - case WatcherLiveStart: - cancelled := false - var err2 error - // restart recorder if interrupted by I/O errors - for !cancelled { - cancelled, err2 = record(recorderCtx, bi, t) - if errors.Is(err2, io.ErrUnexpectedEOF) { - t.logger.Warning("Reading is interrupted because of an unexpected EOF. Retrying...") - cancelled = false - } - } - t.logger.Error("Error when copying live stream: %v", err2) - if err2 == nil || errors.Is(err2, bilibili.ErrRoomIsClosed) || errors.Is(err2, io.EOF) { - t.logger.Info("Live is ended. Stop recording.") - return bilibili.ErrRoomIsClosed - } - t.logger.Error("Cannot recover from unexpected error: %v", err2) - t.logger.Info("Task is cancelled. Stop recording.") - case WatcherLiveStop: - // once the live is ended, the watcher will no longer receive live start event - // we have to restart the watcher - return bilibili.ErrRoomIsClosed - } + + ev := <-chWatcherEvent + switch ev { + case WatcherLiveStart: + var err2 error + // restart recorder if interrupted by I/O errors + _, err2 = record(recorderCtx, bi, t) + if errors.Is(err2, io.EOF) { + t.logger.Info("The live seems to be closed normally.") + } else if errors.Is(err2, io.ErrUnexpectedEOF) { + t.logger.Warning("Reading is interrupted because of an unexpected EOF.") + } else { + t.logger.Error("Error when copying live stream: %v", err2) } + t.logger.Info("Stop recording.") + return err2 + case WatcherLiveStop: + // once the live is ended, the watcher will no longer receive live start event + // we have to restart the watcher + return bilibili.ErrRoomIsClosed + default: + return fmt.Errorf("unknown watcher event: %v", ev) } } -- cgit v1.2.3