-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwakaq.ts
314 lines (274 loc) · 10.7 KB
/
wakaq.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
import { Callback, Redis, Result } from 'ioredis';
import * as os from 'os';
import { Duration } from 'ts-duration';
import { type Logger } from 'winston';
import { ZRANGEPOP } from './constants.js';
import { CronTask } from './cronTask.js';
import { WakaQError } from './exceptions.js';
import { Level } from './logger.js';
import { WakaQueue } from './queue.js';
import { serialize } from './serializer.js';
import { Task } from './task.js';
declare module 'ioredis' {
interface RedisCommander<Context> {
getetatasks(key: string, argv: string, callback?: Callback<string[]>): Result<string[], Context>;
}
}
export interface RegisterTaskParams {
name?: string;
queue?: WakaQueue | string;
maxRetries?: number;
softTimeout?: Duration;
hardTimeout?: Duration;
}
export interface WakaQParams {
queues?: WakaQueue[];
schedules?: CronTask[];
host?: string;
port?: number;
db?: number;
tls?: { key: string; cert: string };
concurrency?: number;
excludeQueues?: string[];
maxRetries?: number;
softTimeout?: Duration | number;
hardTimeout?: Duration | number;
maxMemPercent?: number;
maxTasksPerWorker?: number;
connectTimeout?: number;
commandTimeout?: number;
keepAlive?: number;
noDelay?: boolean;
waitTimeout?: Duration | number;
username?: string;
password?: string;
workerLogFile?: string;
schedulerLogFile?: string;
workerLogLevel?: Level;
schedulerLogLevel?: Level;
afterWorkerStartedCallback?: () => Promise<void>;
beforeTaskStartedCallback?: (task: Task) => Promise<void>;
afterTaskFinishedCallback?: (task: Task) => Promise<void>;
}
export class WakaQ {
public tasks: Map<string, Task> = new Map<string, Task>([]);
public broker: Redis;
public queues: WakaQueue[];
public queuesByName: Map<string, WakaQueue> = new Map<string, WakaQueue>([]);
public queuesByKey: Map<string, WakaQueue> = new Map<string, WakaQueue>([]);
public softTimeout: Duration;
public hardTimeout: Duration;
public concurrency: number;
public schedules: CronTask[];
public excludeQueues: string[];
public maxRetries: number;
public connectTimeout: number;
public commandTimeout: number;
public keepAlive: number;
public noDelay: boolean;
public waitTimeout: Duration;
public maxMemPercent: number;
public maxTasksPerWorker: number;
public workerLogFile?: string;
public schedulerLogFile?: string;
public workerLogLevel: Level;
public schedulerLogLevel: Level;
public logger?: Logger;
private _pubsub?: Redis;
public currentTask?: Task;
public brokerKeys: string[];
public afterWorkerStartedCallback?: () => Promise<void>;
public beforeTaskStartedCallback?: (task: Task) => Promise<void>;
public afterTaskFinishedCallback?: (task: Task) => Promise<void>;
public broadcastKey = 'wakaq-broadcast';
constructor(params?: WakaQParams) {
const queues = params?.queues ?? [];
const schedules = params?.schedules ?? [];
const host = params?.host ?? 'localhost';
const port = params?.port ?? 6379;
const db = params?.db ?? 0;
const tls = params?.tls;
const concurrency = params?.concurrency ?? 1;
const excludeQueues = params?.excludeQueues ?? [];
const maxRetries = params?.maxRetries ?? 0;
const maxMemPercent = params?.maxMemPercent ?? 0;
const maxTasksPerWorker = params?.maxTasksPerWorker ?? 0;
this.connectTimeout = params?.connectTimeout ?? 15000;
this.commandTimeout = params?.commandTimeout ?? 15000;
this.keepAlive = params?.keepAlive ?? 0;
this.noDelay = params?.noDelay ?? true;
const {
username,
password,
workerLogLevel,
schedulerLogLevel,
afterWorkerStartedCallback,
beforeTaskStartedCallback,
afterTaskFinishedCallback,
} = params ?? {};
const lowestPriority = Math.max(
...queues.map((q) => {
return q.priority;
}),
);
queues.forEach((q) => q.setDefaultPriority(lowestPriority));
queues.sort((a, b) => a.priority - b.priority);
this.queues = queues;
queues.forEach((q) => {
this.queuesByName.set(q.name, q);
this.queuesByKey.set(q.brokerKey, q);
});
this.excludeQueues = this._validateQueueNames(excludeQueues);
this.maxRetries = maxRetries;
this.brokerKeys = queues.filter((q) => !this.excludeQueues.includes(q.name)).map((q) => q.brokerKey);
this.schedules = schedules;
this.concurrency = this._formatConcurrency(concurrency);
this.softTimeout = this._asDuration(params?.softTimeout, 0);
this.hardTimeout = this._asDuration(params?.hardTimeout, 0);
this.waitTimeout = this._asDuration(params?.waitTimeout, 1);
if (this.softTimeout.seconds && this.softTimeout.seconds <= this.waitTimeout.seconds)
throw new WakaQError(
`Soft timeout (${this.softTimeout.seconds}) can not be less than or equal to wait timeout (${this.waitTimeout.seconds}).`,
);
if (this.hardTimeout.seconds && this.hardTimeout.seconds <= this.waitTimeout.seconds)
throw new WakaQError(
`Hard timeout (${this.hardTimeout.seconds}) can not be less than or equal to wait timeout (${this.waitTimeout.seconds}).`,
);
if (this.softTimeout.seconds && this.hardTimeout.seconds && this.hardTimeout.seconds <= this.softTimeout.seconds)
throw new WakaQError(
`Hard timeout (${this.hardTimeout.seconds}) can not be less than or equal to soft timeout (${this.softTimeout.seconds}).`,
);
if ((maxMemPercent && maxMemPercent < 1) || maxMemPercent > 99)
throw new WakaQError(`Max memory percent must be between 1 and 99: ${maxMemPercent}`);
this.maxMemPercent = maxMemPercent;
this.maxTasksPerWorker = maxTasksPerWorker > 0 ? maxTasksPerWorker : 0;
this.workerLogFile = params?.workerLogFile;
this.schedulerLogFile = params?.schedulerLogFile;
this.workerLogLevel = workerLogLevel ?? Level.INFO;
this.schedulerLogLevel = schedulerLogLevel ?? Level.INFO;
this.afterWorkerStartedCallback = afterWorkerStartedCallback;
this.beforeTaskStartedCallback = beforeTaskStartedCallback;
this.afterTaskFinishedCallback = afterTaskFinishedCallback;
this.broker = new Redis({
host: host,
port: port,
username: username,
password: password,
db: db,
tls: tls,
lazyConnect: true,
connectTimeout: this.connectTimeout,
commandTimeout: this.commandTimeout,
keepAlive: this.keepAlive,
noDelay: this.noDelay,
});
this.broker.on('error', (err) => {
this.logger?.error(err);
});
}
public async connect() {
await this.broker.connect();
this.broker.defineCommand('getetatasks', {
numberOfKeys: 1,
lua: ZRANGEPOP,
});
return this;
}
public disconnect() {
this.broker.disconnect();
this._pubsub?.disconnect();
}
/*
Task wrapper.
Wrap an async function with this to register it as a task.
Returns the new Task with methods delay() and broadcast().
*/
public task(fn: (...arg0: unknown[]) => Promise<void>, params?: RegisterTaskParams): Task {
const task = new Task(this, fn, params?.name, params?.queue, params?.softTimeout, params?.hardTimeout, params?.maxRetries);
if (this.tasks.has(task.name)) throw new WakaQError(`Duplicate task name: ${task.name}`);
this.tasks.set(task.name, task);
return task;
}
public afterWorkerStarted(callback: () => Promise<void>) {
this.afterWorkerStartedCallback = callback;
return callback;
}
public beforeTaskStarted(callback: (task: Task) => Promise<void>) {
this.beforeTaskStartedCallback = callback;
return callback;
}
public afterTaskFinished(callback: (task: Task) => Promise<void>) {
this.afterTaskFinishedCallback = callback;
return callback;
}
private _validateQueueNames(queueNames: string[]): string[] {
queueNames.forEach((queueName) => {
if (!this.queuesByName.has(queueName)) throw new WakaQError(`Invalid queue: ${queueName}`);
});
return queueNames;
}
private _asDuration(obj?: Duration | { seconds: number } | number, def?: number): Duration {
if (obj instanceof Duration) return obj;
if (typeof obj === 'object' && typeof obj.seconds === 'number') return obj as Duration;
if (typeof obj === 'number') return Duration.second(obj);
return Duration.second(def ?? 0);
}
public async enqueueAtFront(taskName: string, args: any[], queue?: WakaQueue | string) {
queue = this._queueOrDefault(queue);
const payload = serialize({ name: taskName, args: args });
await this.broker.lpush(queue.brokerKey, payload);
}
public async enqueueWithEta(taskName: string, args: any[], eta: Date | Duration, queue?: WakaQueue | string) {
queue = this._queueOrDefault(queue);
const payload = serialize({ name: taskName, args: args });
const timestamp = Math.round((eta instanceof Duration ? Date.now() + eta.milliseconds : eta.getTime()) / 1000);
await this.broker.zadd(queue.brokerEtaKey, 'NX', String(timestamp), payload);
}
public async enqueueAtEnd(taskName: string, args: any[], queue?: WakaQueue | string, retry = 0) {
queue = this._queueOrDefault(queue);
const payload = serialize({ name: taskName, args: args, retry: retry });
await this.broker.rpush(queue.brokerKey, payload);
}
public async broadcast(taskName: string, args: any[]): Promise<number> {
const payload = serialize({ name: taskName, args: args });
const pubsub = await this.pubsub();
return await pubsub.publish(this.broadcastKey, payload);
}
public async sleep(duration: Duration) {
return new Promise((resolve) => {
setTimeout(resolve, duration.milliseconds);
});
}
public async pubsub() {
if (!this._pubsub) {
this._pubsub = this.broker.duplicate();
await this._pubsub.connect();
}
return this._pubsub;
}
private _queueOrDefault(queue?: WakaQueue | string): WakaQueue {
if (typeof queue === 'string') queue = this.queuesByName.get(queue);
if (queue) return queue;
return this.defaultQueue;
}
get defaultQueue(): WakaQueue {
if (this.queues.length === 0) throw new WakaQError('Missing queues.');
return this.queues[this.queues.length - 1] as WakaQueue;
}
private _formatConcurrency(concurrency: number | string | undefined): number {
if (!concurrency) return 0;
if (typeof concurrency === 'number') {
if (concurrency < 1) throw new WakaQError(`Concurrency must be greater than zero: ${concurrency}`);
return Math.round(concurrency);
}
const parts = concurrency.split('*');
if (parts.length > 1) {
return parts.map((part) => this._formatConcurrency(part)).reduce((a, n) => a * n, 1);
} else {
const cores = String(os.cpus().length);
const x = Number.parseInt(concurrency.replace('cores', cores).trim());
if (Number.isNaN(x)) throw new WakaQError(`Error parsing concurrency: ${concurrency}`);
return x;
}
}
}