@@ -16,12 +16,14 @@ export const run: RunMethod = async function (this: Job) {
16
16
17
17
return new Promise ( async ( resolve , reject ) => {
18
18
this . attrs . lastRunAt = new Date ( ) ;
19
- this . attrs . runCount = ( this . attrs . runCount || 0 ) + 1 ;
19
+
20
+ const previousRunAt = this . attrs . nextRunAt ;
20
21
debug ( '[%s:%s] setting lastRunAt to: %s' , this . attrs . name , this . attrs . _id , this . attrs . lastRunAt . toISOString ( ) ) ;
21
22
this . computeNextRunAt ( ) ;
22
23
await this . save ( ) ;
23
24
24
25
let finished = false ;
26
+ let resumeOnRestartSkipped = false ;
25
27
const jobCallback = async ( error ?: Error , result ?: unknown ) => {
26
28
// We don't want to complete the job multiple times
27
29
if ( finished ) {
@@ -33,11 +35,13 @@ export const run: RunMethod = async function (this: Job) {
33
35
if ( error ) {
34
36
this . fail ( error ) ;
35
37
} 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 ;
38
41
39
- if ( this . attrs . shouldSaveResult && result ) {
40
- this . attrs . result = result ;
42
+ if ( this . attrs . shouldSaveResult && result ) {
43
+ this . attrs . result = result ;
44
+ }
41
45
}
42
46
}
43
47
@@ -81,6 +85,15 @@ export const run: RunMethod = async function (this: Job) {
81
85
throw new JobError ( 'Undefined job' ) ;
82
86
}
83
87
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
+
84
97
if ( definition . fn . length === 2 ) {
85
98
debug ( '[%s:%s] process function being called' , this . attrs . name , this . attrs . _id ) ;
86
99
await definition . fn ( this , jobCallback ) ;
0 commit comments