-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathworker_pool.go
106 lines (94 loc) · 2.97 KB
/
worker_pool.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
package worker
import (
"context"
"sync"
"time"
"github.com/content-services/content-sources-backend/pkg/config"
m "github.com/content-services/content-sources-backend/pkg/instrumentation"
"github.com/content-services/content-sources-backend/pkg/models"
"github.com/content-services/content-sources-backend/pkg/tasks/queue"
"github.com/rs/zerolog/log"
)
type TaskHandler func(ctx context.Context, task *models.TaskInfo, queue *queue.Queue) error
type TaskWorkerPool interface {
// StartWorkers Starts workers up to number numWorkers defined in config.
// Should be run as a go routine.
StartWorkers(ctx context.Context)
// Stop Gracefully stops all workers
Stop()
// HeartbeatListener requeues tasks of workers whose heartbeats do not refresh within heartbeat duration
HeartbeatListener()
// RegisterHandler assigns a function of type TaskHandler to a typename.
// This function is the action performed to tasks of typename taskType.
RegisterHandler(taskType string, handler TaskHandler)
}
type WorkerPool struct {
queue queue.Queue
workerWg *sync.WaitGroup // wait for all workers to exit
handlers map[string]TaskHandler // associates a handler function to a typename
taskTypes []string // list of typenames
workers []*worker // list of workers
metrics *m.Metrics
}
func NewTaskWorkerPool(queue queue.Queue, metrics *m.Metrics) TaskWorkerPool {
workerWg := sync.WaitGroup{}
return &WorkerPool{
queue: queue,
workerWg: &workerWg,
handlers: make(map[string]TaskHandler),
metrics: metrics,
}
}
func (w *WorkerPool) HeartbeatListener() {
heartbeat := config.Get().Tasking.Heartbeat
go func() {
log.Logger.Info().Msg("starting task heartbeat listener")
for {
//nolint:staticcheck
for range time.Tick(heartbeat) {
for _, token := range w.queue.Heartbeats(heartbeat) {
id, isRunning, err := w.queue.IdFromToken(token)
if err != nil {
log.Logger.Warn().Err(err).Msg("error getting task id")
}
if isRunning {
err = w.queue.Requeue(id)
if err != nil {
log.Logger.Warn().Err(err).Msg("error requeuing task")
}
}
}
err := w.queue.RequeueFailedTasks(config.RequeueableTasks)
if err != nil {
log.Logger.Warn().Err(err).Msg("error requeuing failed tasks")
}
}
}
}()
}
func (w *WorkerPool) StartWorkers(ctx context.Context) {
for i := 0; i < config.Get().Tasking.WorkerCount; i++ {
wrk := newWorker(workerConfig{
queue: w.queue,
workerWg: w.workerWg,
handlers: w.handlers,
taskTypes: w.taskTypes,
}, w.metrics)
w.workers = append(w.workers, &wrk)
wrk.workerWg.Add(1)
go wrk.start(ctx)
}
}
func (w *WorkerPool) RegisterHandler(taskType string, handler TaskHandler) {
w.handlers[taskType] = handler
if !contains(w.taskTypes, taskType) {
w.taskTypes = append(w.taskTypes, taskType)
}
}
func (w *WorkerPool) Stop() {
log.Logger.Info().Msg("Stopping workers")
for _, wrk := range w.workers {
wrk.stop()
}
w.workerWg.Wait()
}