@@ -7,7 +7,9 @@ import { QueueConfigResolver } from './queue_config_resolver.js'
77import type { Adapter } from './contracts/adapter.js'
88import type { AdapterFactory , JobFactory , QueueManagerConfig } from './types/main.js'
99
10- const noopInternalOperationWrapper : NonNullable < QueueManagerConfig [ 'internalOperationWrapper' ] > = async ( fn ) => fn ( )
10+ const noopInternalOperationWrapper : NonNullable <
11+ QueueManagerConfig [ 'internalOperationWrapper' ]
12+ > = async ( fn ) => fn ( )
1113const noopExecutionWrapper : NonNullable < QueueManagerConfig [ 'executionWrapper' ] > = async ( fn ) => fn ( )
1214
1315type QueueManagerFakeState = {
@@ -19,6 +21,7 @@ type QueueManagerFakeState = {
1921 internalOperationWrapper ?: QueueManagerConfig [ 'internalOperationWrapper' ]
2022 executionWrapper ?: QueueManagerConfig [ 'executionWrapper' ]
2123 configResolver : QueueConfigResolver
24+ locations : string [ ]
2225 fakeAdapter : FakeAdapter
2326}
2427
@@ -63,6 +66,7 @@ class QueueManagerSingleton {
6366 #internalOperationWrapper?: QueueManagerConfig [ 'internalOperationWrapper' ]
6467 #executionWrapper?: QueueManagerConfig [ 'executionWrapper' ]
6568 #configResolver: QueueConfigResolver = new QueueConfigResolver ( { } )
69+ #locations: string [ ] = [ ]
6670 #fakeState?: QueueManagerFakeState
6771
6872 /**
@@ -103,23 +107,52 @@ class QueueManagerSingleton {
103107 this . #internalOperationWrapper = config . internalOperationWrapper
104108 this . #executionWrapper = config . executionWrapper
105109 this . #configResolver = QueueConfigResolver . from ( config )
110+ this . #locations = config . locations ?? [ ]
106111
107- if ( config . locations && config . locations . length > 0 ) {
108- const registered = await Locator . registerFromGlob ( config . locations )
109-
110- if ( registered === 0 ) {
111- this . #logger. warn (
112- `No jobs found for locations: ${ config . locations . join ( ', ' ) } . ` +
113- 'Verify your glob patterns match your job files.'
114- )
115- }
112+ if ( config . autoLoadJobs ?? true ) {
113+ await this . loadJobs ( )
116114 }
117115
118116 this . #initialized = true
119117
120118 return this
121119 }
122120
121+ /**
122+ * Load and register job classes from configured or explicit locations.
123+ *
124+ * This low-level API is useful for framework integrations that need to
125+ * register jobs at a precise moment in their command lifecycle.
126+ *
127+ * @param locations - Optional glob patterns. Defaults to the configured locations.
128+ * @returns Number of jobs successfully registered.
129+ *
130+ * @example
131+ * ```typescript
132+ * await QueueManager.init(config)
133+ * await QueueManager.loadJobs()
134+ *
135+ * // Or with explicit locations
136+ * await QueueManager.loadJobs(['./app/jobs/**\/*.js'])
137+ * ```
138+ */
139+ async loadJobs ( locations : string [ ] = this . #locations) : Promise < number > {
140+ if ( locations . length === 0 ) {
141+ return 0
142+ }
143+
144+ const registered = await Locator . registerFromGlob ( locations )
145+
146+ if ( registered === 0 ) {
147+ this . #logger. warn (
148+ `No jobs found for locations: ${ locations . join ( ', ' ) } . ` +
149+ 'Verify your glob patterns match your job files.'
150+ )
151+ }
152+
153+ return registered
154+ }
155+
123156 /**
124157 * Destroy any materialized adapters from the current configuration before
125158 * replacing it with a new one.
@@ -254,6 +287,7 @@ class QueueManagerSingleton {
254287 internalOperationWrapper : this . #internalOperationWrapper,
255288 executionWrapper : this . #executionWrapper,
256289 configResolver : this . #configResolver,
290+ locations : this . #locations,
257291 fakeAdapter,
258292 }
259293
@@ -295,6 +329,7 @@ class QueueManagerSingleton {
295329 this . #internalOperationWrapper = state . internalOperationWrapper
296330 this . #executionWrapper = state . executionWrapper
297331 this . #configResolver = state . configResolver
332+ this . #locations = state . locations
298333 }
299334
300335 /**
@@ -399,6 +434,7 @@ class QueueManagerSingleton {
399434 this . #internalOperationWrapper = undefined
400435 this . #executionWrapper = undefined
401436 this . #configResolver = new QueueConfigResolver ( { } )
437+ this . #locations = [ ]
402438 this . #fakeState = undefined
403439 }
404440}
0 commit comments