forked from didil/goblero
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dispatch.go
163 lines (134 loc) · 3.42 KB
/
dispatch.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
package blero
import (
"fmt"
"io"
"os"
"sync"
)
// dispatcher struct
type dispatcher struct {
dispatchL sync.Mutex
ch chan int
quitCh chan struct{}
pStore *processorsStore
}
// newDispatcher creates new Dispatcher
func newDispatcher(pStore *processorsStore) *dispatcher {
d := &dispatcher{}
d.ch = make(chan int, 100)
d.quitCh = make(chan struct{})
d.pStore = pStore
return d
}
// testable stderr
var stdErr io.ReadWriter = os.Stderr
// startLoop starts the dispatcher assignment loop
func (d *dispatcher) startLoop(q *queue) {
go func() {
for {
select {
case <-d.ch:
err := d.assignJobs(q)
if err != nil {
fmt.Fprintf(stdErr, "Cannot assign jobs: %v", err)
}
case <-d.quitCh: // loop was stopped
return
}
}
}()
}
// signalLoop signals to the dispatcher loop that an assignment check might need to run
func (d *dispatcher) signalLoop() {
go func() {
select {
case <-d.quitCh: // loop was stopped
return
case d.ch <- 1:
return
}
}()
}
// stopLoop stops the dispatcher assignment loop
func (d *dispatcher) stopLoop() {
close(d.quitCh)
}
// registerProcessor registers a new processor
func (d *dispatcher) registerProcessor(p Processor) int {
d.dispatchL.Lock()
defer d.dispatchL.Unlock()
pID := d.pStore.registerProcessor(p)
// signal that the processor is now available
d.signalLoop()
return pID
}
// unregisterProcessor unregisters a processor
// No more jobs will be assigned but if will not cancel a job that already started processing
func (d *dispatcher) unregisterProcessor(pID int) {
d.dispatchL.Lock()
defer d.dispatchL.Unlock()
d.pStore.unregisterProcessor(pID)
}
// assignJobs assigns pending jobs from the queue to free processors
func (d *dispatcher) assignJobs(q *queue) error {
d.dispatchL.Lock()
defer d.dispatchL.Unlock()
pIDs := d.pStore.getAvailableProcessorsIDs()
for _, pID := range pIDs {
err := d.assignJob(q, pID)
if err != nil {
return err
}
}
return nil
}
// assignJob assigns a pending job processor #pID and starts the run
// NOT THREAD SAFE !! only call from assignJobs
func (d *dispatcher) assignJob(q *queue, pID int) error {
p := d.pStore.getProcessor(pID)
if p == nil {
return fmt.Errorf("Processor %v not found", pID)
}
j, err := q.dequeueJob()
if err != nil {
return err
}
// no jobs to assign
if j == nil {
return nil
}
if d.pStore.isProcessorBusy(pID) {
return fmt.Errorf("Cannot assign job %v to Processor %v. Processor busy", j.ID, pID)
}
d.pStore.setProcessing(pID, j.ID)
go d.runJob(q, pID, p, j)
return nil
}
// unassignJob unmarks a job as assigned to #pID
func (d *dispatcher) unassignJob(pID int) {
d.dispatchL.Lock()
defer d.dispatchL.Unlock()
d.pStore.unsetProcessing(pID)
}
// runJob runs a job on the corresponding processor and moves it to the right queue depending on results
func (d *dispatcher) runJob(q *queue, pID int, p Processor, j *Job) {
defer d.processorDone(pID)
err := p.Run(j)
if err != nil {
fmt.Printf("Processor: %v. Job %v failed with err: %v\n", pID, j.ID, err)
err := q.markJobDone(j.ID, jobFailed)
if err != nil {
fmt.Printf("markJobDone -> %v jobFailed failed: %v\n", j.ID, err)
}
return
}
err = q.markJobDone(j.ID, jobComplete)
if err != nil {
fmt.Printf("markJobDone -> %v jobComplete failed: %v\n", j.ID, err)
}
}
func (d *dispatcher) processorDone(pID int) {
d.unassignJob(pID)
// signal that the processor might now be available
d.signalLoop()
}