diff --git a/package.json b/package.json index bacf740..41c27c7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mongodb-cron", - "version": "1.5.0", + "version": "1.6.0", "description": "MongoDB collection as crontab", "main": "./dist/index.js", "types": "./dist/index.d.ts", diff --git a/src/cron.ts b/src/cron.ts index a5bc87a..d1df4af 100644 --- a/src/cron.ts +++ b/src/cron.ts @@ -167,7 +167,7 @@ export class MongoCron { }, { $set: { [this.config.sleepUntilFieldPath]: sleepUntil }, }, { - returnOriginal: false, // by default, documents are ordered by the sleepUntil field + returnOriginal: true, // return original document to calculate next start based on the original value }); return res.value; } diff --git a/src/scripts/example.ts b/src/scripts/example.ts index 72450da..8ef37b0 100644 --- a/src/scripts/example.ts +++ b/src/scripts/example.ts @@ -39,4 +39,6 @@ import { MongoCron } from '..'; await sleep(30000); cron.stop(); + process.exit(0); + })().catch(console.error); diff --git a/src/tests/cron.test.ts b/src/tests/cron.test.ts index 3bd43a3..131feca 100644 --- a/src/tests/cron.test.ts +++ b/src/tests/cron.test.ts @@ -106,6 +106,28 @@ spec.test('locked documents should not be available for locking', async (ctx) => ctx.is(processed, false); }); +spec.test('recurring documents should be unlocked when prossed', async (ctx) => { + let processed = 0; + const now = moment(); + const collection = ctx.get('collection'); + const cron = new MongoCron({ + collection, + lockDuration: 60000, + onDocument: () => { + processed++; + return sleep(2000); + }, + }); + await collection.insertOne({ + sleepUntil: now.toDate(), + interval: '* * * * * *', + }); + await cron.start(); + await sleep(6000); + await cron.stop(); + ctx.is(processed, 3); +}); + spec.test('condition should filter lockable documents', async (ctx) => { let count = 0; const collection = ctx.get('collection');