Skip to content

Commit 3bfc820

Browse files
committed
perf(scheduler): do not save offset value when every is provided
1 parent f13e58b commit 3bfc820

File tree

4 files changed

+9
-53
lines changed

4 files changed

+9
-53
lines changed

src/classes/job-scheduler.ts

+6-52
Original file line numberDiff line numberDiff line change
@@ -93,19 +93,19 @@ export class JobScheduler extends QueueBase {
9393
}
9494

9595
let nextMillis: number;
96-
let newOffset = offset || 0;
96+
let newOffset = 0;
9797

9898
if (every) {
9999
const prevSlot = Math.floor(startMillis / every) * every;
100100
const nextSlot = prevSlot + every;
101-
if (prevMillis || offset) {
101+
newOffset = startMillis - prevSlot;
102+
// newOffset should always be positive, but we do an extra safety check
103+
newOffset = newOffset < 0 ? 0 : newOffset;
104+
105+
if (prevMillis) {
102106
nextMillis = nextSlot;
103107
} else {
104108
nextMillis = prevSlot;
105-
newOffset = startMillis - prevSlot;
106-
107-
// newOffset should always be positive, but we do an extra safety check
108-
newOffset = newOffset < 0 ? 0 : newOffset;
109109
}
110110
} else if (pattern) {
111111
nextMillis = await this.repeatStrategy(now, repeatOpts, jobName);
@@ -246,7 +246,6 @@ export class JobScheduler extends QueueBase {
246246
mergedOpts.repeat = {
247247
...opts.repeat,
248248
count: currentCount,
249-
offset,
250249
endDate: opts.repeat?.endDate
251250
? new Date(opts.repeat.endDate).getTime()
252251
: undefined,
@@ -255,51 +254,6 @@ export class JobScheduler extends QueueBase {
255254
return mergedOpts;
256255
}
257256

258-
private createNextJob<T = any, R = any, N extends string = string>(
259-
client: RedisClient,
260-
name: N,
261-
nextMillis: number,
262-
offset: number,
263-
jobSchedulerId: string,
264-
opts: JobsOptions,
265-
data: T,
266-
currentCount: number,
267-
// The job id of the job that produced this next iteration
268-
producerId?: string,
269-
) {
270-
//
271-
// Generate unique job id for this iteration.
272-
//
273-
const jobId = this.getSchedulerNextJobId({
274-
jobSchedulerId,
275-
nextMillis,
276-
});
277-
278-
const now = Date.now();
279-
const delay = nextMillis + offset - now;
280-
281-
const mergedOpts = {
282-
...opts,
283-
jobId,
284-
delay: delay < 0 ? 0 : delay,
285-
timestamp: now,
286-
prevMillis: nextMillis,
287-
repeatJobKey: jobSchedulerId,
288-
};
289-
290-
mergedOpts.repeat = { ...opts.repeat, count: currentCount };
291-
292-
const job = new this.Job<T, R, N>(this, name, data, mergedOpts, jobId);
293-
job.addJob(client);
294-
295-
if (producerId) {
296-
const producerJobKey = this.toKey(producerId);
297-
client.hset(producerJobKey, 'nrjid', job.id);
298-
}
299-
300-
return job;
301-
}
302-
303257
async removeJobScheduler(jobSchedulerId: string): Promise<number> {
304258
return this.scripts.removeJobScheduler(jobSchedulerId);
305259
}

src/classes/scripts.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ export class Scripts {
376376
nextMillis: number,
377377
templateData: string,
378378
delayedJobOpts: JobsOptions,
379-
// The job id of the job that produced this next iteration
379+
// The job id of the job that produced this next iteration - TODO: remove in next breaking change
380380
producerId?: string,
381381
): Promise<string | null> {
382382
const client = await this.queue.client;

src/interfaces/repeat-options.ts

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export interface RepeatOptions extends Omit<ParserOptions, 'iterator'> {
4242
count?: number;
4343

4444
/**
45+
* TODO: remove this property in favor of `key`
4546
* Offset in milliseconds to affect the next iteration time
4647
*
4748
* */

src/interfaces/repeatable-options.ts

+1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ export type RepeatableOptions = {
55
limit?: number;
66
pattern?: string;
77
every?: number;
8+
offset?: number;
89
};

0 commit comments

Comments
 (0)