Worker: fix race condition getting logger with worker status

This commit is contained in:
Sybren A. Stüvel 2022-04-21 19:12:53 +02:00
parent 6bdc198301
commit bcbacf6c42
3 changed files with 15 additions and 6 deletions

View File

@ -6,8 +6,6 @@ import (
"context" "context"
"time" "time"
"github.com/rs/zerolog/log"
"git.blender.org/flamenco/pkg/api" "git.blender.org/flamenco/pkg/api"
) )
@ -25,7 +23,7 @@ func (w *Worker) gotoStateAsleep(ctx context.Context) {
func (w *Worker) runStateAsleep(ctx context.Context) { func (w *Worker) runStateAsleep(ctx context.Context) {
defer w.doneWg.Done() defer w.doneWg.Done()
logger := log.With().Str("status", string(w.state)).Logger() logger := w.loggerWithStatus()
logger.Info().Msg("sleeping") logger.Info().Msg("sleeping")
for { for {

View File

@ -36,9 +36,9 @@ func (w *Worker) runStateAwake(ctx context.Context) {
err := recover() err := recover()
if err != nil { if err != nil {
w.SignOff(ctx) w.SignOff(ctx)
log.Panic(). logger := w.loggerWithStatus()
logger.Panic().
Interface("panic", err). Interface("panic", err).
Str("workerStatus", string(w.state)).
Msg("panic, so signed off and going to stop") Msg("panic, so signed off and going to stop")
} }
}() }()
@ -71,7 +71,7 @@ func (w *Worker) runStateAwake(ctx context.Context) {
// fetchTasks periodically tries to fetch a task from the Manager, returning it when obtained. // fetchTasks periodically tries to fetch a task from the Manager, returning it when obtained.
// Returns nil when a task could not be obtained and the period loop was cancelled. // Returns nil when a task could not be obtained and the period loop was cancelled.
func (w *Worker) fetchTask(ctx context.Context) *api.AssignedTask { func (w *Worker) fetchTask(ctx context.Context) *api.AssignedTask {
logger := log.With().Str("status", string(w.state)).Logger() logger := w.loggerWithStatus()
// Initially don't wait at all. // Initially don't wait at all.
var wait time.Duration var wait time.Duration

View File

@ -5,6 +5,7 @@ package worker
import ( import (
"context" "context"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"git.blender.org/flamenco/pkg/api" "git.blender.org/flamenco/pkg/api"
@ -65,3 +66,13 @@ func (w *Worker) ackStateChange(ctx context.Context, state api.WorkerStatus) {
return return
} }
} }
// loggerWithStatus returns a logger with its current status mentioned.
// This is a thread-safe way of getting the logger.
func (w *Worker) loggerWithStatus() zerolog.Logger {
w.stateMutex.Lock()
defer w.stateMutex.Unlock()
logger := log.With().Str("workerStatus", string(w.state)).Logger()
return logger
}