-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess.go
134 lines (121 loc) · 3.29 KB
/
process.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
package errands
import (
"fmt"
time "time"
schemas "github.com/polygon-io/errands-server/schemas"
)
// Processor is the main struct which handles all the processing for a client
type Processor struct {
Parent *ErrandsAPI
Topic string
Concurrency int
Paused bool
Quit chan int
ErrandQueue chan *schemas.Errand
Fn func(*schemas.Errand) (map[string]interface{}, error)
Procs []*ProcThread
}
// NewProcessor creates and returns a *Processor with the params sent.
func (e *ErrandsAPI) NewProcessor(
topic string, concurrency int,
fn func(*schemas.Errand) (map[string]interface{}, error)) (*Processor, error) {
// Create the processor:
obj := &Processor{
Parent: e,
Topic: topic,
Concurrency: concurrency,
Fn: fn,
Paused: false,
Quit: make(chan (int)),
ErrandQueue: make(chan (*schemas.Errand)),
}
// Add it to this APIs Processor list:
e.Processors = append(e.Processors, obj)
// Actually run the processor:
go obj.Run()
return obj, nil
}
// Pause pauses the processor. This will not pause the current threads, it will
// simply stop the processor from processing subsequent items.
func (p *Processor) Pause() {
p.Paused = true
}
// Resume tells the processor that it should start processing items again.
func (p *Processor) Resume() {
p.Paused = false
}
func (p *Processor) requestErrandToProcess() {
errandRes, err := p.Parent.RequestErrandToProcess(p.Topic)
if err != nil {
fmt.Println("Error requesting errand to process:", err)
return
}
if errandRes.Results.ID != "" {
p.ErrandQueue <- &errandRes.Results
}
}
// Run creates the threads, and starts the loop to query for jobs to run.
func (p *Processor) Run() {
ticker := time.NewTicker(4 * time.Second)
// Create the actually processor threads:
for i := 1; i <= p.Concurrency; i++ {
obj := p.NewProcThread()
p.Procs = append(p.Procs, obj)
go obj.RunThread()
}
// Every so often, if proc threads are awaiting jobs, request them:
for {
select {
case <-ticker.C:
// We have room for another item:
if p.procsAwaitingErrands() && len(p.ErrandQueue) == 0 && !p.Paused {
p.requestErrandToProcess()
}
case <-p.Quit:
ticker.Stop()
return
}
}
}
func (p *Processor) procsAwaitingErrands() bool {
for _, proc := range p.Procs {
if proc.AwaitingErrand {
return true
}
}
return false
}
// ProcThread is created per concurrency. So each actual item processed
// will be inside of a ProcThread.
type ProcThread struct {
Processor *Processor
AwaitingErrand bool
}
// NewProcThread creates and returns a *ProcThread
func (p *Processor) NewProcThread() *ProcThread {
obj := &ProcThread{
AwaitingErrand: true,
Processor: p,
}
return obj
}
// RunThread runs the actual processor function on items:
func (proc *ProcThread) RunThread() {
for {
select {
case job := <-proc.Processor.ErrandQueue:
proc.AwaitingErrand = false
fmt.Println("Start processing:", job.ID)
// Actually Processing the job:
res, err := proc.Processor.Fn(job)
if err != nil {
fmt.Println("Error processing:", job.ID, "Err:", err)
proc.Processor.Parent.FailErrand(job.ID, err.Error())
} else {
fmt.Println("Completed processing:", job.ID)
proc.Processor.Parent.CompleteErrand(job.ID, res)
}
proc.AwaitingErrand = true
}
}
}