Skip to content

Commit 5cec963

Browse files
committed
refactor!: consolidate type exports under /types path
1 parent 26d8043 commit 5cec963

File tree

5 files changed

+68
-29
lines changed

5 files changed

+68
-29
lines changed

README.md

Lines changed: 63 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Create a job by extending the `Job` class:
2828

2929
```typescript
3030
import { Job } from '@boringnode/queue'
31-
import type { JobOptions } from '@boringnode/queue/types/main'
31+
import type { JobContext, JobOptions } from '@boringnode/queue/types'
3232

3333
interface SendEmailPayload {
3434
to: string
@@ -41,8 +41,12 @@ export default class SendEmailJob extends Job<SendEmailPayload> {
4141
queue: 'email',
4242
}
4343

44+
constructor(payload: SendEmailPayload, context: JobContext) {
45+
super(payload, context)
46+
}
47+
4448
async execute(): Promise<void> {
45-
console.log(`Sending email to: ${this.payload.to}`)
49+
console.log(`[Attempt ${this.context.attempt}] Sending email to: ${this.payload.to}`)
4650
}
4751
}
4852
```
@@ -227,10 +231,10 @@ Schedule jobs to run in the future:
227231

228232
```typescript
229233
// Various time formats
230-
await SendEmailJob.dispatch(payload).in('30s') // 30 seconds
231-
await SendEmailJob.dispatch(payload).in('5m') // 5 minutes
232-
await SendEmailJob.dispatch(payload).in('2h') // 2 hours
233-
await SendEmailJob.dispatch(payload).in('1d') // 1 day
234+
await SendEmailJob.dispatch(payload).in('30s') // 30 seconds
235+
await SendEmailJob.dispatch(payload).in('5m') // 5 minutes
236+
await SendEmailJob.dispatch(payload).in('2h') // 2 hours
237+
await SendEmailJob.dispatch(payload).in('1d') // 1 day
234238
```
235239

236240
## Priority
@@ -295,7 +299,7 @@ export default class LimitedJob extends Job<Payload> {
295299
static readonly jobName = 'LimitedJob'
296300

297301
static options: JobOptions = {
298-
timeout: '30s', // Maximum execution time
302+
timeout: '30s', // Maximum execution time
299303
failOnTimeout: false, // Retry on timeout (default)
300304
}
301305

@@ -315,22 +319,58 @@ const config = {
315319
}
316320
```
317321

322+
## Job Context
323+
324+
Every job has access to execution context via `this.context`. This provides metadata about the current job execution:
325+
326+
```typescript
327+
import { Job } from '@boringnode/queue'
328+
import type { JobContext } from '@boringnode/queue'
329+
330+
export default class MyJob extends Job<Payload> {
331+
constructor(payload: Payload, context: JobContext) {
332+
super(payload, context)
333+
}
334+
335+
async execute(): Promise<void> {
336+
console.log(`Job ID: ${this.context.jobId}`)
337+
console.log(`Attempt: ${this.context.attempt}`) // 1, 2, 3...
338+
console.log(`Queue: ${this.context.queue}`)
339+
console.log(`Priority: ${this.context.priority}`)
340+
console.log(`Acquired at: ${this.context.acquiredAt}`)
341+
342+
if (this.context.attempt > 1) {
343+
console.log('This is a retry!')
344+
}
345+
}
346+
}
347+
```
348+
349+
### Context Properties
350+
351+
| Property | Type | Description |
352+
| -------------- | ------ | ----------------------------------------------- |
353+
| `jobId` | string | Unique identifier for this job |
354+
| `name` | string | Job class name |
355+
| `attempt` | number | Current attempt number (1-based) |
356+
| `queue` | string | Queue name this job is being processed from |
357+
| `priority` | number | Job priority (lower = higher priority) |
358+
| `acquiredAt` | Date | When this job was acquired by the worker |
359+
| `stalledCount` | number | Times this job was recovered from stalled state |
360+
318361
## Dependency Injection
319362

320363
Use the `jobFactory` option to integrate with IoC containers for dependency injection. This allows your jobs to receive injected services in their constructor.
321364

322365
```typescript
323-
import { Worker } from '@boringnode/queue'
324-
import type { JobFactory } from '@boringnode/queue'
366+
import { QueueManager } from '@boringnode/queue'
325367

326-
const worker = new Worker({
368+
await QueueManager.init({
327369
default: 'redis',
328370
adapters: { redis: redis(connection) },
329-
worker: {
330-
jobFactory: async (JobClass, payload) => {
331-
// Use your IoC container to instantiate jobs
332-
return app.container.make(JobClass, [payload])
333-
},
371+
jobFactory: async (JobClass, payload, context) => {
372+
// Use your IoC container to instantiate jobs
373+
return app.container.make(JobClass, [payload, context])
334374
},
335375
})
336376
```
@@ -339,6 +379,7 @@ Example with injected dependencies:
339379

340380
```typescript
341381
import { Job } from '@boringnode/queue'
382+
import type { JobContext } from '@boringnode/queue'
342383

343384
interface SendEmailPayload {
344385
to: string
@@ -350,20 +391,21 @@ export default class SendEmailJob extends Job<SendEmailPayload> {
350391

351392
constructor(
352393
payload: SendEmailPayload,
353-
private mailer: MailerService, // Injected by IoC container
354-
private logger: Logger // Injected by IoC container
394+
context: JobContext,
395+
private mailer: MailerService, // Injected by IoC container
396+
private logger: Logger // Injected by IoC container
355397
) {
356-
super(payload)
398+
super(payload, context)
357399
}
358400

359401
async execute(): Promise<void> {
360-
this.logger.info(`Sending email to ${this.payload.to}`)
402+
this.logger.info(`[Attempt ${this.context.attempt}] Sending email to ${this.payload.to}`)
361403
await this.mailer.send(this.payload)
362404
}
363405
}
364406
```
365407

366-
Without a `jobFactory`, jobs are instantiated with `new JobClass(payload)`.
408+
Without a `jobFactory`, jobs are instantiated with `new JobClass(payload, context)`.
367409

368410
## Job Discovery
369411

@@ -407,7 +449,7 @@ By default, a simple console logger is used that only outputs warnings and error
407449
Performance comparison with BullMQ using realistic jobs (5ms simulated work per job):
408450

409451
| Jobs | Concurrency | @boringnode/queue | BullMQ | Diff |
410-
|------|-------------|-------------------|--------|-------------|
452+
| ---- | ----------- | ----------------- | ------ | ----------- |
411453
| 100 | 1 | 562ms | 596ms | 5.7% faster |
412454
| 100 | 5 | 116ms | 117ms | ~same |
413455
| 100 | 10 | 62ms | 62ms | ~same |

examples/jobs/send_email_job.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { setTimeout } from 'node:timers/promises'
22
import { Job } from '../../src/job.js'
3-
import type { JobOptions } from '../../src/types/main.js'
3+
import type { JobOptions } from '../../src/types/index.js'
44

55
interface SendEmailPayload {
66
to: string
@@ -15,6 +15,6 @@ export default class SendEmailJob extends Job<SendEmailPayload> {
1515

1616
async execute(): Promise<void> {
1717
await setTimeout(1000)
18-
console.log(`Sending email to: ${this.payload.to}`)
18+
console.log(`[Attempt ${this.context.attempt}] Sending email to: ${this.payload.to}`)
1919
}
2020
}

examples/jobs/sync_job.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { setTimeout } from 'node:timers/promises'
22
import { Job } from '../../src/job.js'
3-
import type { JobOptions } from '../../src/types/main.js'
3+
import type { JobOptions } from '../../src/types/index.js'
44

55
interface SyncJobPayload {
66
source: string
@@ -15,6 +15,6 @@ export default class SyncJob extends Job<SyncJobPayload> {
1515

1616
async execute(): Promise<void> {
1717
await setTimeout(1000)
18-
console.log(`Syncing data from source: ${this.payload.source}`)
18+
console.log(`[Job ${this.context.jobId}] Syncing data from source: ${this.payload.source}`)
1919
}
2020
}

index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,3 @@ export {
99
fixedBackoff,
1010
} from './src/strategies/backoff_strategy.js'
1111
export * as errors from './src/exceptions.js'
12-
13-
export type { JobFactory } from './src/types/main.js'

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
".": "./build/index.js",
1212
"./drivers/*": "./build/src/drivers/*.js",
1313
"./contracts/*": "./build/src/contracts/*.js",
14-
"./types": "./build/src/types/index.js",
15-
"./types/*": "./build/src/types/*.js"
14+
"./types": "./build/src/types/index.js"
1615
},
1716
"scripts": {
1817
"benchmark": "node --import=@poppinss/ts-exec benchmark/run.ts",

0 commit comments

Comments
 (0)