Repository navigation
Expand file tree
/
Copy pathworker.go
More file actions
181 lines (155 loc) · 4.91 KB
/
Copy pathworker.go
File metadata and controls
181 lines (155 loc) · 4.91 KB
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
package gopherqueue
import (
"context"
"errors"
"fmt"
"log"
"sync"
"time"
)
// JobHandler represents a handler function signature for processing a job.
type JobHandler func(ctx context.Context, job *Job) error
// JobExecutor defines an interface for executing a job.
type JobExecutor interface {
Execute(ctx context.Context, job *Job) error
}
// HandlerRegistry maintains a thread-safe mapping of job types to JobHandlers.
type HandlerRegistry struct {
mu sync.RWMutex
handlers map[string]JobHandler
}
// NewHandlerRegistry initializes a new HandlerRegistry instance.
func NewHandlerRegistry() *HandlerRegistry {
return &HandlerRegistry{
handlers: make(map[string]JobHandler),
}
}
// Register registers a handler function for a given job type.
func (r *HandlerRegistry) Register(jobType string, handler JobHandler) {
r.mu.Lock()
defer r.mu.Unlock()
r.handlers[jobType] = handler
}
// Execute looks up and invokes the registered handler for the job type.
func (r *HandlerRegistry) Execute(ctx context.Context, job *Job) error {
r.mu.RLock()
handler, exist := r.handlers[job.Type]
r.mu.RUnlock()
if !exist {
return fmt.Errorf("gopherqueue: no handler registered for job type '%s'", job.Type)
}
return handler(ctx, job)
}
// WorkerPool manages worker goroutines that fetch and process jobs concurrently.
type WorkerPool struct {
queueName string
storage Storage
executor JobExecutor
numWorkers int
pollInterval time.Duration
wg sync.WaitGroup
quit chan struct{}
stopOnce sync.Once
}
// NewWorkerPool initializes a new WorkerPool instance.
func NewWorkerPool(queueName string, numWorkers int, pollInterval time.Duration, store Storage, exec JobExecutor) *WorkerPool {
if numWorkers <= 0 {
numWorkers = 5
}
if queueName == "" {
queueName = "default"
}
if pollInterval <= 0 {
pollInterval = 2 * time.Second
}
return &WorkerPool{
queueName: queueName,
storage: store,
executor: exec,
numWorkers: numWorkers,
pollInterval: pollInterval,
quit: make(chan struct{}),
}
}
// Start launches worker goroutines.
func (wp *WorkerPool) Start(ctx context.Context) {
log.Printf("[WorkerPool] Starting %d workers for queue '%s'...", wp.numWorkers, wp.queueName)
for i := 1; i <= wp.numWorkers; i++ {
wp.wg.Add(1)
go wp.worker(ctx, i)
}
}
func (wp *WorkerPool) worker(ctx context.Context, workerID int) {
defer wp.wg.Done()
log.Printf("[Worker %d] Ready and listening for jobs on queue '%s'...", workerID, wp.queueName)
for {
select {
case <-wp.quit:
log.Printf("[Worker %d] Stopping...", workerID)
return
case <-ctx.Done():
log.Printf("[Worker %d] Context cancelled. Stopping...", workerID)
return
default:
job, err := wp.storage.Fetch(ctx, wp.queueName, wp.pollInterval)
if err != nil {
if errors.Is(err, ErrQueueEmpty) {
continue
}
if errors.Is(err, context.Canceled) {
return
}
log.Printf("[Worker %d] Fetch error: %v", workerID, err)
time.Sleep(1 * time.Second)
continue
}
wp.processJob(ctx, workerID, job)
}
}
}
func (wp *WorkerPool) processJob(parentCtx context.Context, workerID int, job *Job) {
timeout := time.Duration(job.TimeoutSeconds) * time.Second
if timeout <= 0 {
timeout = 30 * time.Second
}
jobCtx, cancel := context.WithTimeout(parentCtx, timeout)
defer cancel()
log.Printf("[Worker %d] Processing Job ID: %s (Type: %s)", workerID, job.ID, job.Type)
var execErr error
defer func() {
if r := recover(); r != nil {
execErr = fmt.Errorf("panic recovered during execution: %v", r)
log.Printf("[Worker %d] CRITICAL: Job %s panicked: %v", workerID, job.ID, r)
}
ackCtx, ackCancel := context.WithTimeout(context.Background(), 5*time.Second)
defer ackCancel()
if execErr == nil {
if ackErr := wp.storage.Ack(ackCtx, job); ackErr != nil {
log.Printf("[Worker %d] Failed to ACK Job %s: %v", workerID, job.ID, ackErr)
} else {
log.Printf("[Worker %d] Successfully COMPLETED Job ID: %s", workerID, job.ID)
}
} else if rateErr, ok := IsRateLimited(execErr); ok {
if reschedErr := wp.storage.Reschedule(ackCtx, job, rateErr.RetryAfter); reschedErr != nil {
log.Printf("[Worker %d] Failed to Reschedule Rate Limited Job %s: %v", workerID, job.ID, reschedErr)
} else {
log.Printf("[Worker %d] Job ID: %s RATE_LIMITED (Key: %s, Deferred for %v)", workerID, job.ID, rateErr.Key, rateErr.RetryAfter)
}
} else {
if nackErr := wp.storage.Nack(ackCtx, job, execErr); nackErr != nil {
log.Printf("[Worker %d] Failed to NACK Job %s: %v", workerID, job.ID, nackErr)
} else {
log.Printf("[Worker %d] Job ID: %s FAILED/RETRYING (Error: %v)", workerID, job.ID, execErr)
}
}
}()
execErr = wp.executor.Execute(jobCtx, job)
}
// Stop signals all workers to stop and waits for them to finish.
func (wp *WorkerPool) Stop() {
wp.stopOnce.Do(func() {
close(wp.quit)
})
wp.wg.Wait()
log.Println("[WorkerPool] All workers stopped cleanly.")
}