Skip to content

Commit 658488d

Browse files
committed
fix: improve error handling in QueueManager
1 parent e89f662 commit 658488d

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed

src/exceptions.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,21 @@ export const E_JOB_TIMEOUT = createError<[jobName: string, timeout: number]>(
4444
'The job "%s" has exceeded the timeout of %dms',
4545
'E_JOB_TIMEOUT'
4646
)
47+
48+
export const E_QUEUE_NOT_INITIALIZED = createError(
49+
'QueueManager is not initialized. Call QueueManager.init() before using it.',
50+
'E_QUEUE_NOT_INITIALIZED',
51+
500
52+
)
53+
54+
export const E_ADAPTER_INIT_ERROR = createError<[adapterName: string, originalMessage: string]>(
55+
'Failed to initialize adapter "%s". Reason: %s',
56+
'E_ADAPTER_INIT_ERROR',
57+
500
58+
)
59+
60+
export const E_NO_JOBS_FOUND = createError<[patterns: string]>(
61+
'No jobs found for the specified locations: %s. Verify your glob patterns match your job files.',
62+
'E_NO_JOBS_FOUND',
63+
500
64+
)

src/locator.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ class LocatorSingleton {
1414
this.#registry.set(name, JobClass)
1515
}
1616

17-
async registerFromGlob(patterns: string[]) {
17+
async registerFromGlob(patterns: string[]): Promise<number> {
18+
let registered = 0
19+
1820
for (const pattern of patterns) {
1921
debug('registering jobs from glob pattern: %s', pattern)
2022
for await (const file of glob(pattern)) {
@@ -27,12 +29,15 @@ class LocatorSingleton {
2729

2830
if (JobClass && typeof JobClass === 'function' && JobClass.name) {
2931
this.register(JobClass.name, JobClass)
32+
registered++
3033
}
3134
} catch (error) {
3235
console.warn(`Failed to load job from ${file}:`, error)
3336
}
3437
}
3538
}
39+
40+
return registered
3641
}
3742

3843
get<T extends Job = Job>(name: string): JobClass<T> | undefined {

src/queue_manager.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type { Adapter } from './contracts/adapter.js'
66
import type { AdapterFactory, QueueConfig, QueueManagerConfig, RetryConfig } from './types/main.js'
77

88
class QueueManagerSingleton {
9+
#initialized = false
910
#defaultAdapter!: string
1011
#adapters: Record<string, AdapterFactory> = {}
1112
#adapterInstances: Map<string, Adapter> = new Map()
@@ -32,13 +33,26 @@ class QueueManagerSingleton {
3233
}
3334

3435
if (config.locations && config.locations.length > 0) {
35-
await Locator.registerFromGlob(config.locations)
36+
const registered = await Locator.registerFromGlob(config.locations)
37+
38+
if (registered === 0) {
39+
this.#logger.warn(
40+
`No jobs found for locations: ${config.locations.join(', ')}. ` +
41+
'Verify your glob patterns match your job files.'
42+
)
43+
}
3644
}
3745

46+
this.#initialized = true
47+
3848
return this
3949
}
4050

4151
use(adapter?: string): Adapter {
52+
if (!this.#initialized) {
53+
throw new errors.E_QUEUE_NOT_INITIALIZED()
54+
}
55+
4256
if (!adapter) {
4357
adapter = this.#defaultAdapter
4458
}
@@ -62,9 +76,8 @@ class QueueManagerSingleton {
6276
this.#adapterInstances.set(adapter, instance)
6377
return instance
6478
} catch (error) {
65-
// TODO: Improve error handling
66-
throw new Error()
67-
// throw new errors.E_ADAPTER_ERROR(`Failed to initialize adapter "${adapter}"`, error as Error)
79+
const message = error instanceof Error ? error.message : String(error)
80+
throw new errors.E_ADAPTER_INIT_ERROR([adapter, message])
6881
}
6982
}
7083

@@ -115,6 +128,7 @@ class QueueManagerSingleton {
115128
await adapter.destroy()
116129
}
117130
this.#adapterInstances.clear()
131+
this.#initialized = false
118132
}
119133
}
120134

0 commit comments

Comments
 (0)