Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(scheduler): do not save offset value when every is provided #3142

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 6 additions & 52 deletions src/classes/job-scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,19 +93,19 @@ export class JobScheduler extends QueueBase {
}

let nextMillis: number;
let newOffset = offset || 0;
let newOffset = 0;

if (every) {
const prevSlot = Math.floor(startMillis / every) * every;
const nextSlot = prevSlot + every;
if (prevMillis || offset) {
newOffset = startMillis - prevSlot;
// newOffset should always be positive, but we do an extra safety check
newOffset = newOffset < 0 ? 0 : newOffset;

if (prevMillis) {
nextMillis = nextSlot;
} else {
nextMillis = prevSlot;
newOffset = startMillis - prevSlot;

// newOffset should always be positive, but we do an extra safety check
newOffset = newOffset < 0 ? 0 : newOffset;
}
} else if (pattern) {
nextMillis = await this.repeatStrategy(now, repeatOpts, jobName);
Expand Down Expand Up @@ -246,7 +246,6 @@ export class JobScheduler extends QueueBase {
mergedOpts.repeat = {
...opts.repeat,
count: currentCount,
offset,
endDate: opts.repeat?.endDate
? new Date(opts.repeat.endDate).getTime()
: undefined,
Expand All @@ -255,51 +254,6 @@ export class JobScheduler extends QueueBase {
return mergedOpts;
}

private createNextJob<T = any, R = any, N extends string = string>(
client: RedisClient,
name: N,
nextMillis: number,
offset: number,
jobSchedulerId: string,
opts: JobsOptions,
data: T,
currentCount: number,
// The job id of the job that produced this next iteration
producerId?: string,
) {
//
// Generate unique job id for this iteration.
//
const jobId = this.getSchedulerNextJobId({
jobSchedulerId,
nextMillis,
});

const now = Date.now();
const delay = nextMillis + offset - now;

const mergedOpts = {
...opts,
jobId,
delay: delay < 0 ? 0 : delay,
timestamp: now,
prevMillis: nextMillis,
repeatJobKey: jobSchedulerId,
};

mergedOpts.repeat = { ...opts.repeat, count: currentCount };

const job = new this.Job<T, R, N>(this, name, data, mergedOpts, jobId);
job.addJob(client);

if (producerId) {
const producerJobKey = this.toKey(producerId);
client.hset(producerJobKey, 'nrjid', job.id);
}

return job;
}

async removeJobScheduler(jobSchedulerId: string): Promise<number> {
return this.scripts.removeJobScheduler(jobSchedulerId);
}
Expand Down
2 changes: 1 addition & 1 deletion src/classes/scripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ export class Scripts {
nextMillis: number,
templateData: string,
delayedJobOpts: JobsOptions,
// The job id of the job that produced this next iteration
// The job id of the job that produced this next iteration - TODO: remove in next breaking change
producerId?: string,
): Promise<string | null> {
const client = await this.queue.client;
Expand Down
1 change: 1 addition & 0 deletions src/interfaces/repeat-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export interface RepeatOptions extends Omit<ParserOptions, 'iterator'> {
count?: number;

/**
* @deprecated
* Offset in milliseconds to affect the next iteration time
*
* */
Expand Down
81 changes: 80 additions & 1 deletion tests/test_job_scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -983,9 +983,16 @@ describe('Job Scheduler', function () {
this.clock.setSystemTime(date);
const nextTick = 2 * ONE_SECOND;

let iterationCount = 0;
const worker = new Worker(
queueName,
async () => {
async job => {
if (iterationCount === 0) {
expect(job.opts.delay).to.be.eq(0);
} else {
expect(job.opts.delay).to.be.eq(2000);
}
iterationCount++;
this.clock.tick(nextTick);
},
{ autorun: false, connection, prefix },
Expand Down Expand Up @@ -1040,6 +1047,78 @@ describe('Job Scheduler', function () {
});
});

describe("when using 'every' option and jobs are moved to active some time after after delay", function () {
it('should repeat every 2 seconds and start immediately', async function () {
const date = new Date('2017-02-07 9:24:00');
this.clock.setSystemTime(date);
const nextTick = 2 * ONE_SECOND;

let iterationCount = 0;
const worker = new Worker(
queueName,
async job => {
if (iterationCount === 0) {
expect(job.opts.delay).to.be.eq(0);
} else {
expect(job.opts.delay).to.be.eq(2000);
}
this.clock.tick(nextTick + iterationCount * 100);
iterationCount++;
},
{ autorun: false, connection, prefix },
);

let prev: Job;
let counter = 0;

const completing = new Promise<void>((resolve, reject) => {
worker.on('completed', async job => {
try {
if (prev && counter === 1) {
expect(prev.timestamp).to.be.lte(job.timestamp);
expect(job.timestamp - prev.timestamp).to.be.lte(1);
} else if (prev) {
expect(prev.timestamp).to.be.lt(job.timestamp);
expect(job.timestamp - prev.timestamp).to.be.eq(
2000 + (counter - 2) * 100,
);
}
prev = job;
counter++;
if (counter === 5) {
resolve();
}
} catch (err) {
reject(err);
}
});
});

await queue.upsertJobScheduler(
'repeat',
{
every: 2000,
},
{ data: { foo: 'bar' } },
);

const waitingCountBefore = await queue.getWaitingCount();
expect(waitingCountBefore).to.be.eq(1);

worker.run();

await completing;

const waitingCount = await queue.getWaitingCount();
expect(waitingCount).to.be.eq(0);

const delayedCountAfter = await queue.getDelayedCount();
expect(delayedCountAfter).to.be.eq(1);

await worker.close();
});
});

describe("when using 'every' and time is one millisecond before iteration time", function () {
it('should repeat every 2 seconds and start immediately', async function () {
const startTimeMillis = new Date('2017-02-07 9:24:00').getTime();
Expand Down