-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask.go
More file actions
367 lines (329 loc) · 6.54 KB
/
task.go
File metadata and controls
367 lines (329 loc) · 6.54 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package conversion
import (
"context"
"encoding/json"
"errors"
"fmt"
"sync"
"time"
"github.com/gocacher/cacher"
"go.uber.org/atomic"
)
// Queue ...
type Queue struct {
cacher cacher.Cacher
queuing *sync.Map
tasking *sync.Pool
running *sync.Map
}
// Task ...
type Task struct {
context context.Context
cancel context.CancelFunc
queue *Queue
autoStop *atomic.Bool
Limit int
Interval int
ClearTemp bool
}
// AutoStop ...
func (t *Task) AutoStop() bool {
return t.autoStop.Load()
}
// SetAutoStop ...
func (t *Task) SetAutoStop(autoStop bool) {
t.autoStop.Store(autoStop)
}
// DefaultLimit ...
var DefaultLimit = 3
// NewQueue ...
func NewQueue(c cacher.Cacher) *Queue {
return &Queue{
cacher: c,
queuing: &sync.Map{},
tasking: &sync.Pool{},
running: &sync.Map{},
}
}
// Add ...
func (q *Queue) Add(s string) {
q.queuing.Store(s, nil)
if err := q.cache(); err != nil {
log.Error(err)
}
q.tasking.Put(s)
}
// Delete ...
func (q *Queue) Delete(s string) {
q.queuing.Delete(s)
if err := q.cache(); err != nil {
log.Error(err)
}
}
// Get ...
func (q *Queue) Get() (v string, b bool) {
if t := q.tasking.Get(); t != nil {
if v, b = t.(string); b {
if !q.Has(v) {
return "", false
}
}
}
return
}
// Has ...
func (q *Queue) Has(s string) bool {
_, ok := q.queuing.Load(s)
return ok
}
// Running ...
func (q *Queue) Running(work IWork) (b bool) {
_, b = q.running.LoadOrStore(work.ID(), work)
return
}
// Stop ...
func (q *Queue) Stop(id string) {
if v, b := q.running.Load(id); b {
if work, b := v.(IWork); b {
if e := work.Stop(); e != nil {
log.Error(e)
} else {
return
}
}
}
q.Delete(id)
}
// Finish ...
func (q *Queue) Finish(s string) {
q.running.Delete(s)
q.Delete(s)
}
// IsRunning ...
func (q *Queue) IsRunning(s string) (b bool) {
_, b = q.running.Load(s)
return
}
// List ...
func (q *Queue) List() []string {
var runs []string
q.queuing.Range(func(key, value interface{}) bool {
if v, b := key.(string); b {
runs = append(runs, v)
}
return true
})
return runs
}
// Restore ...
func (q *Queue) Restore() ([]string, error) {
bytes, e := q.cacher.Get("running")
if e != nil {
return nil, e
}
var runs []string
e = json.Unmarshal(bytes, &runs)
if e != nil {
return nil, e
}
for _, run := range runs {
work, err := LoadWork(run)
if err != nil {
return nil, err
}
err = work.Reset()
if err != nil {
return nil, err
}
q.Add(run)
}
return runs, nil
}
func (q *Queue) cache() error {
bytes, e := json.Marshal(q.List())
if e != nil {
return Wrap(e)
}
return Wrap(q.cacher.Set("running", bytes))
}
// AddWorker ...
func (t *Task) AddWorker(work IWork, force bool) error {
log.With("id", work.ID()).Info("add work")
if t.queue.IsRunning(work.ID()) {
return nil
}
iwork, e := LoadWork(work.ID())
if e == nil {
if force {
if err := iwork.Reset(); err != nil {
return Wrap(err, "add work force")
}
}
} else {
if err := work.Store(); err != nil {
return Wrap(err, "add work store")
}
}
return t.StartWork(work.ID())
}
// Stop ...
func (t *Task) Stop() {
if t.cancel != nil {
t.cancel()
}
}
// restore ...
func (t *Task) restore() error {
ss, e := t.queue.Restore()
if e != nil {
return Wrap(e)
}
for _, v := range ss {
t.queue.Add(v)
}
return nil
}
// Start ...
func (t *Task) Start() error {
if !CheckDatabase() {
return errors.New("sql service was not ready")
}
if !CheckNode() {
return errors.New("node service was not ready")
}
if err := t.restore(); err != nil {
//ignore restore:first error key not found
log.Warnw("if not your first run,this has some problems", "error", err)
}
wg := &sync.WaitGroup{}
for i := 0; i < t.Limit; i++ {
wg.Add(1)
log.Infow("task thread start", "idx", i)
go func(wg *sync.WaitGroup) {
defer wg.Done()
WorkEnd:
for {
select {
case <-t.context.Done():
log.With("error", t.context.Err()).Error("done")
return
default:
}
if v, b := t.queue.Get(); b {
work, e := LoadWork(v)
if e != nil {
log.With("id", v, "error", e).Error("load work")
continue
}
if t.queue.Running(work) {
log.With("id", work.ID()).Warn("work was running")
continue
}
switch work.Status() {
case WorkWaiting:
log.With("id", work.ID()).Info("work run")
e = work.Run(t.context)
if e != nil {
log.With("id", work.ID(), "error", e).Error("run")
}
case WorkStopped:
log.With("id", work.ID()).Info("work was stopped")
continue
case WorkRunning:
log.With("id", work.ID()).Warn("work was running")
continue
case WorkFinish:
log.With("id", work.ID()).Warn("work was finished")
continue
default:
log.With("id", work.ID()).Error("work status wrong")
e := work.Reset()
if e != nil {
log.With("id", work.ID(), "error", e).Error("fix status error")
return
}
}
log.With("id", work.ID()).Info("end run")
t.queue.Finish(work.ID())
continue
}
if t.AutoStop() {
break WorkEnd
}
//service queuing for new Work
time.Sleep(5 * time.Second)
}
}(wg)
}
log.Info("waiting for end")
wg.Wait()
return nil
}
// GetWorkStatus ...
func (t *Task) GetWorkStatus(id string) (WorkStatus, error) {
work, e := LoadWork(id)
if e != nil {
return WorkAbnormal, fmt.Errorf("get status:%w", e)
}
ok := t.queue.Has(work.ID())
if !ok && work.Status() == WorkRunning {
return WorkWaiting, nil
}
return work.Status(), nil
}
// GetWork ...
func (t *Task) GetWork(id string) (IWork, error) {
return LoadWork(id)
}
// StartWork ...
func (t *Task) StartWork(id string) error {
iwork, e := LoadWork(id)
if e != nil {
return Wrap(e)
}
if iwork.Status() == WorkStopped {
if err := iwork.Reset(); err != nil {
return Wrap(err)
}
}
t.queue.Add(iwork.ID())
return nil
}
// StopWork ...
func (t *Task) StopWork(id string) {
//stop running
t.queue.Stop(id)
//change stop status
iwork, err := LoadWork(id)
if err == nil {
if e := iwork.Stop(); e != nil {
log.Error(e)
} else {
return
}
} else {
log.Error(err)
}
}
// AllRun ...
func (t *Task) AllRun() (works []IWork, e error) {
for _, v := range t.queue.List() {
iwork, err := LoadWork(v)
if err != nil {
return nil, err
}
works = append(works, iwork)
}
return works, nil
}
// NewTask ...
func NewTask() *Task {
ctx, cancel := context.WithCancel(context.Background())
return &Task{
context: ctx,
cancel: cancel,
queue: NewQueue(_cache),
autoStop: atomic.NewBool(true),
Limit: DefaultLimit,
}
}