-
Notifications
You must be signed in to change notification settings - Fork 357
/
repeat.ts
279 lines (238 loc) · 7.39 KB
/
repeat.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
import { parseExpression } from 'cron-parser';
import { createHash } from 'crypto';
import { RepeatBaseOptions, RepeatableJob, RepeatOptions } from '../interfaces';
import { JobsOptions, RepeatStrategy } from '../types';
import { Job } from './job';
import { QueueBase } from './queue-base';
import { RedisConnection } from './redis-connection';
export class Repeat extends QueueBase {
private repeatStrategy: RepeatStrategy;
private repeatKeyHashAlgorithm: string;
constructor(
name: string,
opts: RepeatBaseOptions,
Connection?: typeof RedisConnection,
) {
super(name, opts, Connection);
this.repeatStrategy =
(opts.settings && opts.settings.repeatStrategy) || getNextMillis;
this.repeatKeyHashAlgorithm =
(opts.settings && opts.settings.repeatKeyHashAlgorithm) || 'md5';
}
async addNextRepeatableJob<T = any, R = any, N extends string = string>(
name: N,
data: T,
opts: JobsOptions,
skipCheckExists?: boolean,
): Promise<Job<T, R, N> | undefined> {
// HACK: This is a temporary fix to enable easy migration from bullmq <3.0.0
// to >= 3.0.0. It should be removed when moving to 4.x.
const repeatOpts: RepeatOptions & { cron?: string } = { ...opts.repeat };
repeatOpts.pattern ??= repeatOpts.cron;
delete repeatOpts.cron;
const prevMillis = opts.prevMillis || 0;
const currentCount = repeatOpts.count ? repeatOpts.count + 1 : 1;
if (
typeof repeatOpts.limit !== 'undefined' &&
currentCount > repeatOpts.limit
) {
return;
}
let now = Date.now();
if (
!(typeof repeatOpts.endDate === undefined) &&
now > new Date(repeatOpts.endDate!).getTime()
) {
return;
}
now = prevMillis < now ? now : prevMillis;
const nextMillis = await this.repeatStrategy(now, repeatOpts, name);
const pattern = repeatOpts.pattern;
const hasImmediately = Boolean(
(repeatOpts.every || pattern) && repeatOpts.immediately,
);
const offset = hasImmediately ? now - nextMillis : undefined;
if (nextMillis) {
// We store the undecorated opts.jobId into the repeat options
if (!prevMillis && opts.jobId) {
repeatOpts.jobId = opts.jobId;
}
const repeatJobKey = getRepeatKey(name, repeatOpts);
let repeatableExists = true;
if (!skipCheckExists) {
// Check that the repeatable job hasn't been removed
// TODO: a lua script would be better here
const client = await this.client;
repeatableExists = !!(await client.zscore(
this.keys.repeat,
repeatJobKey,
));
}
const { immediately, ...filteredRepeatOpts } = repeatOpts;
// The job could have been deleted since this check
if (repeatableExists) {
return this.createNextJob<T, R, N>(
name,
nextMillis,
repeatJobKey,
{ ...opts, repeat: { offset, ...filteredRepeatOpts } },
data,
currentCount,
hasImmediately,
);
}
}
}
private async createNextJob<T = any, R = any, N extends string = string>(
name: N,
nextMillis: number,
repeatJobKey: string,
opts: JobsOptions,
data: T,
currentCount: number,
hasImmediately: boolean,
) {
const client = await this.client;
//
// Generate unique job id for this iteration.
//
const jobId = this.getRepeatJobId({
name,
nextMillis,
namespace: this.hash(repeatJobKey),
jobId: opts.repeat.jobId,
key: opts.repeat.key,
});
const now = Date.now();
const delay =
nextMillis + (opts.repeat.offset ? opts.repeat.offset : 0) - now;
const mergedOpts = {
...opts,
jobId,
delay: delay < 0 || hasImmediately ? 0 : delay,
timestamp: now,
prevMillis: nextMillis,
repeatJobKey,
};
mergedOpts.repeat = { ...opts.repeat, count: currentCount };
await client.zadd(this.keys.repeat, nextMillis.toString(), repeatJobKey);
return this.Job.create<T, R, N>(this, name, data, mergedOpts);
}
async removeRepeatable(
name: string,
repeat: RepeatOptions,
jobId?: string,
): Promise<number> {
const repeatJobKey = getRepeatKey(name, { ...repeat, jobId });
const repeatJobId = this.getRepeatJobId({
name,
nextMillis: '',
namespace: this.hash(repeatJobKey),
jobId: jobId ?? repeat.jobId,
key: repeat.key,
});
return this.scripts.removeRepeatable(repeatJobId, repeatJobKey);
}
async removeRepeatableByKey(repeatJobKey: string): Promise<number> {
const data = this.keyToData(repeatJobKey);
const repeatJobId = this.getRepeatJobId({
name: data.name,
nextMillis: '',
namespace: this.hash(repeatJobKey),
jobId: data.id,
});
return this.scripts.removeRepeatable(repeatJobId, repeatJobKey);
}
private keyToData(key: string, next?: number): RepeatableJob {
const data = key.split(':');
const pattern = data.slice(4).join(':') || null;
return {
key,
name: data[0],
id: data[1] || null,
endDate: parseInt(data[2]) || null,
tz: data[3] || null,
pattern,
next,
};
}
async getRepeatableJobs(
start = 0,
end = -1,
asc = false,
): Promise<RepeatableJob[]> {
const client = await this.client;
const key = this.keys.repeat;
const result = asc
? await client.zrange(key, start, end, 'WITHSCORES')
: await client.zrevrange(key, start, end, 'WITHSCORES');
const jobs = [];
for (let i = 0; i < result.length; i += 2) {
jobs.push(this.keyToData(result[i], parseInt(result[i + 1])));
}
return jobs;
}
async getRepeatableCount(): Promise<number> {
const client = await this.client;
return client.zcard(this.toKey('repeat'));
}
private hash(str: string) {
return createHash(this.repeatKeyHashAlgorithm).update(str).digest('hex');
}
private getRepeatJobId({
name,
nextMillis,
namespace,
jobId,
key,
}: {
name?: string;
nextMillis: number | string;
namespace?: string;
jobId?: string;
key?: string;
}) {
const checksum = key ?? this.hash(`${name}${jobId || ''}${namespace}`);
return `repeat:${checksum}:${nextMillis}`;
// return `repeat:${jobId || ''}:${name}:${namespace}:${nextMillis}`;
//return `repeat:${name}:${namespace}:${nextMillis}`;
}
}
function getRepeatKey(name: string, repeat: RepeatOptions) {
const endDate = repeat.endDate ? new Date(repeat.endDate).getTime() : '';
const tz = repeat.tz || '';
const pattern = repeat.pattern;
const suffix = (pattern ? pattern : String(repeat.every)) || '';
const jobId = repeat.jobId ? repeat.jobId : '';
return `${name}:${jobId}:${endDate}:${tz}:${suffix}`;
}
export const getNextMillis = (
millis: number,
opts: RepeatOptions,
): number | undefined => {
const pattern = opts.pattern;
if (pattern && opts.every) {
throw new Error(
'Both .pattern and .every options are defined for this repeatable job',
);
}
if (opts.every) {
return (
Math.floor(millis / opts.every) * opts.every +
(opts.immediately ? 0 : opts.every)
);
}
const currentDate =
opts.startDate && new Date(opts.startDate) > new Date(millis)
? new Date(opts.startDate)
: new Date(millis);
const interval = parseExpression(pattern, {
...opts,
currentDate,
});
try {
return interval.next().getTime();
} catch (e) {
// Ignore error
}
};