@@ -5,31 +5,24 @@ import { AppLoggerService } from '@/common/app-logger/app-logger.service';
55import { env } from '@/env' ;
66import { RedisService } from '../redis/redis.service' ;
77import { BullMQPrometheusService } from '@/common/monitoring/bullmq-prometheus.service' ;
8- import type { BullBoardInjectedInstance } from './types' ;
8+ import type { BullBoardInjectedInstance , IQueueService , QueueOptions } from './types' ;
99import { QueueBullboardService } from './queue-bullboard.service' ;
1010
11- export interface QueueOptions < T = any > {
12- name : string ;
13- concurrency ?: number ;
14- jobOptions ?: {
15- attempts ?: number ;
16- backoff ?: {
17- type : 'exponential' | 'fixed' ;
18- delay : number ;
19- } ;
20- removeOnComplete ?: boolean | number | { count : number ; age : number } ;
21- removeOnFail ?: boolean | number | { count : number ; age : number } ;
22- priority ?: number ;
23- } ;
24- }
25-
2611@Injectable ( )
27- export class QueueService implements OnModuleDestroy {
12+ export class BullMQQueueService implements OnModuleDestroy , IQueueService {
2813 private redisClient : IORedis | null ;
2914 private queues : Map < string , Queue > = new Map ( ) ;
3015 private workers : Map < string , Worker > = new Map ( ) ;
3116 private readonly shouldProcessJobs : boolean ;
3217
18+ async addJob < T = any > ( queueName : string , job_name : string , data : T , opts ?: any ) : Promise < any > {
19+ const queue = this . getQueue ( queueName ) ;
20+
21+ return queue . add ( job_name , data , {
22+ priority : opts ?. priority ,
23+ } ) ;
24+ }
25+
3326 constructor (
3427 private readonly logger : AppLoggerService ,
3528 private readonly redisService : RedisService ,
@@ -58,40 +51,16 @@ export class QueueService implements OnModuleDestroy {
5851 return this . shouldProcessJobs ;
5952 }
6053
61- private getQueue < T = any > ( options : QueueOptions < T > ) : Queue < T > {
54+ public getQueue ( queueName : string ) : Queue {
6255 if ( ! this . redisClient ) {
6356 throw new Error ( 'Redis client not initialized' ) ;
6457 }
6558
66- if ( this . queues . has ( options . name ) ) {
67- return this . queues . get ( options . name ) as unknown as Queue < T > ;
68- }
69-
70- const queue = new Queue < T > ( options . name , {
71- connection : this . redisClient as any ,
72- defaultJobOptions : {
73- attempts : options . jobOptions ?. attempts ?? 3 ,
74- backoff : options . jobOptions ?. backoff ?? {
75- type : options . jobOptions ?. backoff ?. type ?? 'exponential' ,
76- delay : options . jobOptions ?. backoff ?. delay ?? 5000 ,
77- } ,
78- removeOnComplete : options . jobOptions ?. removeOnComplete ?? { count : 100 , age : 3600 * 24 } ,
79- removeOnFail : options . jobOptions ?. removeOnFail ?? false ,
80- } ,
81- } ) ;
82-
83- this . queues . set ( options . name , queue as Queue ) ;
84- this . logger . log ( `Queue created: ${ options . name } ` ) ;
85-
86- if ( this . bullMQPrometheusService ) {
87- this . bullMQPrometheusService . registerQueue ( queue ) ;
88- }
89-
90- if ( this . shouldProcessJobs && this . bullBoard && this . queueBullboardService ) {
91- this . queueBullboardService . registerQueue ( this . bullBoard , queue ) ;
59+ if ( this . queues . has ( queueName ) ) {
60+ return this . queues . get ( queueName ) as Queue ;
9261 }
9362
94- return queue ;
63+ throw new Error ( `Queue with name ' ${ queueName } ' does not exist. Please create it first.` ) ;
9564 }
9665
9766 registerWorker < T = any > (
@@ -115,8 +84,8 @@ export class QueueService implements OnModuleDestroy {
11584 return ;
11685 }
11786
118- const worker = new Worker < T , any , string > ( queueName , processor , {
119- connection : this . redisClient as any ,
87+ const worker = new Worker ( queueName , processor , {
88+ connection : this . redisClient ,
12089 concurrency : options . concurrency ?? 1 ,
12190 autorun : true ,
12291 } ) ;
@@ -139,25 +108,25 @@ export class QueueService implements OnModuleDestroy {
139108 } ) ;
140109 } ) ;
141110
142- this . workers . set ( queueName , worker as unknown as Worker < T , any , string > ) ;
111+ this . workers . set ( queueName , worker ) ;
143112 this . logger . log ( `Worker registered for queue: ${ queueName } ` ) ;
144113 }
145114
146- createQueue < T = any > ( queueName : string , options ?: QueueOptions < T > ) : void {
115+ createQueue ( queueName : string , options ?: QueueOptions ) : void {
147116 if ( this . queues . has ( queueName ) ) {
148117 return ;
149118 }
150119
151- const queue = new Queue < T , any , string > ( queueName , {
152- connection : this . redisClient as any ,
120+ const queue = new Queue ( queueName , {
121+ connection : this . redisClient as IORedis ,
153122 defaultJobOptions : options ?. jobOptions ?? {
154123 attempts : 3 ,
155124 backoff : { type : 'exponential' , delay : 5000 } ,
156125 removeOnComplete : { count : 100 , age : 3600 * 24 } ,
157126 removeOnFail : false ,
158127 } ,
159128 } ) ;
160- this . queues . set ( queueName , queue as Queue ) ;
129+ this . queues . set ( queueName , queue ) ;
161130 this . logger . log ( `Queue created: ${ queueName } ` ) ;
162131
163132 if ( this . bullMQPrometheusService ) {
@@ -182,41 +151,36 @@ export class QueueService implements OnModuleDestroy {
182151 }
183152
184153 async setupJobScheduler < T = any > (
185- queue : Queue ,
154+ queueName : string ,
186155 schedulerId : string ,
187- options : {
188- every : number ;
189- data ?: T ;
190- jobName ?: string ;
191- jobOptions ?: {
192- attempts ?: number ;
193- backoff ?: {
194- type : 'exponential' | 'fixed' ;
195- delay : number ;
196- } ;
197- } ;
156+ scheduleOpts : { every : number } ,
157+ jobOpts : {
158+ name : string ;
159+ data : T ;
160+ opts ?: any ;
198161 } ,
199- ) {
162+ ) : Promise < any > {
200163 try {
201- const jobName = options . jobName || 'scheduled-job' ;
164+ const queue = this . getQueue ( queueName ) ;
165+ const jobName = jobOpts . name ;
202166 const firstJob = await queue . upsertJobScheduler (
203167 schedulerId ,
204- { every : options . every , jobId : schedulerId } ,
168+ { every : scheduleOpts . every , jobId : schedulerId } ,
205169 {
206170 name : jobName ,
207- data : options . data || { timestamp : Date . now ( ) } ,
171+ data : jobOpts . data || { timestamp : Date . now ( ) } ,
208172 opts : {
209- attempts : options . jobOptions ?. attempts || 10 ,
210- backoff : options . jobOptions ?. backoff || {
173+ attempts : jobOpts . opts ?. attempts || 3 ,
174+ backoff : jobOpts . opts ?. backoff || {
211175 type : 'exponential' ,
212- delay : 10000 ,
176+ delay : 3000 ,
213177 } ,
214178 } ,
215179 } ,
216180 ) ;
217181 this . logger . log ( `Created job scheduler: ${ schedulerId } ` , {
218182 schedulerId,
219- every : options . every ,
183+ every : scheduleOpts . every ,
220184 jobId : firstJob ?. id ,
221185 } ) ;
222186
0 commit comments