Skip to content

Commit

Permalink
Fix lock duration bug
Browse files Browse the repository at this point in the history
  • Loading branch information
xpepermint committed May 5, 2019
1 parent 43f8cd3 commit 9f4abf1
Show file tree
Hide file tree
Showing 4 changed files with 26 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.5.0",
"version": "1.6.0",
"description": "MongoDB collection as crontab",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion src/cron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 2 additions & 0 deletions src/scripts/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,6 @@ import { MongoCron } from '..';
await sleep(30000);
cron.stop();

process.exit(0);

})().catch(console.error);
22 changes: 22 additions & 0 deletions src/tests/cron.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 9f4abf1

Please sign in to comment.