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

Feat add immediately opt for repeat #536

Merged
Merged
Changes from 1 commit
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
Next Next commit
feat(repeat): add immediately opt for repeat
roggervalf committed May 8, 2021
commit 43a74d3473c65afe13e9cd85652a9e49e197f7db
12 changes: 7 additions & 5 deletions src/classes/repeat.ts
Original file line number Diff line number Diff line change
@@ -54,7 +54,7 @@ export class Repeat extends QueueBase {
name,
nextMillis,
repeatJobKey,
{ ...opts, repeat: repeatOpts },
{ ...opts, repeat: { ...repeatOpts, immediately: false } },
data,
currentCount,
);
@@ -215,7 +215,11 @@ function getNextMillis(millis: number, opts: RepeatOptions) {
}

if (opts.every) {
return Math.floor(millis / opts.every) * opts.every + opts.every;
console.log('immediately', opts.immediately);
return (
Math.floor(millis / opts.every) * opts.every +
(opts.immediately ? 0 : opts.every)
);
}

const currentDate =
@@ -235,7 +239,5 @@ function getNextMillis(millis: number, opts: RepeatOptions) {
}

function md5(str: string) {
return createHash('md5')
.update(str)
.digest('hex');
return createHash('md5').update(str).digest('hex');
}
6 changes: 5 additions & 1 deletion src/interfaces/repeat-options.ts
Original file line number Diff line number Diff line change
@@ -29,7 +29,11 @@ export interface RepeatOptions {
* (`cron` setting cannot be used together with this setting.)
*/
every?: number;

/**
* Repeated job should start right now
* ( work only with every settings)
*/
immediately?: boolean;
/**
* The start value for the repeat iteration count.
*/
52 changes: 51 additions & 1 deletion src/test/test_repeat.ts
Original file line number Diff line number Diff line change
@@ -20,7 +20,7 @@ const MAX_INT = 2147483647;

const NoopProc = async (job: Job) => {};

describe('repeat', function() {
describe.only('repeat', function() {
this.timeout(10000);
let repeat: Repeat;
let queue: Queue;
@@ -274,6 +274,56 @@ describe('repeat', function() {
await worker.close();
});

it.only('should repeat every 2 seconds and start immediately', async function() {
this.timeout(2000);
const queueScheduler = new QueueScheduler(queueName);
await queueScheduler.waitUntilReady();

const date = new Date('2017-02-07 9:24:00');
this.clock.setSystemTime(date);
const nextTick = 2 * ONE_SECOND;

const worker = new Worker(queueName, async () => {});

await queue.add(
'repeat',
{ foo: 'bar' },
{
repeat: {
every: 2000,
immediately: true,
},
},
);

this.clock.tick(500);

let prev: Job;
let counter = 0;

const completing = new Promise<void>(resolve => {
worker.on('completed', async job => {
this.clock.tick(nextTick);
if (prev && counter === 1) {
expect(prev.timestamp).to.be.lt(job.timestamp);
expect(job.timestamp - prev.timestamp).to.be.gte(500);
} else if (prev) {
expect(prev.timestamp).to.be.lt(job.timestamp);
expect(job.timestamp - prev.timestamp).to.be.gte(2000);
}
prev = job;
counter++;
if (counter === 5) {
resolve();
}
});
});

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

// Skipped until we find a way of simulating time to avoid waiting 5 days
it.skip('should repeat once a day for 5 days', async function() {
const queueScheduler = new QueueScheduler(queueName);