-
Notifications
You must be signed in to change notification settings - Fork 0
/
processing_worker.go
76 lines (62 loc) · 1.15 KB
/
processing_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
package core
import (
"context"
"runtime"
"time"
)
type stopChan chan bool
type processingWorker interface {
Run()
Stop()
}
type _processingWorker struct {
stackM *stackManager
stack *stack
dispatch dispatchManager
sch Scheduler
stop stopChan
ctx context.Context
cancel context.CancelFunc
}
func newProcessingWorker(ctx context.Context, stack *stack, sch Scheduler, dispatch dispatchManager) processingWorker {
ctx, cancel := context.WithCancel(ctx)
return &_processingWorker{
sch: sch,
stack: stack,
dispatch: dispatch,
ctx: ctx,
cancel: cancel,
}
}
func (p *_processingWorker) Run() {
go func() {
for {
select {
case <-p.ctx.Done():
return
default:
p.process()
runtime.Gosched()
}
}
}()
}
func (p *_processingWorker) Stop() {
p.cancel()
}
func (p *_processingWorker) process() {
p.stack.Lock()
defer p.stack.Unlock()
if p.stack.len == 0 {
return
}
e := p.stack.Get(0)
now := time.Now()
if e.ShouldExecuteAt.Before(now) || e.ShouldExecuteAt.Equal(now) {
p.stack.Pop()
p.dispatch.Dispatch(*e.event)
if e.Mode == CronMode {
p.sch.schedule(*e.event)
}
}
}