Sybren A. Stüvel
0b39f229a1
Worker and Manager implementation of the "may-I-kee-running" protocol. While running tasks, the Worker will ask the Manager periodically whether it's still allowed to keep running that task. This allows the Manager to abort commands on Workers when: - the Worker should go to another state (typically 'asleep' or 'shutdown'), - the task changed status from 'active' to something non-runnable (typically 'canceled' when the job as a whole is canceled). - the task has been assigned to a different Worker. This can happen when a Worker loses its connection to its Manager, resulting in a task timeout (not yet implemented) after which the task can be assigned to another Worker. If then the connectivity is restored, the first Worker should abort (last-assigned Worker wins).
44 lines
855 B
Go
44 lines
855 B
Go
package worker
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"git.blender.org/flamenco/pkg/api"
|
|
)
|
|
|
|
const durationSleepCheck = 3 * time.Second
|
|
|
|
func (w *Worker) gotoStateAsleep(ctx context.Context) {
|
|
w.stateMutex.Lock()
|
|
defer w.stateMutex.Unlock()
|
|
|
|
w.state = api.WorkerStatusAsleep
|
|
w.doneWg.Add(2)
|
|
w.ackStateChange(ctx, w.state)
|
|
go w.runStateAsleep(ctx)
|
|
}
|
|
|
|
func (w *Worker) runStateAsleep(ctx context.Context) {
|
|
defer w.doneWg.Done()
|
|
logger := w.loggerWithStatus()
|
|
logger.Info().Msg("sleeping")
|
|
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
logger.Debug().Msg("asleep state interrupted by context cancellation")
|
|
return
|
|
case <-w.doneChan:
|
|
logger.Debug().Msg("asleep state interrupted by shutdown")
|
|
return
|
|
case <-time.After(durationSleepCheck):
|
|
if w.changeStateIfRequested(ctx) {
|
|
return
|
|
}
|
|
}
|
|
}
|
|
}
|