@@ -16,12 +16,14 @@ export const run: RunMethod = async function (this: Job) {
1616
1717 return new Promise ( async ( resolve , reject ) => {
1818 this . attrs . lastRunAt = new Date ( ) ;
19- this . attrs . runCount = ( this . attrs . runCount || 0 ) + 1 ;
19+
20+ const previousRunAt = this . attrs . nextRunAt ;
2021 debug ( '[%s:%s] setting lastRunAt to: %s' , this . attrs . name , this . attrs . _id , this . attrs . lastRunAt . toISOString ( ) ) ;
2122 this . computeNextRunAt ( ) ;
2223 await this . save ( ) ;
2324
2425 let finished = false ;
26+ let resumeOnRestartSkipped = false ;
2527 const jobCallback = async ( error ?: Error , result ?: unknown ) => {
2628 // We don't want to complete the job multiple times
2729 if ( finished ) {
@@ -33,11 +35,13 @@ export const run: RunMethod = async function (this: Job) {
3335 if ( error ) {
3436 this . fail ( error ) ;
3537 } else {
36- this . attrs . lastFinishedAt = new Date ( ) ;
37- this . attrs . finishedCount = ( this . attrs . finishedCount || 0 ) + 1 ;
38+ if ( ! resumeOnRestartSkipped ) {
39+ this . attrs . lastFinishedAt = new Date ( ) ;
40+ this . attrs . finishedCount = ( this . attrs . finishedCount || 0 ) + 1 ;
3841
39- if ( this . attrs . shouldSaveResult && result ) {
40- this . attrs . result = result ;
42+ if ( this . attrs . shouldSaveResult && result ) {
43+ this . attrs . result = result ;
44+ }
4145 }
4246 }
4347
@@ -81,6 +85,15 @@ export const run: RunMethod = async function (this: Job) {
8185 throw new JobError ( 'Undefined job' ) ;
8286 }
8387
88+ if ( ! this . pulse . _resumeOnRestart && previousRunAt && this . pulse . _readyAt >= previousRunAt ) {
89+ debug ( '[%s:%s] job resumeOnRestart skipped' , this . attrs . name , this . attrs . _id ) ;
90+ resumeOnRestartSkipped = true ;
91+ await jobCallback ( undefined , 'skipped' ) ;
92+ return ;
93+ }
94+
95+ this . attrs . runCount = ( this . attrs . runCount || 0 ) + 1 ;
96+
8497 if ( definition . fn . length === 2 ) {
8598 debug ( '[%s:%s] process function being called' , this . attrs . name , this . attrs . _id ) ;
8699 await definition . fn ( this , jobCallback ) ;
0 commit comments