Skip to content

Commit

Permalink
Fix old recurring job processing
Browse files Browse the repository at this point in the history
  • Loading branch information
xpepermint committed May 5, 2019
1 parent 9f4abf1 commit 249ef3e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mongodb-cron",
"version": "1.6.0",
"version": "1.7.0",
"description": "MongoDB collection as crontab",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand Down
4 changes: 3 additions & 1 deletion src/cron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,9 @@ export class MongoCron {
currentDate: future.toDate(),
endDate: dot.pick(this.config.repeatUntilFieldPath, doc),
});
return interval.next().toDate();
const next = interval.next().toDate();
const now = moment().toDate();
return next < now ? now : next; // process old recurring jobs only once
} catch (err) {
return null;
}
Expand Down
18 changes: 18 additions & 0 deletions src/tests/cron.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,24 @@ spec.test('recurring documents should be unlocked when prossed', async (ctx) =>
ctx.is(processed, 3);
});

spec.test('recurring documents should process from current date', async (ctx) => {
let processed = 0;
const past = moment().subtract(10, 'days');
const collection = ctx.get('collection');
const cron = new MongoCron({
collection,
onDocument: () => processed++,
});
await collection.insertOne({
sleepUntil: past.toDate(), // should be treated as now() date
interval: '* * * * * *',
});
await cron.start();
await sleep(2000);
await cron.stop();
ctx.true(processed <= 4);
});

spec.test('condition should filter lockable documents', async (ctx) => {
let count = 0;
const collection = ctx.get('collection');
Expand Down

0 comments on commit 249ef3e

Please sign in to comment.