diff options
author | Keuin <[email protected]> | 2023-07-29 21:27:47 +0800 |
---|---|---|
committer | Keuin <[email protected]> | 2023-07-29 21:27:47 +0800 |
commit | 03d9784bf682b05e3c4a46eb6cd32f4319e8636b (patch) | |
tree | 5a0f8052804304a2088ff3c57cd0b9bfccf34902 /recording | |
parent | 2f0ef363eabce940314dc33f5966335642722fa2 (diff) |
Make status field concurrent safe
Diffstat (limited to 'recording')
-rw-r--r-- | recording/runner.go | 6 | ||||
-rw-r--r-- | recording/task.go | 19 |
2 files changed, 15 insertions, 10 deletions
diff --git a/recording/runner.go b/recording/runner.go index ca4fe48..9671029 100644 --- a/recording/runner.go +++ b/recording/runner.go @@ -42,7 +42,7 @@ var errLiveEnded = errs.NewError(errs.LiveEnded) // During the process, its status may change. // Note: this method is blocking. func (t *RunningTask) runTaskWithAutoRestart() { - t.status = StWaiting + t.setStatus(StWaiting) loop: for { err := tryRunTask(t) @@ -56,7 +56,7 @@ loop: if !errors.Is(err, errLiveEnded) { t.logger.Error("Temporary error: %v", err) } - t.status = StRestarting + t.setStatus(StRestarting) default: t.logger.Error("Cannot recover from error: %v", err) break loop @@ -171,7 +171,7 @@ func tryRunTask(t *RunningTask) error { case nil: // live is started, start recording // (now the watcher should have stopped) - t.status = StRecording + t.setStatus(StRecording) return func() error { var err error run := true diff --git a/recording/task.go b/recording/task.go index d1f9c79..328fe66 100644 --- a/recording/task.go +++ b/recording/task.go @@ -14,7 +14,7 @@ import ( "time" ) -type TaskStatus int +type TaskStatus uint32 const ( StNotStarted TaskStatus = iota @@ -48,7 +48,7 @@ type RunningTask struct { // ctx: the biggest context this task uses. It may create children contexts. ctx context.Context // status: running status - status TaskStatus + status atomic.Uint32 // hookStarted: called asynchronously when the task is started. This won't be called when restarting. hookStarted func() // hookStopped: called asynchronously when the task is stopped. This won't be called when restarting. @@ -58,8 +58,12 @@ type RunningTask struct { roomTitle atomic.Pointer[string] } +func (t *RunningTask) setStatus(status TaskStatus) { + t.status.Store(uint32(status)) +} + func (t *RunningTask) GetStatus() TaskStatus { - return t.status + return TaskStatus(t.status.Load()) } func (t *RunningTask) GetRoomTitle() *string { @@ -73,23 +77,24 @@ func NewRunningTask( hookStopped func(), logger logging.Logger, ) *RunningTask { - return &RunningTask{ + t := &RunningTask{ TaskConfig: config, ctx: ctx, - status: StNotStarted, hookStarted: hookStarted, hookStopped: hookStopped, logger: logger, } + t.setStatus(StNotStarted) + return t } func (t *RunningTask) StartTask() error { - st := t.status + st := t.GetStatus() switch st { case StNotStarted: // TODO real start go func() { - defer func() { t.status = StStopped }() + defer func() { t.setStatus(StStopped) }() t.hookStarted() defer t.hookStopped() // do the task |