Skip to content

Commit 9f4abf1

Browse files
committed
Fix lock duration bug
1 parent 43f8cd3 commit 9f4abf1

File tree

4 files changed

+26
-2
lines changed

4 files changed

+26
-2
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mongodb-cron",
3-
"version": "1.5.0",
3+
"version": "1.6.0",
44
"description": "MongoDB collection as crontab",
55
"main": "./dist/index.js",
66
"types": "./dist/index.d.ts",

src/cron.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ export class MongoCron {
167167
}, {
168168
$set: { [this.config.sleepUntilFieldPath]: sleepUntil },
169169
}, {
170-
returnOriginal: false, // by default, documents are ordered by the sleepUntil field
170+
returnOriginal: true, // return original document to calculate next start based on the original value
171171
});
172172
return res.value;
173173
}

src/scripts/example.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,6 @@ import { MongoCron } from '..';
3939
await sleep(30000);
4040
cron.stop();
4141

42+
process.exit(0);
43+
4244
})().catch(console.error);

src/tests/cron.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,28 @@ spec.test('locked documents should not be available for locking', async (ctx) =>
106106
ctx.is(processed, false);
107107
});
108108

109+
spec.test('recurring documents should be unlocked when prossed', async (ctx) => {
110+
let processed = 0;
111+
const now = moment();
112+
const collection = ctx.get('collection');
113+
const cron = new MongoCron({
114+
collection,
115+
lockDuration: 60000,
116+
onDocument: () => {
117+
processed++;
118+
return sleep(2000);
119+
},
120+
});
121+
await collection.insertOne({
122+
sleepUntil: now.toDate(),
123+
interval: '* * * * * *',
124+
});
125+
await cron.start();
126+
await sleep(6000);
127+
await cron.stop();
128+
ctx.is(processed, 3);
129+
});
130+
109131
spec.test('condition should filter lockable documents', async (ctx) => {
110132
let count = 0;
111133
const collection = ctx.get('collection');

0 commit comments

Comments
 (0)