Skip to content

Commit db87d7f

Browse files
committed
Merge branch 'main' into codex/update-polling-logic-for-task-handlers
2 parents 94a314c + 7364d6b commit db87d7f

4 files changed

Lines changed: 20 additions & 9 deletions

File tree

.npmignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.github
2+
eslint.config.js
3+
test
4+
.npmrc

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@mongoosejs/task",
3-
"version": "0.6.1",
3+
"version": "0.6.2",
44
"private": false,
55
"scripts": {
66
"lint": "eslint .",
@@ -19,8 +19,8 @@
1919
"devDependencies": {
2020
"@masteringjs/eslint-config": "0.1.1",
2121
"eslint": "9.30.0",
22-
"mocha": "10.1.0",
22+
"mocha": "12.0.0-beta-8",
2323
"mongoose": "9.x",
24-
"sinon": "15.2.0"
24+
"sinon": "16.x"
2525
}
2626
}

src/taskSchema.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,11 @@ taskSchema.statics.cancelTask = async function cancelTask(filter) {
9696
if (filter != null) {
9797
filter = { $and: [{ status: 'pending' }, filter] };
9898
}
99-
const task = await this.findOneAndUpdate(filter, { status: 'cancelled', cancelledAt: new Date() }, { returnDocument: 'after' });
99+
const task = await this.findOneAndUpdate(
100+
filter,
101+
{ status: 'cancelled', cancelledAt: new Date() },
102+
{ returnDocument: 'before' }
103+
);
100104
return task;
101105
};
102106

@@ -174,7 +178,7 @@ taskSchema.statics.expireTimedOutTasks = async function expireTimedOutTasks(opti
174178
finishedRunningAt: now
175179
}
176180
},
177-
{ new: true }
181+
{ returnDocument: 'before' }
178182
);
179183

180184
if (!task) {
@@ -281,7 +285,7 @@ taskSchema.statics.poll = async function poll(opts) {
281285
timeoutAt: new Date(now.valueOf() + 10 * 60 * 1000), // 10 minutes from startedRunningAt
282286
...additionalParams
283287
},
284-
{ new: false }
288+
{ returnDocument: 'before' }
285289
);
286290

287291
if (task == null || task.status !== 'pending') {
@@ -322,14 +326,15 @@ taskSchema.statics.execute = async function(task, options = {}) {
322326
try {
323327
let result = null;
324328
if (typeof task.timeoutMS === 'number') {
329+
let timeoutId;
325330
result = await Promise.race([
326331
Promise.resolve(
327332
this._handlers.get(task.name).call(task, task.params, task)
328333
),
329334
new Promise((_, reject) => {
330-
setTimeout(() => reject(new Error(`Task timed out after ${task.timeoutMS} ms`)), task.timeoutMS);
335+
timeoutId = setTimeout(() => reject(new Error(`Task timed out after ${task.timeoutMS} ms`)), task.timeoutMS);
331336
})
332-
]);
337+
]).finally(() => clearTimeout(timeoutId));
333338
} else {
334339
result = await Promise.resolve(
335340
this._handlers.get(task.name).call(task, task.params, task)

test/task.test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,9 @@ describe('Task', function() {
288288
resolve = _resolve;
289289
reject = _reject;
290290
});
291+
let handlerTimeout;
291292
Task.registerHandler('getQuestion', async () => {
292-
await new Promise(resolve => setTimeout(resolve, 10000));
293+
await new Promise(resolve => { handlerTimeout = setTimeout(resolve, 10000); });
293294
});
294295

295296
let task = await Task.schedule('getQuestion', time.now().valueOf() + 100000, null, { timeoutMS: 50 });
@@ -301,6 +302,7 @@ describe('Task', function() {
301302
assert.equal(task.status, 'failed');
302303
assert.equal(task.error.message, 'Task timed out after 50 ms');
303304
assert.equal(task.finishedRunningAt.valueOf(), now.valueOf());
305+
clearTimeout(handlerTimeout);
304306
});
305307

306308
it('expires timed out tasks and handles repeats', async function() {

0 commit comments

Comments
 (0)