-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtask_worker.go
168 lines (138 loc) · 3.55 KB
/
task_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
package server
import (
"sync"
"time"
"github.com/doomsday-project/doomsday/server/logger"
)
type taskWorkerFactory struct {
sched *taskQueue
cache *Cache
log *logger.Logger
curID uint
}
func newTaskWorkerFactory(sched *taskQueue, cache *Cache, log *logger.Logger) *taskWorkerFactory {
return &taskWorkerFactory{
sched: sched,
cache: cache,
log: log,
}
}
func (f *taskWorkerFactory) newWorker() *taskWorker {
ret := &taskWorker{
sched: f.sched,
cache: f.cache,
log: f.log,
id: f.curID,
state: WorkerStateIdle,
stateAt: time.Now(),
}
f.curID++
return ret
}
type taskWorker struct {
sched *taskQueue
cache *Cache
log *logger.Logger
id uint
//Never try to grab the scheduler lock while holding this lock.
// You can try to grab this lock while holding the scheduler lock, but
// not in the reverse order.
stateLock sync.RWMutex
state WorkerState
stateAt time.Time
}
//WorkerState is the current thing the worker is doing. i.e. idle vs running
type WorkerState uint
const (
//WorkerStateIdle means the worker is currently waiting for a task
WorkerStateIdle = iota
//WorkerStateRunning means the worker is currently running a task
WorkerStateRunning
//WorkerStateScheduling means the worker is currently scheduling a future task
WorkerStateScheduling
)
func (w WorkerState) String() string {
ret := "unknown"
switch w {
case WorkerStateIdle:
ret = "idle"
case WorkerStateRunning:
ret = "running"
case WorkerStateScheduling:
ret = "scheduling"
}
return ret
}
type taskWorkerState struct {
state *WorkerState
}
func (w *taskWorker) consumeScheduler() {
go func() {
for {
next := w.runNext()
w.scheduleNextRunOf(next)
}
}()
}
//runNext blocks until there is a task for this worker to handle. it then
//dequeues and runs that task if it is not marked to skip.
func (w *taskWorker) runNext() managerTask {
w.sched.lock.Lock()
for w.sched.empty() || w.sched.data[0].state == queueTaskStatePending {
w.sched.cond.Wait()
}
ret := w.sched.dequeueNoLock()
if ret.state == queueTaskStateSkip {
w.sched.lock.Unlock()
w.log.WriteF("Worker %d skipping %s %s of `%s'", w.id, ret.reason, ret.kind, ret.source.Core.Name)
return ret
}
ret.assignedWorker = w
w.sched.running = append(w.sched.running, ret)
w.SetState(WorkerStateRunning)
w.sched.lock.Unlock()
w.log.WriteF("Worker %d running %s %s of `%s'", w.id, ret.reason, ret.kind, ret.source.Core.Name)
ret.run(w.cache, w.log)
w.SetState(WorkerStateScheduling)
w.sched.lock.Lock()
w.sched.running.deleteTaskWithID(ret.id)
w.sched.lock.Unlock()
w.SetState(WorkerStateIdle)
return ret
}
func (w *taskWorker) scheduleNextRunOf(task managerTask) {
if task.reason == runReasonAdhoc {
return
}
var nextTime time.Time
var skipSched bool
switch task.kind {
case queueTaskKindAuth:
nextTime, skipSched = task.source.CalcNextAuth()
case queueTaskKindRefresh:
nextTime = task.source.CalcNextRefresh()
}
if skipSched {
w.log.WriteF("Skipping further scheduling of `%s' for `%s'", task.kind.String(), task.source.Core.Name)
return
}
w.sched.enqueue(managerTask{
source: task.source,
runTime: nextTime,
reason: runReasonSchedule,
kind: task.kind,
})
}
//State returns the current state this worker is in, and the time that it
// entered that state at
func (w *taskWorker) State() (WorkerState, time.Time) {
w.stateLock.RLock()
defer w.stateLock.RUnlock()
return w.state, w.stateAt
}
func (w *taskWorker) SetState(state WorkerState) {
w.stateLock.Lock()
defer w.stateLock.Unlock()
w.state = state
w.stateAt = time.Now()
}