-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathworker.go
531 lines (445 loc) · 17.6 KB
/
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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
// ** goworkerpool.com **********************************************************************************************
// ** github.com/enriquebris/goworkerpool **
// ** v0.10.0 *******************************************************************************************************
package goworkerpool
import (
"fmt"
"sync"
"time"
"github.com/enriquebris/goconcurrentcounter"
)
const (
ErrorWorkerNilTask = "worker.nil.task"
ErrorWorkerNoTaskFunc = "worker.no.func.to.process.task"
ListenInProgress = "listen.in.progress"
WorkerClosed = "worker.closed"
)
type (
// external action: func() (reEnqueueWorker bool)
// reEnqueueWorker bool ==> let the pool knows whether the worker should be re-enqueued in the availableWorkers queue
workerExternalActionFunc func() (reEnqueueWorker bool)
// internal action: function() (bool), return keepWorking
workerInternalActionFunc func() bool
// function to be executed by the worker to provide feedback about the action/task processing
workerFeedbackFunc func(wkrID int, reEnqueueWorker bool)
// external function to be executed by the worker
workerExternalGeneralFunc func()
// external function to be called by the worker if the handler fails
workerExternalFailFunc func(tsk *task, err error)
)
// workerFeedback holds the worker's feedback data
type workerFeedback struct {
workerID int
reEnqueueWorker bool
}
// workerFunctionData holds the worker's function result
type workerFunctionData struct {
result bool
err error
}
type worker struct {
id int
tasksChan chan *task
actionsChan chan action
// to let Listen knows that Close() was executed
closeChan chan chan struct{}
// to let know Listen is not longer listening
closeListenChan chan struct{}
// action:internal function
internalActions map[string]workerInternalActionFunc
// action: external function
externalActions map[string]workerExternalActionFunc
// external functions
// external function to provide feedback about the action/task processing
externalFeedbackFunc workerFeedbackFunc
// external function to be executed just before start processing an action
externalPreActionFunc workerExternalGeneralFunc
// external function to be executed after an action gets processed
externalPostActionFunc workerExternalGeneralFunc
// external function to be executed just before start processing a task
externalPreTaskFunc workerExternalGeneralFunc
// external function to be executed after a task gets processed
externalPostTaskFunc workerExternalGeneralFunc
// external function to be called by the worker if the handler fails
externalFailFunc workerExternalFailFunc
// error channel to send errors
errorChan chan *PoolError
// metrics about the tasks' executions
taskSuccesses int
externalTaskSuccesses goconcurrentcounter.Int
taskFailures int
// general information map (actions in progress, etc)
generalInfoMap sync.Map
}
// newWorker builds and returns a new worker
func newWorker(id int) *worker {
ret := &worker{}
ret.initialize(id)
return ret
}
func (st *worker) initialize(id int) {
st.id = id
st.tasksChan = make(chan *task)
st.actionsChan = make(chan action)
st.closeChan = make(chan chan struct{})
st.closeListenChan = make(chan struct{})
st.internalActions = make(map[string]workerInternalActionFunc)
st.externalActions = make(map[string]workerExternalActionFunc)
// closed
st.setStatus(WorkerClosed, false)
// setup the internal actions' functions
st.setupInternalFunctionOperations()
}
// GetID returns the ID
func (st *worker) GetID() int {
return st.id
}
// *************************************************************************************************************
// Channels ***************************************************************************************************
// *************************************************************************************************************
// GetTasksChannel returns tasks' channel
func (st *worker) GetTasksChannel() chan *task {
return st.tasksChan
}
// GetActionsChannel returns actions' channel
func (st *worker) GetActionsChannel() chan action {
return st.actionsChan
}
// Close finalizes the worker: releases all worker's resources.
// If Listen is running it will be exited with no ability to be executed again.
// It returns a channel to let know when Listen is closed.
func (st *worker) Close() chan struct{} {
// check whether Listen is in progress
waitingForListenDown := make(chan struct{})
select {
// send the chanel to receive a signal once Listen is being stopped :waitingForListenDown
case st.closeChan <- waitingForListenDown:
// wait until Listen is down
<-waitingForListenDown
default:
close(waitingForListenDown)
}
// close the channels
close(st.closeChan)
close(st.GetTasksChannel())
close(st.GetActionsChannel())
// flag the worker as "closed"
st.setStatus(WorkerClosed, true)
// channel to let know Listen is not longer listening
return st.closeListenChan
}
// *************************************************************************************************************
// Error Channel **********************************************************************************************
// *************************************************************************************************************
// SetErrorChannel sets the channel to send errors
func (st *worker) SetErrorChannel(ch chan *PoolError) {
st.errorChan = ch
}
// GetErrorChannel returns the error channel
func (st *worker) GetErrorChannel() chan *PoolError {
return st.errorChan
}
// *************************************************************************************************************
// Internal functions *****************************************************************************************
// *************************************************************************************************************
// setInternalFunctionForAction sets a internal function to be executed after an action
func (st *worker) setInternalFunctionForAction(actionCodes []string, fn workerInternalActionFunc) {
for _, actionCode := range actionCodes {
st.internalActions[actionCode] = fn
}
}
// *************************************************************************************************************
// External functions *****************************************************************************************
// *************************************************************************************************************
// SetExternalTaskSuccesses sets the external task successes counter
func (st *worker) SetExternalTaskSuccesses(external goconcurrentcounter.Int) {
st.externalTaskSuccesses = external
}
// GetExternalTaskSuccesses returns the external task successes counter
func (st *worker) GetExternalTaskSuccesses() goconcurrentcounter.Int {
return st.externalTaskSuccesses
}
// SetExternalFunctionForAction sets a external function to be executed after an action
func (st *worker) SetExternalFunctionForAction(actionCode string, fn workerExternalActionFunc) {
st.externalActions[actionCode] = fn
}
// SetExternalFeedbackFunction sets the external function to provide feedback about the action/task processing phase.
// The ExternalFeedbackFunction won't be executed after the worker receives the Close action.
func (st *worker) SetExternalFeedbackFunction(fn workerFeedbackFunc) {
st.externalFeedbackFunc = fn
}
// SetExternalPreActionFunc sets the external function to be executed just before start processing an action
func (st *worker) SetExternalPreActionFunc(fn workerExternalGeneralFunc) {
st.externalPreActionFunc = fn
}
// SetExternalPostActionFunc sets the external function to be executed after an action gets processed
func (st *worker) SetExternalPostActionFunc(fn workerExternalGeneralFunc) {
st.externalPostActionFunc = fn
}
// SetExternalPreTaskFunc sets the function to be executed just before start processing an action
func (st *worker) SetExternalPreTaskFunc(fn workerExternalGeneralFunc) {
st.externalPreTaskFunc = fn
}
// SetExternalPostTaskFunc sets the function to be executed after an action gets processed
func (st *worker) SetExternalPostTaskFunc(fn workerExternalGeneralFunc) {
st.externalPostTaskFunc = fn
}
// SetExternalFailFunc sets the external function to be executed once the handler fails
func (st *worker) SetExternalFailFunc(fn workerExternalFailFunc) {
st.externalFailFunc = fn
}
// GetExternalFailFunc returns the external function to be executed once the handler fails
func (st *worker) GetExternalFailFunc() workerExternalFailFunc {
return st.externalFailFunc
}
// *************************************************************************************************************
// Process ****************************************************************************************************
// *************************************************************************************************************
// ProcessTask processes a task
func (st *worker) ProcessTask(tsk *task) error {
// do not allow nil tasks
if tsk == nil {
return newPoolError(ErrorWorkerNilTask, "nil task was received by the worker")
}
if tsk.Code == taskRegular && tsk.Func == nil && tsk.CallbackFunc == nil {
return newPoolError(ErrorWorkerNoTaskFunc, "No function provided to process the task")
}
st.tasksChan <- tsk
return nil
}
// ProcessAction processes an action
func (st *worker) ProcessAction(actn action) {
st.actionsChan <- actn
}
// *************************************************************************************************************
// Listen *****************************************************************************************************
// *************************************************************************************************************
// Listen listens to actions/tasks and execute them once received.
//
// What kind of information could be processed by a worker?
// - actions (priority: 1). Actions get processed before enqueued tasks.
// - tasks (priority: 2).
// - late actions (priority: 2). Late actions come in the same queue as tasks, that way it allows to execute actions
// just after tasks were processed.
func (st *worker) Listen() {
if !st.getStatus(ListenInProgress) {
go func() {
var (
keepWorking = true
reEnqueueWorker = true
isAction = false
isTask = false
executeExternalFeedbackFunc = true
)
// Listen in progress
st.setStatus(ListenInProgress, true)
for keepWorking {
isAction = false
isTask = false
executeExternalFeedbackFunc = true
select {
case closeChan := <-st.closeChan:
// break the loop
keepWorking = false
// do not execute the external feedback function
executeExternalFeedbackFunc = false
// sends back the signal saying: "the worker is being closed, Listen is no longer up"
defer func() { closeChan <- struct{}{} }()
case actn := <-st.actionsChan:
isAction = true
// external function preAction
if st.externalPreActionFunc != nil {
st.externalPreActionFunc()
}
// internal function
if internalFunc, ok := st.internalActions[actn.Code]; ok {
keepWorking = internalFunc()
}
// external function
if externalFunc, ok := st.externalActions[actn.Code]; ok {
reEnqueueWorker = externalFunc()
}
case tsk := <-st.tasksChan:
// check whether st.Close was called and st.taskChan is being closed (break if true ...)
if tsk == nil {
keepWorking = false
break
}
isTask = true
// regular task
switch tsk.Code {
// regular task
case taskRegular:
// external function preTask
if st.externalPreTaskFunc != nil {
st.externalPreTaskFunc()
}
var success bool
// execute the task's function (if any)
if tsk.Func != nil {
//success = tsk.Func(tsk.Data)
resultChan := make(chan workerFunctionData, 2)
// execute data handler
st.executeFunction(tsk.Func, tsk.Data, resultChan, 0)
result := <-resultChan
if result.err == nil {
// no handler issues
success = result.result
} else {
// handler execution failed !
// report worker's function error
if st.GetExternalFailFunc() != nil {
st.GetExternalFailFunc()(tsk, result.err)
}
// handler's failure acts as a false
success = false
}
// adjust metrics based on task's function execution
// update the worker's metrics
if success {
st.taskSuccesses++
// update the external metrics
if st.externalTaskSuccesses != nil {
st.externalTaskSuccesses.Update(1)
}
} else {
st.taskFailures++
// TODO ::: update the external metrics
}
}
// task's callback
if tsk.CallbackFunc != nil {
// TODO ::: catch any panics here
tsk.CallbackFunc(tsk.Data)
}
// late action
case taskLateAction:
// get the action
actn, _ := tsk.Data.(action)
isAction = true
// late actions come through the tasks channel to keep the order, but they are actions (no tasks)
isTask = false
// execute all functions related to actions
// external function preAction
if st.externalPreActionFunc != nil {
st.externalPreActionFunc()
}
// internal function
if internalFunc, ok := st.internalActions[actn.Code]; ok {
keepWorking = internalFunc()
}
// external function
if externalFunc, ok := st.externalActions[actn.Code]; ok {
reEnqueueWorker = externalFunc()
}
}
}
// keepWorker overwrites reEnqueueWorker whether keepWorker == false
if !keepWorking {
reEnqueueWorker = false
}
// let the pool's dispatcher knows whether this worker could be re-enqueued (based on reEnqueueWorker)
if executeExternalFeedbackFunc && st.externalFeedbackFunc != nil {
// call the external feedback function
st.externalFeedbackFunc(st.GetID(), reEnqueueWorker)
}
// external post action/task function
// action
if isAction && st.externalPostActionFunc != nil {
st.externalPostActionFunc()
continue
}
// task
if isTask && st.externalPostTaskFunc != nil {
st.externalPostTaskFunc()
continue
}
}
// Listen is done, not "in progress" anymore
st.setStatus(ListenInProgress, false)
// let know Listen is not longer listening
select {
case st.closeListenChan <- struct{}{}:
default:
// do nothing, st.closeListenChan is closed
}
}()
}
}
// executeFunction executes the user function, sends the return over resultChan and controls the time execution (optional)
func (st *worker) executeFunction(fn PoolFunc, data interface{}, resultChan chan workerFunctionData, maxDuration time.Duration) {
defer func() {
if r := recover(); r != nil {
resultChan <- workerFunctionData{
result: false,
err: newPoolError(ErrorWorkerFuncPanic, fmt.Sprintf("%v", r)),
}
}
}()
// no max execution time
if maxDuration == 0 {
resultChan <- workerFunctionData{
result: fn(data),
err: nil,
}
} else {
doneChan := make(chan bool)
go func(fn PoolFunc, data interface{}, doneChan chan bool) {
// TODO ::: defer && recover
doneChan <- fn(data)
}(fn, data, doneChan)
select {
case result := <-doneChan:
resultChan <- workerFunctionData{
result: result,
err: nil,
}
case <-time.After(maxDuration):
resultChan <- workerFunctionData{
result: false,
err: newPoolError(ErrorWorkerMaxTimeExceeded, "Function exceeded max execution time"),
}
}
}
}
// *************************************************************************************************************
// ** Metrics *************************************************************************************************
// *************************************************************************************************************
// GetTaskSuccesses returns how many tasks has been successfully processed
func (st *worker) GetTaskSuccesses() int {
return st.taskSuccesses
}
// GetTaskFailures returns how many tasks has not been successfully processed
func (st *worker) GetTaskFailures() int {
return st.taskFailures
}
// *************************************************************************************************************
// ** Statuses. Operation in progress *************************************************************************
// *************************************************************************************************************
// setStatus sets the status for a given operation
func (st *worker) setStatus(operationName string, status bool) {
st.generalInfoMap.Store(operationName, status)
}
// getStatus returns the status for a given operation
func (st *worker) getStatus(operationName string) bool {
rawValue, ok := st.generalInfoMap.Load(operationName)
if !ok {
return false
}
value := rawValue.(bool)
return value
}
// Closed returns true if the worker was closed using Close
func (st *worker) Closed() bool {
return st.getStatus(WorkerClosed)
}
// *************************************************************************************************************
// ** Internal action operations ******************************************************************************
// *************************************************************************************************************
func (st *worker) setupInternalFunctionOperations() {
// KillWorker
st.setInternalFunctionForAction([]string{actionKillWorker, actionLateKillWorker}, func() bool {
// return false would break the Listen loop, so the worker will die
return false
})
}