Worker: capture panics in 'active' state and cleanly sign off before dying

This commit is contained in:
Sybren A. Stüvel 2022-02-28 12:07:10 +01:00
parent 7689a988b1
commit b5b7b228ed

View File

@ -37,17 +37,28 @@ const (
func (w *Worker) gotoStateAwake(ctx context.Context) { func (w *Worker) gotoStateAwake(ctx context.Context) {
w.stateMutex.Lock() w.stateMutex.Lock()
defer w.stateMutex.Unlock()
w.state = api.WorkerStatusAwake w.state = api.WorkerStatusAwake
w.stateMutex.Unlock()
w.doneWg.Add(2) w.doneWg.Add(2)
w.ackStateChange(ctx, w.state) w.ackStateChange(ctx, w.state)
go w.runStateAwake(ctx) go w.runStateAwake(ctx)
} }
// runStateAwake fetches a task and executes it, in an endless loop. // runStateAwake fetches a task and executes it, in an endless loop.
func (w *Worker) runStateAwake(ctx context.Context) { func (w *Worker) runStateAwake(ctx context.Context) {
defer func() {
err := recover()
if err != nil {
w.SignOff(ctx)
log.Panic().
Interface("panic", err).
Str("workerStatus", string(w.state)).
Msg("panic, so signed off and going to stop")
}
}()
defer w.doneWg.Done() defer w.doneWg.Done()
defer log.Debug().Msg("stopping state 'awake'") defer log.Debug().Msg("stopping state 'awake'")