@@ -3,25 +3,17 @@ import debug from './debug.js'
33import { Locator } from './locator.js'
44import { consoleLogger , type Logger } from './logger.js'
55import { FakeAdapter } from './drivers/fake_adapter.js'
6+ import { QueueConfigResolver } from './queue_config_resolver.js'
67import type { Adapter } from './contracts/adapter.js'
7- import type {
8- AdapterFactory ,
9- JobFactory ,
10- JobOptions ,
11- QueueConfig ,
12- QueueManagerConfig ,
13- RetryConfig ,
14- } from './types/main.js'
8+ import type { AdapterFactory , JobFactory , QueueManagerConfig } from './types/main.js'
159
1610type QueueManagerFakeState = {
1711 defaultAdapter : string
1812 adapters : Record < string , AdapterFactory >
1913 adapterInstances : Map < string , Adapter >
20- globalRetryConfig ?: RetryConfig
21- globalJobOptions ?: JobOptions
22- queueConfigs : Map < string , QueueConfig >
2314 logger : Logger
2415 jobFactory ?: JobFactory
16+ configResolver : QueueConfigResolver
2517 fakeAdapter : FakeAdapter
2618}
2719
@@ -61,11 +53,9 @@ class QueueManagerSingleton {
6153 #defaultAdapter! : string
6254 #adapters: Record < string , AdapterFactory > = { }
6355 #adapterInstances: Map < string , Adapter > = new Map ( )
64- #globalRetryConfig?: RetryConfig
65- #globalJobOptions?: JobOptions
66- #queueConfigs: Map < string , QueueConfig > = new Map ( )
6756 #logger: Logger = consoleLogger
6857 #jobFactory?: JobFactory
58+ #configResolver: QueueConfigResolver = new QueueConfigResolver ( { } )
6959 #fakeState?: QueueManagerFakeState
7060
7161 /**
@@ -101,16 +91,9 @@ class QueueManagerSingleton {
10191
10292 this . #defaultAdapter = config . default
10393 this . #adapters = config . adapters
104- this . #globalRetryConfig = config . retry
105- this . #globalJobOptions = config . defaultJobOptions
10694 this . #logger = config . logger ?? consoleLogger
10795 this . #jobFactory = config . jobFactory
108-
109- if ( config . queues ) {
110- for ( const [ queue , queueConfig ] of Object . entries ( config . queues ) ) {
111- this . #queueConfigs. set ( queue , queueConfig as QueueConfig )
112- }
113- }
96+ this . #configResolver = QueueConfigResolver . from ( config )
11497
11598 if ( config . locations && config . locations . length > 0 ) {
11699 const registered = await Locator . registerFromGlob ( config . locations )
@@ -216,11 +199,9 @@ class QueueManagerSingleton {
216199 defaultAdapter : this . #defaultAdapter,
217200 adapters : this . #adapters,
218201 adapterInstances : this . #adapterInstances,
219- globalRetryConfig : this . #globalRetryConfig,
220- globalJobOptions : this . #globalJobOptions,
221- queueConfigs : this . #queueConfigs,
222202 logger : this . #logger,
223203 jobFactory : this . #jobFactory,
204+ configResolver : this . #configResolver,
224205 fakeAdapter,
225206 }
226207
@@ -257,44 +238,9 @@ class QueueManagerSingleton {
257238 this . #defaultAdapter = state . defaultAdapter
258239 this . #adapters = state . adapters
259240 this . #adapterInstances = state . adapterInstances
260- this . #globalRetryConfig = state . globalRetryConfig
261- this . #globalJobOptions = state . globalJobOptions
262- this . #queueConfigs = state . queueConfigs
263241 this . #logger = state . logger
264242 this . #jobFactory = state . jobFactory
265- }
266-
267- /**
268- * Get the merged retry configuration for a job.
269- *
270- * Configuration is merged with priority: job > queue > global.
271- * This allows specific jobs or queues to override global defaults.
272- *
273- * @param queue - The queue name
274- * @param jobRetryConfig - Optional job-level retry config
275- * @returns The merged retry configuration
276- *
277- * @example
278- * ```typescript
279- * // Global: maxRetries=3, Queue: maxRetries=5, Job: maxRetries=1
280- * // Result: maxRetries=1 (job wins)
281- * const config = QueueManager.getMergedRetryConfig('emails', { maxRetries: 1 })
282- * ```
283- */
284- getMergedRetryConfig ( queue : string , jobRetryConfig ?: RetryConfig ) : RetryConfig {
285- const queueConfig = this . #queueConfigs. get ( queue )
286- const queueRetryConfig = queueConfig ?. retry || { }
287-
288- let maxRetries =
289- jobRetryConfig ?. maxRetries ??
290- queueRetryConfig . maxRetries ??
291- this . #globalRetryConfig?. maxRetries ??
292- 0
293-
294- let backoff =
295- jobRetryConfig ?. backoff || queueRetryConfig . backoff || this . #globalRetryConfig?. backoff
296-
297- return { maxRetries, backoff }
243+ this . #configResolver = state . configResolver
298244 }
299245
300246 /**
@@ -307,22 +253,14 @@ class QueueManagerSingleton {
307253 }
308254
309255 /**
310- * Get the merged job options for a job (priority: job > queue > global) .
256+ * Get the resolver responsible for effective queue/ job runtime config .
311257 */
312- getMergedJobOptions ( queue : string , jobOptions ?: JobOptions ) : JobOptions {
313- const queueConfig = this . #queueConfigs. get ( queue )
314- const queueJobOptions = queueConfig ?. defaultJobOptions
315-
316- return {
317- removeOnComplete :
318- jobOptions ?. removeOnComplete ??
319- queueJobOptions ?. removeOnComplete ??
320- this . #globalJobOptions?. removeOnComplete ,
321- removeOnFail :
322- jobOptions ?. removeOnFail ??
323- queueJobOptions ?. removeOnFail ??
324- this . #globalJobOptions?. removeOnFail ,
258+ getConfigResolver ( ) : QueueConfigResolver {
259+ if ( ! this . #initialized) {
260+ throw new errors . E_QUEUE_NOT_INITIALIZED ( )
325261 }
262+
263+ return this . #configResolver
326264 }
327265
328266 #validateConfig( config : QueueManagerConfig ) : void {
@@ -376,6 +314,7 @@ class QueueManagerSingleton {
376314
377315 this . #adapterInstances. clear ( )
378316 this . #initialized = false
317+ this . #configResolver = new QueueConfigResolver ( { } )
379318 this . #fakeState = undefined
380319 }
381320}
0 commit comments