Skip to content

Commit

Permalink
fix: revert 1.6.3
Browse files Browse the repository at this point in the history
  • Loading branch information
code-xhyun committed Nov 22, 2024
1 parent 1fac1ba commit 545d9d4
Showing 1 changed file with 10 additions and 96 deletions.
106 changes: 10 additions & 96 deletions src/pulse/resume-on-restart.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import createDebugger from 'debug';
import { Pulse } from '.';
import { Job } from '../job';

const debug = createDebugger('pulse:resumeOnRestart');

Expand All @@ -19,48 +18,19 @@ export const resumeOnRestart: ResumeOnRestartMethod = function (this: Pulse, res

if (this._collection && this._resumeOnRestart) {
const now = new Date();

// Non-recurring jobs
this._collection
.updateMany(
{
$and: [
{ repeatInterval: { $exists: false } }, // Ensure the job is not recurring (no repeatInterval)
{ repeatAt: { $exists: false } }, // Ensure the job is not recurring (no repeatAt)
$or: [
{
lockedAt: { $exists: true },
nextRunAt: { $ne: null },
$or: [{ $expr: { $eq: ['$runCount', '$finishedCount'] } }, { lastFinishedAt: { $exists: false } }],
},
{
$or: [
{
lockedAt: { $exists: true }, // Locked jobs (interrupted or in-progress)
$and: [
{
$or: [
{ nextRunAt: { $lte: now, $ne: null } }, // Overdue jobs
{ nextRunAt: { $exists: false } }, // Jobs missing nextRunAt
{ nextRunAt: null }, // Jobs explicitly set to null
],
},
{
$or: [
{ $expr: { $eq: ['$runCount', '$finishedCount'] } }, // Jobs finished but stuck due to locking
{ $or: [{ lastFinishedAt: { $exists: false } }, { lastFinishedAt: null }] }, // Jobs that were not finished
],
},
],
},
{
lockedAt: { $exists: false }, // Unlocked jobs (not in-progress)
$and: [
{
$or: [
{ nextRunAt: { $lte: now, $ne: null } }, // Overdue jobs
{ nextRunAt: { $exists: false } }, // Jobs missing nextRunAt
{ nextRunAt: null }, // Jobs explicitly set to null
],
},
{ $or: [{ lastFinishedAt: { $exists: false } }, { lastFinishedAt: null }] }, // Jobs not finished
],
},
],
lockedAt: { $exists: false },
lastFinishedAt: { $exists: false },
nextRunAt: { $lte: now, $ne: null },
},
],
},
Expand All @@ -71,63 +41,7 @@ export const resumeOnRestart: ResumeOnRestartMethod = function (this: Pulse, res
)
.then((result) => {
if (result.modifiedCount > 0) {
debug('Resumed %d unfinished standard jobs (%s)', result.modifiedCount, now.toISOString());
}
});

// Recurring jobs
this._collection
.find({
$and: [
{ $or: [{ repeatInterval: { $exists: true } }, { repeatAt: { $exists: true } }] }, // Recurring jobs
{
$or: [
{ nextRunAt: { $lte: now } }, // Overdue jobs
{ nextRunAt: { $exists: false } }, // Jobs missing nextRunAt
{ nextRunAt: null }, // Jobs explicitly set to null
],
},
{
$or: [
{ lastFinishedAt: { $exists: false } }, // Jobs never run
{ lastFinishedAt: { $lte: now } }, // Jobs finished in the past
{ lastFinishedAt: null }, // Jobs explicitly set to null
],
},
],
})
.toArray()
.then((jobs) => {
const updates = jobs.map((jobData) => {
const job = new Job({
pulse: this,
name: jobData.name || '',
data: jobData.data || {},
type: jobData.type || 'normal',
priority: jobData.priority || 'normal',
shouldSaveResult: jobData.shouldSaveResult || false,
attempts: jobData.attempts || 0,
backoff: jobData.backoff,
...jobData,
});

job.computeNextRunAt();

return this._collection.updateOne(
{ _id: job.attrs._id },
{
$set: { nextRunAt: job.attrs.nextRunAt },
$unset: { lockedAt: undefined, lastModifiedBy: undefined, lastRunAt: undefined },
}
);
});

return Promise.all(updates);
})
.then((results) => {
const modifiedCount = results.filter((res) => res.modifiedCount > 0).length;
if (modifiedCount > 0) {
debug('Resumed %d recurring jobs (%s)', modifiedCount, now.toISOString());
debug('resuming unfinished %d jobs(%s)', result.modifiedCount, now.toISOString());
}
});
}
Expand Down

1 comment on commit 545d9d4

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines Statements Branches Functions
Coverage: 70%
71.73% (741/1033) 54.63% (171/313) 65.4% (104/159)

Pulse Test Report

Tests Skipped Failures Errors Time
73 0 💤 0 ❌ 0 🔥 13.918s ⏱️

Please sign in to comment.