2022-01-28 16:02:12 +00:00
|
|
|
package worker
|
|
|
|
|
2022-03-07 14:26:46 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
2022-02-22 18:00:44 +00:00
|
|
|
|
2022-01-28 16:02:12 +00:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/rs/zerolog/log"
|
2022-03-01 19:45:09 +00:00
|
|
|
|
|
|
|
"git.blender.org/flamenco/pkg/api"
|
2022-01-28 16:02:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Worker performs regular Flamenco Worker operations.
|
|
|
|
type Worker struct {
|
|
|
|
doneChan chan struct{}
|
|
|
|
doneWg *sync.WaitGroup
|
|
|
|
|
2022-08-12 18:15:19 +00:00
|
|
|
// Will be closed by the Worker when it wants to shut down. See Worker.WaitForShutdown().
|
|
|
|
shutdown chan struct{}
|
|
|
|
|
2022-02-22 16:13:04 +00:00
|
|
|
client FlamencoClient
|
2022-01-28 16:02:12 +00:00
|
|
|
|
|
|
|
state api.WorkerStatus
|
2022-01-31 14:01:51 +00:00
|
|
|
stateStarters map[api.WorkerStatus]StateStarter // gotoStateXXX functions
|
2022-01-28 16:02:12 +00:00
|
|
|
stateMutex *sync.Mutex
|
|
|
|
|
|
|
|
taskRunner TaskRunner
|
|
|
|
}
|
|
|
|
|
2022-01-31 14:01:51 +00:00
|
|
|
type StateStarter func(context.Context)
|
2022-01-28 16:02:12 +00:00
|
|
|
|
2022-01-31 15:05:27 +00:00
|
|
|
type TaskRunner interface {
|
|
|
|
Run(ctx context.Context, task api.AssignedTask) error
|
|
|
|
}
|
2022-01-28 16:02:12 +00:00
|
|
|
|
|
|
|
// NewWorker constructs and returns a new Worker.
|
|
|
|
func NewWorker(
|
2022-02-22 16:13:04 +00:00
|
|
|
flamenco FlamencoClient,
|
2022-01-28 16:02:12 +00:00
|
|
|
taskRunner TaskRunner,
|
|
|
|
) *Worker {
|
|
|
|
|
|
|
|
worker := &Worker{
|
|
|
|
doneChan: make(chan struct{}),
|
|
|
|
doneWg: new(sync.WaitGroup),
|
2022-08-12 18:15:19 +00:00
|
|
|
shutdown: make(chan struct{}),
|
2022-01-28 16:02:12 +00:00
|
|
|
|
|
|
|
client: flamenco,
|
|
|
|
|
|
|
|
state: api.WorkerStatusStarting,
|
2022-01-31 14:01:51 +00:00
|
|
|
stateStarters: make(map[api.WorkerStatus]StateStarter),
|
2022-01-28 16:02:12 +00:00
|
|
|
stateMutex: new(sync.Mutex),
|
|
|
|
|
2022-01-31 14:01:51 +00:00
|
|
|
taskRunner: taskRunner,
|
2022-01-28 16:02:12 +00:00
|
|
|
}
|
2022-01-31 14:01:51 +00:00
|
|
|
worker.setupStateMachine()
|
2022-01-28 16:02:12 +00:00
|
|
|
return worker
|
|
|
|
}
|
|
|
|
|
2022-01-31 14:01:51 +00:00
|
|
|
// Start starts the worker by sending it to the given state.
|
|
|
|
func (w *Worker) Start(ctx context.Context, state api.WorkerStatus) {
|
|
|
|
w.changeState(ctx, state)
|
2022-01-28 16:02:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close gracefully shuts down the Worker.
|
|
|
|
func (w *Worker) Close() {
|
|
|
|
log.Debug().Msg("worker gracefully shutting down")
|
|
|
|
close(w.doneChan)
|
|
|
|
w.doneWg.Wait()
|
|
|
|
}
|
2022-08-12 18:15:19 +00:00
|
|
|
|
|
|
|
// WaitForShutdown waits until Flamenco wants to shut down the application.
|
|
|
|
// Returns `true` when the Worker has signalled it wants to shut down.
|
|
|
|
// Returns `false` when the shutdown was caused by the context closing.
|
|
|
|
func (w *Worker) WaitForShutdown(ctx context.Context) bool {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return false
|
|
|
|
case <-w.shutdown:
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|