-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.go
65 lines (54 loc) · 1.87 KB
/
worker.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package uwe
import (
"context"
"github.com/lancer-kit/sam"
"github.com/pkg/errors"
)
type WorkerName string
// Worker is an interface for async workers
// which launches and manages by the `Chief`.
type Worker interface {
// Init initializes some state of the worker that required interaction with outer context,
// for example, initialize some connectors. In many cases this method is optional,
// so it can be implemented as empty: `func (*W) Init() error { return nil }`.
Init() error
// Run starts the `Worker` instance execution. The context will provide a signal
// when a worker must stop through the `ctx.Done()`.
Run(ctx Context) error
}
// workerRO worker runtime object, hold worker instance, state and communication chanel
type workerRO struct {
sam.StateMachine
worker Worker
canceler context.CancelFunc
}
const (
WStateNotExists sam.State = "NotExists"
WStateNew sam.State = "New"
WStateInitialized sam.State = "Initialized"
WStateRun sam.State = "Run"
WStateStopped sam.State = "Stopped"
WStateFailed sam.State = "Failed"
)
// newWorkerSM returns filled state machine of the worker lifecycle
//
// (*) -> [New] -> [Initialized] -> [Run] -> [Stopped]
// | | |
// | | ↓
// |-------------|------> [Failed]
func newWorkerSM() (sam.StateMachine, error) {
sm := sam.NewStateMachine()
s := &sm
workerSM, err := s.
AddTransitions(WStateNew, WStateInitialized, WStateFailed).
AddTransitions(WStateInitialized, WStateRun, WStateFailed).
AddTransitions(WStateRun, WStateStopped, WStateFailed).
Finalize(WStateStopped)
if err != nil || workerSM == nil {
return sm, errors.Wrap(err, "worker state machine init failed: ")
}
if err = workerSM.SetState(WStateNew); err != nil {
return sm, errors.Wrap(err, "failed to set state new")
}
return workerSM.Clone(), nil
}