Skip to content

Commit

Permalink
feat: add jobs.ts inside start path to preload jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
Acidiney Dias committed Feb 6, 2024
1 parent 18425c6 commit 917f477
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 13 deletions.
23 changes: 15 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ Example:
```typescript
import app from '@adonisjs/core/services/app'
import bull from '@acidiney/bull-queue/services/main'
import { RegisterStripeCustomer, RegisterStripeCustomerPayload } from '#app/jobs/register_stripe_customer.js'
import { RegisterStripeCustomerPayload } from '#app/jobs/register_stripe_customer.js'

await app.booted(async () => {
bull.dispatch(
RegisterStripeCustomer.instance(),
'RegisterStripeCustomer',
{ userId: '123456' } as RegisterStripeCustomerPayload,
)
})
Expand Down Expand Up @@ -77,14 +77,18 @@ export class RegisterStripeCustomer implements JobHandlerContract<RegisterStripe
const { userId } = job.data;
// Send notification or log failure
}
}

public static instance (): 'RegisterStripeCustomer' {
app.container.singleton('RegisterStripeCustomer', () => new RegisterStripeCustomer())
// You need to declare the new job inside `start/jobs.ts` to be preload.
import { RegisterStripeCustomer } from '#app/jobs/register_stripe_customer.js'

return 'RegisterStripeCustomer'
}
const jobs: Record<string, Function> = {
[RegisterStripeCustomer.name]: () => new RegisterStripeCustomer(),
}

export { jobs }


// Define payload types for jobs in the `config/queue.ts` file to ensure type safety and consistency.
declare module '@adonisjs/core/types' {
interface ContainerBindings {
Expand All @@ -93,6 +97,9 @@ declare module '@adonisjs/core/types' {
}
```




### Job Lifecycle <a id="job-lifecycle"></a>

Define the `handle` method to execute job logic and the `failed` method to handle failed attempts.
Expand All @@ -114,7 +121,7 @@ Example:
node ace queue:listen:ui
```

By default, the UI will be accessible at `localhost:9999/ui`. You can specify a different port using the `--port` option:
By default, the UI will be accessible at `localhost:9999/admin`. You can specify a different port using the `--port` option:

```bash
node ace queue:listen:ui --port=3939
Expand All @@ -136,4 +143,4 @@ This command starts the queue worker and launches the UI for convenient manageme
- **h3**: A library for generating unique hash codes.

# Author
Acidiney Dias
Acidiney Dias
1 change: 1 addition & 0 deletions configure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export async function configure(command: ConfigureCommand) {
* Publish config file
*/
await codemods.makeUsingStub(stubsRoot, 'config/queue.stub', {})
await codemods.makeUsingStub(stubsRoot, 'start/jobs.stub', {})

/**
* Register provider
Expand Down
11 changes: 11 additions & 0 deletions providers/queue_provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@ export default class QueueProvider {
})
}

async boot() {
const { jobs } = await import(this.app.startPath('jobs.ts'))
const logger = await this.app.container.make('logger')
const jobNames = Object.keys(jobs)

for (const job of jobNames) {
this.app.container.singleton<any>(job, jobs[job])
logger.info(`[@acidiney/bull-queue] ${job} loaded!`)
}
}

async shutdown() {
const bullQueue = await this.app.container.make('bull_queue')

Expand Down
5 changes: 0 additions & 5 deletions stubs/config/queue.stub
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,3 @@ connection: {
queueNames: ['default'],
queuePrefix: '@bull-'
})

declare module '@adonisjs/core/types' {
interface ContainerBindings {
}
}
6 changes: 6 additions & 0 deletions stubs/start/jobs.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{{{ exports({ to: app.makePath('start', 'jobs.ts') }) }}}

const jobs: Record<string, Function> = {
}

export { jobs }

0 comments on commit 917f477

Please sign in to comment.