@@ -38,16 +38,13 @@ export abstract class Job {
3838 let finishedWithError = false ;
3939 const sw = stopwatch ( ) ;
4040
41- // This sql transaction will catch any and all errors that are generated while processing the
42- // job. Each of them were previously tagged as retryable or not retryable so we'll make a
43- // decision here about what to do in each case. If we choose to retry, this queue entry will
44- // simply not be marked as `processed = true` so it can be picked up by the queue at a later
45- // time.
41+ // This block will catch any and all errors that are generated while processing the job. Each of
42+ // them were previously tagged as retryable or not retryable so we'll make a decision here about
43+ // what to do in each case. If we choose to retry, this queue entry will simply not be marked as
44+ // `processed = true` so it can be picked up by the queue at a later time.
4645 try {
47- await this . db . sqlWriteTransaction ( async sql => {
48- await this . handler ( ) ;
49- processingFinished = true ;
50- } ) ;
46+ await this . handler ( ) ;
47+ processingFinished = true ;
5148 } catch ( error ) {
5249 if ( error instanceof RetryableJobError ) {
5350 const retries = await this . db . increaseJobRetryCount ( { id : this . job . id } ) ;
@@ -59,7 +56,7 @@ export abstract class Job {
5956 error ,
6057 `Job ${ this . description ( ) } recoverable error after ${ sw . getElapsed ( ) } ms, trying again later`
6158 ) ;
62- await this . db . updateJobStatus ( { id : this . job . id , status : DbJobStatus . pending } ) ;
59+ await this . updateStatus ( DbJobStatus . pending ) ;
6360 } else {
6461 logger . warn ( error , `Job ${ this . description ( ) } max retries reached, giving up` ) ;
6562 processingFinished = true ;
@@ -74,12 +71,20 @@ export abstract class Job {
7471 } finally {
7572 if ( processingFinished ) {
7673 const status = finishedWithError ? DbJobStatus . failed : DbJobStatus . done ;
77- await this . db . updateJobStatus ( {
78- id : this . job . id ,
79- status : status ,
80- } ) ;
81- logger . info ( `Job ${ this . description ( ) } ${ status } in ${ sw . getElapsed ( ) } ms` ) ;
74+ if ( await this . updateStatus ( status ) ) {
75+ logger . info ( `Job ${ this . description ( ) } ${ status } in ${ sw . getElapsed ( ) } ms` ) ;
76+ }
8277 }
8378 }
8479 }
80+
81+ private async updateStatus ( status : DbJobStatus ) : Promise < boolean > {
82+ try {
83+ await this . db . updateJobStatus ( { id : this . job . id , status : status } ) ;
84+ return true ;
85+ } catch ( error ) {
86+ logger . error ( `Job ${ this . description ( ) } could not update status to ${ status } : ${ error } ` ) ;
87+ return false ;
88+ }
89+ }
8590}
0 commit comments