88 */
99
1010import { Log } from '@athenna/logger'
11- import { Json , Options } from '@athenna/common'
12- import type { ConnectionOptions } from '#src/types'
11+ import { Is , Json , Options } from '@athenna/common'
12+ import type { Job , ConnectionOptions } from '#src/types'
1313import { ConnectionFactory } from '#src/factories/ConnectionFactory'
14- import { RUN_WITH_WORKER_CONTEXT , type ScopedQueueProcessor } from '#src/drivers/Driver'
14+ import { QueueExecutionScope } from '#src/worker/QueueExecutionScope'
15+ import {
16+ RUN_WITH_WORKER_CONTEXT ,
17+ type ScopedQueueProcessor
18+ } from '#src/drivers/Driver'
1519
1620export class FakeDriver {
1721 public constructor ( connection ?: string , client ?: any ) {
@@ -38,6 +42,7 @@ export class FakeDriver {
3842 public static visibilityTimeout : number = 30000
3943 public static workerInterval : number = 1000
4044 public static noAckDelayMs : number = 1700
45+ public static options ?: ConnectionOptions [ 'options' ]
4146 public static backoff : {
4247 type : 'fixed' | 'exponential'
4348 delay : number
@@ -99,6 +104,7 @@ export class FakeDriver {
99104
100105 this . isConnected = true
101106 this . isSavedOnFactory = options . saveOnFactory
107+ this . options = options . options
102108 }
103109
104110 /**
@@ -225,18 +231,65 @@ export class FakeDriver {
225231 return 0
226232 }
227233
228- protected runScopedQueueProcessor < T > (
234+ /**
235+ * Change the job visibility values in the queue.
236+ */
237+ public static changeJobVisibility ( _jobId : string , _seconds : number ) : Promise < void > {
238+ return Promise . resolve ( )
239+ }
240+
241+ /**
242+ * Send a job to the deadletter queue.
243+ */
244+ public static sendJobToDLQ ( _jobId : string ) : Promise < void > {
245+ return Promise . resolve ( )
246+ }
247+
248+ private static createContextJob < T > ( data : T ) {
249+ if ( this . isJob ( data ) ) {
250+ return data
251+ }
252+
253+ return {
254+ id : null ,
255+ attempts : this . attempts ,
256+ data
257+ } as Job
258+ }
259+
260+ private static isJob ( data : unknown ) : data is Job {
261+ if ( ! data || ! Is . Object ( data ) ) {
262+ return false
263+ }
264+
265+ const candidate = data as Partial < Job >
266+
267+ return 'data' in candidate && 'attempts' in candidate
268+ }
269+
270+ protected static runScopedQueueProcessor < T > (
229271 processor : ScopedQueueProcessor < T > ,
230272 data : T ,
231- callback : ( ) => any | Promise < any >
273+ callback : ( ) => any | Promise < any > ,
274+ captureScope ?: ( scope : QueueExecutionScope < T > ) => void
232275 ) {
233276 const runner = processor [ RUN_WITH_WORKER_CONTEXT ]
234277
235278 if ( runner ) {
236- return runner ( data , callback )
279+ return runner ( data , callback , captureScope )
237280 }
238281
239- return callback ( )
282+ const scope = new QueueExecutionScope < T > ( {
283+ name : this . queueName ,
284+ connection : this . connection ,
285+ options : this . options ,
286+ traceId : null ,
287+ job : this . createContextJob ( data )
288+ } )
289+
290+ captureScope ?.( scope )
291+
292+ return scope . run ( callback )
240293 }
241294
242295 /**
@@ -256,21 +309,23 @@ export class FakeDriver {
256309 ) {
257310 const data = await this . pop ( )
258311
259- try {
260- await processor ( data )
261- } catch ( err ) {
262- Log . channelOrVanilla ( 'exception' ) . error ( {
263- msg : `failed to process job: ${ err . message } ` ,
264- queue : this . queueName ,
265- deadletter : this . deadletter ,
266- name : err . name ,
267- code : err . code ,
268- help : err . help ,
269- details : err . details ,
270- metadata : err . metadata ,
271- stack : err . stack ,
272- job : data
273- } )
274- }
312+ await this . runScopedQueueProcessor ( processor , data , async ( ) => {
313+ try {
314+ await processor ( data )
315+ } catch ( err ) {
316+ Log . channelOrVanilla ( 'exception' ) . error ( {
317+ msg : `failed to process job: ${ err . message } ` ,
318+ queue : this . queueName ,
319+ deadletter : this . deadletter ,
320+ name : err . name ,
321+ code : err . code ,
322+ help : err . help ,
323+ details : err . details ,
324+ metadata : err . metadata ,
325+ stack : err . stack ,
326+ job : data
327+ } )
328+ }
329+ } )
275330 }
276331}
0 commit comments