-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcookiemonster.go
212 lines (175 loc) · 4.61 KB
/
cookiemonster.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
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
package cookiemonster
import (
"errors"
"os"
"os/signal"
"plugin"
"sync"
"sync/atomic"
"time"
)
// Cookie represents a unit of work
type Cookie interface {
// work identifier
ID() string
// data needed to process the work
Content() interface{}
// optional map of metadata related to the work
Metadata() map[string]string
}
// Jar represents a work provider
type Jar interface {
// generate units of work to distribute amongst the various workers
Retrieve() ([]Cookie, error)
// mark the work as done (e.g., delete a message from a queue after it's been processed)
Retire(Cookie) error
}
// DigestFn represents the function that handles the work
type DigestFn func(Cookie) error
// Digester handles the work orchestration
type Digester struct {
workers int
workChan chan []Cookie
infoLogger Logger
errorLogger Logger
stopSignals []os.Signal
jar Jar
backoff Backoff
running atomic.Value
workersWG sync.WaitGroup
orchestratorWG sync.WaitGroup
mux sync.Mutex
}
// NewDigesterWithPlugin creates a new Digester with a Jar from a plugin
func NewDigesterWithPlugin(jarPath string, options ...DigesterOptionFunc) (*Digester, error) {
plug, err := plugin.Open(jarPath)
if err != nil {
return nil, err
}
sym, err := plug.Lookup("Jar")
if err != nil {
return nil, err
}
jar, ok := sym.(Jar)
if !ok {
return nil, errors.New("unexpected type from module symbol")
}
return NewDigester(jar, options...), nil
}
// NewDigesterWithPlugin creates a new Digester with a Jar
func NewDigester(jar Jar, options ...DigesterOptionFunc) *Digester {
d := &Digester{jar: jar}
d.running.Store(false)
for _, option := range options {
option(d)
}
d.infoF("handling defaults")
d.handleDefaults()
return d
}
// Start initiates the worker pool
func (d *Digester) Start(fn DigestFn) error {
d.mux.Lock()
defer d.mux.Unlock()
if d.isRunning() {
return errors.New("digester is already running")
}
d.infoF("starting digester")
d.running.Store(true)
d.startWorkers(fn)
d.startOrchestrator()
if len(d.stopSignals) > 0 {
d.infoF("waiting for OS signals...")
d.waitForSignals(d.stopSignals...)
d.Stop()
}
return nil
}
// Stop handles the graceful shutdown of the worker pool
func (d *Digester) Stop() {
d.infoF("stopping digester")
d.running.Store(false)
d.orchestratorWG.Wait()
close(d.workChan)
d.workersWG.Wait()
}
func (d *Digester) startWorkers(fn DigestFn) {
d.workersWG.Add(d.workers)
work := func(workerID int) {
defer d.infoF("worker %d: stopping", workerID)
defer d.workersWG.Done()
for cc := range d.workChan {
d.infoF("worker %d: handling %d messages", workerID, len(cc))
for _, c := range cc {
d.infoF("worker %d: digesting message %s", workerID, c.ID())
if err := fn(c); err != nil {
d.errorF("worker %d: could not digest message %s: %s", workerID, c.ID(), err)
continue
}
d.infoF("worker %d: retiring message %s", workerID, c.ID())
if err := d.jar.Retire(c); err != nil {
d.errorF("worker %d: could not retire message %s: %s", workerID, c.ID(), err)
continue
}
}
}
}
d.infoF("starting %d workers", d.workers)
for i := 0; i < d.workers; i++ {
workerID := i + 1
d.infoF("starting worker %d", workerID)
go work(workerID)
}
}
func (d *Digester) startOrchestrator() {
d.orchestratorWG.Add(1)
orchestrate := func() {
defer d.infoF("orchestrator: stopping")
defer d.orchestratorWG.Done()
for {
if !d.isRunning() {
break
}
currBackoff := d.backoff.Current()
d.infoF("orchestrator: sleeping for %s", currBackoff.String())
time.Sleep(currBackoff)
d.infoF("orchestrator: retrieving cookies from jar")
cc, err := d.jar.Retrieve()
if err != nil {
d.errorF("orchestrator: failed to retrieve from jar: %s", err)
continue
}
if len(cc) == 0 {
d.infoF("orchestrator: found an empty jar")
d.backoff.Next()
continue
}
d.backoff.Reset()
d.infoF("orchestrator: digesting %d cookies", len(cc))
d.workChan <- cc
}
}
d.infoF("starting orchestrator")
go orchestrate()
}
func (d *Digester) waitForSignals(signals ...os.Signal) {
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, signals...)
c := <-signalChan
d.infoF("signal %s triggered", c)
}
func (d *Digester) isRunning() bool {
return d.running.Load().(bool)
}
func (d *Digester) infoF(format string, args ...interface{}) {
if d.infoLogger == nil {
return
}
d.infoLogger.Printf("[INFO] "+format, args...)
}
func (d *Digester) errorF(format string, args ...interface{}) {
if d.errorLogger == nil {
return
}
d.errorLogger.Printf("[ERROR] "+format, args...)
}