-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathworker.ts
More file actions
414 lines (369 loc) · 11.9 KB
/
worker.ts
File metadata and controls
414 lines (369 loc) · 11.9 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
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
import { SpanKind, trace, Tracer } from "@opentelemetry/api";
import { Logger } from "@trigger.dev/core/logger";
import { calculateNextRetryDelay } from "@trigger.dev/core/v3";
import { type RetryOptions } from "@trigger.dev/core/v3/schemas";
import { type RedisOptions } from "ioredis";
import { z } from "zod";
import { AnyQueueItem, SimpleQueue } from "./queue.js";
import Redis from "ioredis";
import { nanoid } from "nanoid";
import { startSpan } from "./telemetry.js";
import pLimit from "p-limit";
import { createRedisClient } from "@internal/redis";
export type WorkerCatalog = {
[key: string]: {
schema: z.ZodFirstPartySchemaTypes | z.ZodDiscriminatedUnion<any, any>;
visibilityTimeoutMs: number;
retry?: RetryOptions;
};
};
type QueueCatalogFromWorkerCatalog<Catalog extends WorkerCatalog> = {
[K in keyof Catalog]: Catalog[K]["schema"];
};
type JobHandler<Catalog extends WorkerCatalog, K extends keyof Catalog> = (params: {
id: string;
payload: z.infer<Catalog[K]["schema"]>;
visibilityTimeoutMs: number;
attempt: number;
}) => Promise<void>;
export type WorkerConcurrencyOptions = {
workers?: number;
tasksPerWorker?: number;
limit?: number;
};
type WorkerOptions<TCatalog extends WorkerCatalog> = {
name: string;
redisOptions: RedisOptions;
catalog: TCatalog;
jobs: {
[K in keyof TCatalog]: JobHandler<TCatalog, K>;
};
concurrency?: WorkerConcurrencyOptions;
pollIntervalMs?: number;
immediatePollIntervalMs?: number;
logger?: Logger;
tracer?: Tracer;
};
// This results in attempt 12 being a delay of 1 hour
const defaultRetrySettings = {
maxAttempts: 12,
factor: 2,
//one second
minTimeoutInMs: 1_000,
//one hour
maxTimeoutInMs: 3_600_000,
randomize: true,
};
class Worker<TCatalog extends WorkerCatalog> {
private subscriber: Redis | undefined;
private tracer: Tracer;
queue: SimpleQueue<QueueCatalogFromWorkerCatalog<TCatalog>>;
private jobs: WorkerOptions<TCatalog>["jobs"];
private logger: Logger;
private workerLoops: Promise<void>[] = [];
private isShuttingDown = false;
private concurrency: Required<NonNullable<WorkerOptions<TCatalog>["concurrency"]>>;
// The p-limit limiter to control overall concurrency.
private limiter: ReturnType<typeof pLimit>;
constructor(private options: WorkerOptions<TCatalog>) {
this.logger = options.logger ?? new Logger("Worker", "debug");
this.tracer = options.tracer ?? trace.getTracer(options.name);
const schema: QueueCatalogFromWorkerCatalog<TCatalog> = Object.fromEntries(
Object.entries(this.options.catalog).map(([key, value]) => [key, value.schema])
) as QueueCatalogFromWorkerCatalog<TCatalog>;
this.queue = new SimpleQueue({
name: options.name,
redisOptions: options.redisOptions,
logger: this.logger,
schema,
});
this.jobs = options.jobs;
const { workers = 1, tasksPerWorker = 1, limit = 10 } = options.concurrency ?? {};
this.concurrency = { workers, tasksPerWorker, limit };
// Create a p-limit instance using this limit.
this.limiter = pLimit(this.concurrency.limit);
}
public start() {
const { workers, tasksPerWorker } = this.concurrency;
// Launch a number of "worker loops" on the main thread.
for (let i = 0; i < workers; i++) {
this.workerLoops.push(this.runWorkerLoop(`worker-${nanoid(12)}`, tasksPerWorker));
}
this.setupShutdownHandlers();
this.subscriber = createRedisClient(this.options.redisOptions, {
onError: (error) => {
this.logger.error(`RedisWorker subscriber redis client error:`, {
error,
keyPrefix: this.options.redisOptions.keyPrefix,
});
},
});
this.setupSubscriber();
return this;
}
/**
* Enqueues a job for processing.
* @param options - The enqueue options.
* @param options.id - Optional unique identifier for the job. If not provided, one will be generated. It prevents duplication.
* @param options.job - The job type from the worker catalog.
* @param options.payload - The job payload that matches the schema defined in the catalog.
* @param options.visibilityTimeoutMs - Optional visibility timeout in milliseconds. Defaults to value from catalog.
* @param options.availableAt - Optional date when the job should become available for processing. Defaults to now.
* @returns A promise that resolves when the job is enqueued.
*/
enqueue<K extends keyof TCatalog>({
id,
job,
payload,
visibilityTimeoutMs,
availableAt,
}: {
id?: string;
job: K;
payload: z.infer<TCatalog[K]["schema"]>;
visibilityTimeoutMs?: number;
availableAt?: Date;
}) {
return startSpan(
this.tracer,
"enqueue",
async (span) => {
const timeout = visibilityTimeoutMs ?? this.options.catalog[job].visibilityTimeoutMs;
span.setAttribute("job_visibility_timeout_ms", timeout);
return this.queue.enqueue({
id,
job,
item: payload,
visibilityTimeoutMs: timeout,
availableAt,
});
},
{
kind: SpanKind.PRODUCER,
attributes: {
job_type: job as string,
job_id: id,
},
}
);
}
/**
* Reschedules an existing job to a new available date.
* If the job isn't in the queue, it will be ignored.
*/
reschedule(id: string, availableAt: Date) {
return startSpan(
this.tracer,
"reschedule",
async (span) => {
return this.queue.reschedule(id, availableAt);
},
{
kind: SpanKind.PRODUCER,
attributes: {
job_id: id,
},
}
);
}
ack(id: string) {
return startSpan(
this.tracer,
"ack",
() => {
return this.queue.ack(id);
},
{
attributes: {
job_id: id,
},
}
);
}
/**
* The main loop that each worker runs. It repeatedly polls for items,
* processes them, and then waits before the next iteration.
*/
private async runWorkerLoop(workerId: string, taskCount: number): Promise<void> {
const pollIntervalMs = this.options.pollIntervalMs ?? 1000;
const immediatePollIntervalMs = this.options.immediatePollIntervalMs ?? 100;
while (!this.isShuttingDown) {
// Check overall load. If at capacity, wait a bit before trying to dequeue more.
if (this.limiter.activeCount + this.limiter.pendingCount >= this.concurrency.limit) {
await Worker.delay(pollIntervalMs);
continue;
}
try {
const items = await this.queue.dequeue(taskCount);
if (items.length === 0) {
await Worker.delay(pollIntervalMs);
continue;
}
// Schedule each item using the limiter.
for (const item of items) {
this.limiter(() => this.processItem(item as AnyQueueItem, items.length, workerId)).catch(
(err) => {
this.logger.error("Unhandled error in processItem:", { error: err, workerId, item });
}
);
}
} catch (error) {
this.logger.error("Error dequeuing items:", { name: this.options.name, error });
await Worker.delay(pollIntervalMs);
continue;
}
// Wait briefly before immediately polling again since we processed items
await Worker.delay(immediatePollIntervalMs);
}
}
/**
* Processes a single item.
*/
private async processItem(
{ id, job, item, visibilityTimeoutMs, attempt, timestamp }: AnyQueueItem,
batchSize: number,
workerId: string
): Promise<void> {
const catalogItem = this.options.catalog[job as any];
const handler = this.jobs[job as any];
if (!handler) {
this.logger.error(`No handler found for job type: ${job}`);
return;
}
await startSpan(
this.tracer,
"processItem",
async () => {
await handler({ id, payload: item, visibilityTimeoutMs, attempt });
// On success, acknowledge the item.
await this.queue.ack(id);
},
{
kind: SpanKind.CONSUMER,
attributes: {
job_id: id,
job_type: job,
attempt,
job_timestamp: timestamp.getTime(),
job_age_in_ms: Date.now() - timestamp.getTime(),
worker_id: workerId,
worker_limit_concurrency: this.limiter.concurrency,
worker_limit_active: this.limiter.activeCount,
worker_limit_pending: this.limiter.pendingCount,
worker_name: this.options.name,
batch_size: batchSize,
},
}
).catch(async (error) => {
const errorMessage = error instanceof Error ? error.message : String(error);
this.logger.error(`Error processing item:`, {
name: this.options.name,
id,
job,
item,
visibilityTimeoutMs,
error,
errorMessage,
});
// Attempt requeue logic.
try {
const newAttempt = attempt + 1;
const retrySettings = {
...defaultRetrySettings,
...catalogItem.retry,
};
const retryDelay = calculateNextRetryDelay(retrySettings, newAttempt);
if (!retryDelay) {
this.logger.error(`Item ${id} reached max attempts. Moving to DLQ.`, {
name: this.options.name,
id,
job,
item,
visibilityTimeoutMs,
attempt: newAttempt,
errorMessage,
});
await this.queue.moveToDeadLetterQueue(id, errorMessage);
return;
}
const retryDate = new Date(Date.now() + retryDelay);
this.logger.info(`Requeuing failed item ${id} with delay`, {
name: this.options.name,
id,
job,
item,
retryDate,
retryDelay,
visibilityTimeoutMs,
attempt: newAttempt,
});
await this.queue.enqueue({
id,
job,
item,
availableAt: retryDate,
attempt: newAttempt,
visibilityTimeoutMs,
});
} catch (requeueError) {
this.logger.error(
`Failed to requeue item ${id}. It will be retried after the visibility timeout.`,
{
name: this.options.name,
id,
job,
item,
visibilityTimeoutMs,
error: requeueError,
}
);
}
});
}
// A simple helper to delay for a given number of milliseconds.
private static delay(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}
private setupSubscriber() {
const channel = `${this.options.name}:redrive`;
this.subscriber?.subscribe(channel, (err) => {
if (err) {
this.logger.error(`Failed to subscribe to ${channel}`, { error: err });
} else {
this.logger.log(`Subscribed to ${channel}`);
}
});
this.subscriber?.on("message", this.handleRedriveMessage.bind(this));
}
private async handleRedriveMessage(channel: string, message: string) {
try {
const { id } = JSON.parse(message) as any;
if (typeof id !== "string") {
throw new Error("Invalid message format: id must be a string");
}
await this.queue.redriveFromDeadLetterQueue(id);
this.logger.log(`Redrived item ${id} from Dead Letter Queue`);
} catch (error) {
this.logger.error("Error processing redrive message", { error, message });
}
}
private setupShutdownHandlers() {
process.on("SIGTERM", this.shutdown.bind(this));
process.on("SIGINT", this.shutdown.bind(this));
}
private async shutdown() {
if (this.isShuttingDown) return;
this.isShuttingDown = true;
this.logger.log("Shutting down worker loops...");
// Wait for all worker loops to finish.
await Promise.all(this.workerLoops);
await this.subscriber?.unsubscribe();
await this.subscriber?.quit();
await this.queue.close();
this.logger.log("All workers and subscribers shut down.");
}
public async stop() {
await this.shutdown();
}
}
export { Worker };