@@ -17,12 +17,12 @@ import {
1717 ChangeMessageVisibilityCommand
1818} from '@aws-sdk/client-sqs'
1919
20- import { Log } from '@athenna/logger'
2120import { createHash } from 'node:crypto'
2221import { Driver } from '#src/drivers/Driver'
2322import { Is , Options , Uuid } from '@athenna/common'
2423import type { ConnectionOptions } from '#src/types'
2524import { ConnectionFactory } from '#src/factories/ConnectionFactory'
25+ import { AwsSqsDriverExceptionHandler } from '#src/handlers/AwsSqsDriverExceptionHandler'
2626import { NotFifoSqsQueueTypeException } from '#src/exceptions/NotFifoSqsQueueTypeException'
2727
2828export class AwsSqsDriver extends Driver < SQSClient > {
@@ -39,7 +39,7 @@ export class AwsSqsDriver extends Driver<SQSClient> {
3939 /**
4040 * Convert milliseconds to seconds.
4141 */
42- private msToS ( v : number ) {
42+ public msToS ( v : number ) {
4343 const s = Math . ceil ( v / 1000 )
4444 return Math . max ( 0 , Math . min ( 43200 , s ) )
4545 }
@@ -430,57 +430,21 @@ export class AwsSqsDriver extends Driver<SQSClient> {
430430 this . msToS ( this . noAckDelayMs + requeueJitterMs )
431431 )
432432 }
433- } catch ( err ) {
434- stopHeartbeat ( )
435-
436- const receiveCount = Number (
437- job . metadata . Attributes ?. ApproximateReceiveCount ?? '1'
438- )
439- const attempts = Math . max ( this . attempts - receiveCount , 0 )
440- const shouldRetry = attempts > 0
441-
442- if ( Config . is ( 'worker.logger.prettifyException' ) ) {
443- Log . channelOrVanilla ( 'exception' ) . error (
444- await err . toAthennaException ( ) . prettify ( )
445- )
446- } else {
447- Log . channelOrVanilla ( 'exception' ) . error ( {
448- msg : `failed to process job: ${ err . message } ` ,
449- queue : this . queueName ,
450- deadletter : this . deadletter ,
451- name : err . name ,
452- code : err . code ,
453- help : err . help ,
454- details : err . details ,
455- metadata : err . metadata ,
456- stack : err . stack ,
457- job
458- } )
459- }
460-
461- if ( shouldRetry ) {
462- const delay = this . calculateBackoffDelay ( job . attempts )
463-
464- await this . changeJobVisibility (
465- job . id ,
466- this . msToS ( delay + requeueJitterMs )
467- )
468-
469- return
470- }
471-
472- if ( this . deadletter ) {
473- await this . sendJobToDLQ ( job )
474- }
475-
476- await this . ack ( job . id )
433+ } catch ( error ) {
434+ await new AwsSqsDriverExceptionHandler ( ) . handle ( {
435+ job,
436+ error,
437+ driver : this ,
438+ stopHeartbeat,
439+ requeueJitterMs
440+ } )
477441 }
478442 }
479443
480444 /**
481445 * Send a job to the deadletter quue.
482446 */
483- private async sendJobToDLQ ( job : any ) {
447+ public async sendJobToDLQ ( job : any ) {
484448 if ( Is . Object ( job . data ) ) {
485449 job . data = JSON . stringify ( job . data )
486450 }
@@ -503,7 +467,7 @@ export class AwsSqsDriver extends Driver<SQSClient> {
503467 /**
504468 * Change the job visibility values in SQS.
505469 */
506- private async changeJobVisibility ( id : string , seconds : number ) {
470+ public async changeJobVisibility ( id : string , seconds : number ) {
507471 const cmd = new ChangeMessageVisibilityCommand ( {
508472 QueueUrl : this . queueName ,
509473 ReceiptHandle : id ,
0 commit comments