@@ -25,29 +25,39 @@ export const resumeOnRestart: ResumeOnRestartMethod = function (this: Pulse, res
2525 . updateMany (
2626 {
2727 $and : [
28- { repeatInterval : { $exists : false } } ,
29- { repeatAt : { $exists : false } } ,
28+ { repeatInterval : { $exists : false } } , // Ensure the job is not recurring (no repeatInterval)
29+ { repeatAt : { $exists : false } } , // Ensure the job is not recurring (no repeatAt)
3030 {
3131 $or : [
3232 {
33- lockedAt : { $exists : true } ,
34- $or : [
35- { nextRunAt : { $lte : now , $ne : null } } ,
36- { nextRunAt : { $exists : false } } ,
37- { nextRunAt : null } ,
38- ] ,
39- $or : [
40- { $expr : { $eq : [ '$runCount' , '$finishedCount' ] } } ,
41- { $or : [ { lastFinishedAt : { $exists : false } } , { lastFinishedAt : null } ] } ,
33+ lockedAt : { $exists : true } , // Locked jobs (interrupted or in-progress)
34+ $and : [
35+ {
36+ $or : [
37+ { nextRunAt : { $lte : now , $ne : null } } , // Overdue jobs
38+ { nextRunAt : { $exists : false } } , // Jobs missing nextRunAt
39+ { nextRunAt : null } , // Jobs explicitly set to null
40+ ] ,
41+ } ,
42+ {
43+ $or : [
44+ { $expr : { $eq : [ '$runCount' , '$finishedCount' ] } } , // Jobs finished but stuck due to locking
45+ { $or : [ { lastFinishedAt : { $exists : false } } , { lastFinishedAt : null } ] } , // Jobs that were not finished
46+ ] ,
47+ } ,
4248 ] ,
4349 } ,
4450 {
45- lockedAt : { $exists : false } ,
46- $or : [ { lastFinishedAt : { $exists : false } } , { lastFinishedAt : null } ] ,
47- $or : [
48- { nextRunAt : { $lte : now , $ne : null } } ,
49- { nextRunAt : { $exists : false } } ,
50- { nextRunAt : null } ,
51+ lockedAt : { $exists : false } , // Unlocked jobs (not in-progress)
52+ $and : [
53+ {
54+ $or : [
55+ { nextRunAt : { $lte : now , $ne : null } } , // Overdue jobs
56+ { nextRunAt : { $exists : false } } , // Jobs missing nextRunAt
57+ { nextRunAt : null } , // Jobs explicitly set to null
58+ ] ,
59+ } ,
60+ { $or : [ { lastFinishedAt : { $exists : false } } , { lastFinishedAt : null } ] } , // Jobs not finished
5161 ] ,
5262 } ,
5363 ] ,
@@ -69,8 +79,21 @@ export const resumeOnRestart: ResumeOnRestartMethod = function (this: Pulse, res
6979 this . _collection
7080 . find ( {
7181 $and : [
72- { $or : [ { repeatInterval : { $exists : true } } , { repeatAt : { $exists : true } } ] } ,
73- { $or : [ { nextRunAt : { $lte : now } } , { nextRunAt : { $exists : false } } , { nextRunAt : null } ] } ,
82+ { $or : [ { repeatInterval : { $exists : true } } , { repeatAt : { $exists : true } } ] } , // Recurring jobs
83+ {
84+ $or : [
85+ { nextRunAt : { $lte : now } } , // Overdue jobs
86+ { nextRunAt : { $exists : false } } , // Jobs missing nextRunAt
87+ { nextRunAt : null } , // Jobs explicitly set to null
88+ ] ,
89+ } ,
90+ {
91+ $or : [
92+ { lastFinishedAt : { $exists : false } } , // Jobs never run
93+ { lastFinishedAt : { $lte : now } } , // Jobs finished in the past
94+ { lastFinishedAt : null } , // Jobs explicitly set to null
95+ ] ,
96+ } ,
7497 ] ,
7598 } )
7699 . toArray ( )
0 commit comments