-
Notifications
You must be signed in to change notification settings - Fork 9
/
worker-http-controller.ts
321 lines (272 loc) · 10.5 KB
/
worker-http-controller.ts
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
import { createHash } from "crypto";
import { Job, Worker } from "bullmq";
import { Redis, Cluster } from "ioredis";
import { debug, info } from "../../utils/log";
import { HttpHandlerOpts, WorkerMetadata } from "../../interfaces";
import { validateWorkerMetadata } from "../../validators";
import { config } from "../../config";
const debugEnabled = config.debugEnabled;
const workers: { [queueName: string]: Worker } = {};
const metadatasShas: { [queueName: string]: string } = {};
const workerMetadataKey = config.workerMetadataKey;
const workerMetadataStream = config.workerMetadataStream;
const workerStreamBlockingTime = 5000;
const abortController = new AbortController();
export const gracefulShutdownWorkers = async () => {
info(`Closing workers...`);
abortController.abort();
const closingWorkers = Object.keys(workers).map(async (queueName) => workers[queueName].close());
await Promise.all(closingWorkers);
info('Workers closed');
}
const workerFromMetadata = (queueName: string, workerMetadata: WorkerMetadata, connection: Redis | Cluster): Worker => {
const { endpoint: workerEndpoint, opts: workerOptions } = workerMetadata;
debugEnabled && debug(`Starting worker for queue ${queueName} with endpoint ${workerMetadata.endpoint.url} and options ${JSON.stringify(workerMetadata.opts) || 'default'}`);
const worker = new Worker(queueName, async (job: Job, token?: string) => {
debugEnabled && debug(`Processing job ${job.id} from queue ${queueName} with endpoint ${workerEndpoint.url}`);
// Process job by calling an external service using the worker endpoint
const controller = new AbortController()
const timeoutId = setTimeout(() => {
debugEnabled && ("Timeout, aborting...")
controller.abort()
}, workerEndpoint.timeout || 3000)
try {
const response = await fetch(workerEndpoint.url, {
method: workerEndpoint.method,
headers: workerEndpoint.headers,
body: JSON.stringify({ job: job.toJSON(), token }),
signal: controller.signal
});
if (!response.ok) {
throw new Error(response.statusText);
}
debugEnabled && debug(`Job "${job.id}:${job.name}" processed successfully`);
// If response is supposed to be json lets parse it
if (response.headers.get('content-type')?.includes('application/json')) {
return response.json();
} else {
return response.text();
}
} catch (err) {
debugEnabled && debug(`Failed to process job ${job.id}: ${(<Error>err).message}`);
throw err;
} finally {
clearTimeout(timeoutId);
}
}, { ...workerOptions, connection, prefix: config.defaultQueuePrefix });
if (debugEnabled) {
worker.on('failed', (job, err) => {
debug(`Job ${job!.id} failed: ${err}`);
});
}
return worker;
};
let lastEventId: string | undefined;
export const workerStreamListener = async (redisClient: Redis | Cluster, abortSignal: AbortSignal) => {
const streamBlockingClient = redisClient.duplicate();
let running = true;
abortSignal.addEventListener('abort', () => {
running = false;
streamBlockingClient.disconnect();
});
while (running) {
const streams = await streamBlockingClient.xread('BLOCK', workerStreamBlockingTime, 'STREAMS', workerMetadataStream, lastEventId || '0');
// If we got no events, continue to the next iteration
if (!streams || streams.length === 0) {
continue;
}
const stream = streams[0];
debugEnabled && debug(`Received ${streams.length} event${streams.length > 1 ? "s" : ""} from stream ${workerMetadataStream}`);
const [_streamName, events] = stream;
for (const [eventId, fields] of events) {
lastEventId = eventId;
const queueName = fields[1];
const existingWorker = workers[queueName];
const existingSha = metadatasShas[queueName];
const workerMetadataRaw = await redisClient.hget(workerMetadataKey, queueName);
// If workerMetadatadaVersion is older than the event id, we need to update the worker
if (workerMetadataRaw) {
const workerMetadataSha256 = createHash('sha256').update(workerMetadataRaw).digest('hex');
if ((existingSha !== workerMetadataSha256)) {
const workerMetadata = JSON.parse(workerMetadataRaw);
workers[queueName] = workerFromMetadata(queueName, workerMetadata, redisClient);
metadatasShas[queueName] = workerMetadataSha256;
if (existingWorker) {
await existingWorker.close();
}
}
} else {
// worker has been removed
debugEnabled && debug(`Worker for queue ${queueName} has been removed`);
if (existingWorker) {
await existingWorker.close();
delete workers[queueName];
delete metadatasShas[queueName];
}
}
}
}
}
export const WorkerHttpController = {
loadScripts: async (redisClient: Redis | Cluster) => {
const luaScripts = {
updateWorkerMetadata: `
local workerMetadataKey = KEYS[1]
local workerMetadataStream = KEYS[2]
local queueName = ARGV[1]
local workerMetadata = ARGV[2]
local streamMaxLen = ARGV[3]
redis.call('HSET', workerMetadataKey, queueName, workerMetadata)
local eventId = redis.call('XADD', workerMetadataStream, 'MAXLEN', streamMaxLen, '*', 'worker', queueName)
return eventId
`,
removeWorkerMetadata: `
local workerMetadataKey = KEYS[1]
local workerMetadataStream = KEYS[2]
local queueName = ARGV[1]
local streamMaxLen = ARGV[2]
local removedWorker = redis.call('HDEL', workerMetadataKey, queueName)
if removedWorker == 1 then
local eventId = redis.call('XADD', workerMetadataStream, 'MAXLEN', streamMaxLen, '*', 'worker', queueName)
return { removedWorker, eventId }
end
`
}
for (const [scriptName, script] of Object.entries(luaScripts)) {
redisClient.defineCommand(scriptName, { numberOfKeys: 2, lua: script });
}
},
/**
* Load workers from Redis and start them.
*
* @param redisClient
* @param workersRedisClient
*/
loadWorkers: async (redisClient: Redis | Cluster, workersRedisClient: Redis | Cluster) => {
// Load workers from Redis and start them
debugEnabled && debug('Loading workers from Redis...');
const result = await redisClient.xrevrange(config.workerMetadataStream, '+', '-', 'COUNT', 1);
if (result.length > 0) {
[[lastEventId]] = result
}
const stream = redisClient.hscanStream(workerMetadataKey, { count: 10 });
stream.on('data', (result: string[]) => {
for (let i = 0; i < result.length; i += 2) {
const queueName = result[i];
const value = result[i + 1];
const workerMetadata = JSON.parse(value) as WorkerMetadata;
workers[queueName] = workerFromMetadata(queueName, workerMetadata, workersRedisClient);
metadatasShas[queueName] = createHash('sha256').update(value).digest('hex');
}
});
await new Promise<void>((resolve, reject) => {
stream.on('end', () => {
debugEnabled && debug('Workers loaded');
resolve();
});
stream.on('error', (err) => {
debugEnabled && debug(`Failed to load workers: ${err}`);
reject(err);
});
});
},
init: async (redisClient: Redis | Cluster, workersRedisClient: Redis | Cluster) => {
await WorkerHttpController.loadScripts(redisClient);
await WorkerHttpController.loadWorkers(redisClient, workersRedisClient);
workerStreamListener(workersRedisClient, abortController.signal);
},
/**
* Add a new worker to the system. A worker is a BullMQ worker that processes
* jobs by calling an external service.
*
* The worker metadata is stored in Redis so that the workers are re-instantiated
* after a restart.
*
* Only one worker per queue is allowed, so calling this method with the same
* queue name will replace the existing worker.
*
* @param opts
*
* @returns
*/
addWorker: async (opts: HttpHandlerOpts) => {
const workerMetadata = await opts.req.json() as WorkerMetadata;
try {
validateWorkerMetadata(workerMetadata);
} catch (err) {
return new Response((<Error>err).message, { status: 400 });
}
const { queue: queueName } = workerMetadata;
const { redisClient } = opts;
// Upsert worker metadata and notify all listeners about the change.
try {
const eventId = await (<any>redisClient)['updateWorkerMetadata'](
workerMetadataKey,
workerMetadataStream,
queueName,
JSON.stringify(workerMetadata),
config.maxLenWorkerMetadataStream
);
lastEventId = eventId as string;
return new Response('OK', { status: 200 });
} catch (err) {
const errMsg = `Failed to store worker metadata in Redis: ${err}`;
debugEnabled && debug(errMsg);
return new Response(errMsg, { status: 500 });
}
},
/**
*
* Returns a list of all registered workers.
*
* @param opts
*
* @returns { queue: string, url: string }[]
*
*/
getWorkers: async (opts: HttpHandlerOpts) => {
const workerMetadata = await opts.redisClient.hgetall(workerMetadataKey);
// Filter out some metadata fields that are not needed by the client
for (const queueName in workerMetadata) {
workerMetadata[queueName] = JSON.parse(workerMetadata[queueName]).endpoint.url;
}
return new Response(JSON.stringify(workerMetadata), { status: 200 });
},
/**
* Remove worker from the system.
*
* @param opts
*/
removeWorker: async (opts: HttpHandlerOpts) => {
const { queueName } = opts.params;
const { redisClient } = opts;
try {
const result = await (<any>redisClient)['removeWorkerMetadata'](
workerMetadataKey,
workerMetadataStream,
queueName,
config.maxLenWorkerMetadataStream
);
if (!result && !workers[queueName]) {
return new Response('Worker not found', { status: 404 });
}
lastEventId = result[1];
return new Response('OK', { status: 200 });
} catch (_err) {
const err = _err as Error;
debugEnabled && debug(`Failed to remove worker: ${err}`);
return new Response(`Failed to remove worker ${err.toString()}`, { status: 500 });
}
},
/**
* Cleans the proxy metadata from the Redis host.
* @param redisClient
* @returns
*/
cleanMetadata: async (redisClient: Redis | Cluster) => {
const multi = redisClient.multi();
multi.del(workerMetadataKey);
multi.del(workerMetadataStream);
return multi.exec();
}
}