-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathworker.go
183 lines (155 loc) · 3.48 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package falcon
import (
"context"
"encoding/json"
"fmt"
"sync"
)
type Config struct {
Before func(*Worker) error
Job func(*Worker) error
OnSuccess func(*Worker)
OnError func(error, *Worker)
OnComplete func(*Worker)
}
type Worker struct {
*Config `json:"-"`
id int
state *State[string, any]
parent *Engine
queue chan any
ctx context.Context
mu sync.RWMutex
}
type WorkerStatus string
var (
WorkerStatusUnknown WorkerStatus = "unknown"
WorkerStatusWaiting WorkerStatus = "waiting"
WorkerStatusPreProcessing WorkerStatus = "pre-processing"
WorkerStatusProcessing WorkerStatus = "processing"
WorkerStatusSuccess WorkerStatus = "success"
WorkerStatusClosing WorkerStatus = "closing"
WorkerStatusClosed WorkerStatus = "closed"
WorkerStatusError WorkerStatus = "error"
)
var DefaultConfig = &Config{
Before: func(w *Worker) error {
return nil
},
Job: func(w *Worker) error {
return nil
},
OnSuccess: func(w *Worker) {},
OnError: func(err error, w *Worker) {},
OnComplete: func(w *Worker) {},
}
func NewWorker(e *Engine, id int, cfg ...*Config) *Worker {
w := &Worker{
Config: DefaultConfig,
id: id,
state: NewState[string, any](),
}
if len(cfg) == 1 {
w.Config = cfg[0]
}
w.state.Set("status", WorkerStatusWaiting)
return w
}
func (w *Worker) GetId() int {
w.mu.RLock()
defer w.mu.RUnlock()
return w.id
}
func (w *Worker) GetStatus() WorkerStatus {
if st, ok := w.GetState("status"); ok {
return st.(WorkerStatus)
}
return WorkerStatusUnknown
}
func (w *Worker) GetState(state string) (any, bool) {
return w.state.Get(state)
}
func (w *Worker) GetContext() context.Context {
return w.ctx
}
func (w *Worker) SetState(state string, v any) {
w.state.Set(state, v)
}
func (w *Worker) Parent() *Engine {
return w.parent
}
func (w *Worker) Close() error {
w.state.Set("status", WorkerStatusClosed)
return nil
}
func (w *Worker) MarshalJSON() ([]byte, error) {
w.mu.RLock()
defer w.mu.RUnlock()
output := struct {
Id int `json:"id"`
State *State[string, any] `json:"state"`
}{
Id: w.id,
State: w.state,
}
return json.Marshal(output)
}
func (w *Worker) String() string {
b, _ := json.Marshal(w)
return string(b)
}
func (w *Worker) work() {
for {
select {
case <-w.ctx.Done():
w.state.Set("status", WorkerStatusClosing)
w.Close()
return
case msg := <-w.queue:
if msg == nil {
// nothing to do
continue
}
// make a done channel to keep
// track of the worker status
done := make(chan bool)
go func(w *Worker) {
defer func() {
if r := recover(); r != nil {
w.OnError(fmt.Errorf("%v", r), w)
}
if w.OnComplete != nil {
w.OnComplete(w)
}
w.state.Reset()
w.state.Set("status", WorkerStatusWaiting)
done <- true
}()
w.state.Set("message", msg)
if w.Config == nil {
// fmt.Println("no configured jobs")
return
}
if w.Before != nil {
w.state.Set("status", WorkerStatusPreProcessing)
if err := w.Before(w); err != nil {
w.state.Set("status", WorkerStatusError)
w.OnError(err, w)
return
}
}
w.state.Set("status", WorkerStatusProcessing)
if err := w.Job(w); err != nil {
w.state.Set("status", WorkerStatusError)
w.OnError(err, w)
return
}
// fmt.Println("on success:")
w.state.Set("status", WorkerStatusSuccess)
w.OnSuccess(w)
}(w)
// wait for the job to finish
<-done
}
}
}