Skip to content

Commit

Permalink
docs: explain better how to declare jobs in the docs
Browse files Browse the repository at this point in the history
closes #3
  • Loading branch information
acidiney committed Mar 18, 2024
1 parent 2c70f93 commit e0aee37
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 25 deletions.
54 changes: 32 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
1. [Installation](#installation)
2. [Configuration](#configuration)
3. [Usage](#usage)
- [Job Dispatching](#job-dispatching)
- [Job Creation](#job-creation)
- [Job Lifecycle](#job-lifecycle)
- [Job Dispatching](#job-dispatching)
- [Job Creation](#job-creation)
- [Job Lifecycle](#job-lifecycle)
4. [Advanced Features](#advanced-features)
- [Job Attempts and Retries](#job-attempts-and-retries)
- [Running the Queue Worker](#running-the-queue-worker)
6. [Dependencies](#dependencies)
- [Job Attempts and Retries](#job-attempts-and-retries)
- [Running the Queue Worker](#running-the-queue-worker)
5. [Dependencies](#dependencies)

## Installation <a id="installation"></a>

Expand All @@ -37,59 +37,64 @@ node ace configure @acidiney/bull-queue

Utilize the `dispatch` method provided by the `bull` provider to enqueue jobs.
Example:

> Please note that #app is an alia that was created by me, and isn't in adonis by default... So if you want to use it, you will need to add it in your tsconfig and package.json
```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 RegisterStripeCustomer, {
RegisterStripeCustomerPayload,
} from '#app/jobs/register_stripe_customer.js'

await app.booted(async () => {
bull.dispatch(
RegisterStripeCustomer.name,
{ userId: '123456' } as RegisterStripeCustomerPayload,
)
bull.dispatch(RegisterStripeCustomer.name, { userId: '123456' } as RegisterStripeCustomerPayload)
})
```

### Job Creation <a id="job-creation"></a>

Generate new job classes using the `node ace make:job {job}` command.

Example:

```ts
// app/jobs/register_stripe_customer.ts
import app from '@adonisjs/core/services/app'
import { JobHandlerContract, Job } from '@acidiney/bull-queue/types'

export type RegisterStripeCustomerPayload = {
userId: string;
};

export default class RegisterStripeCustomer implements JobHandlerContract<RegisterStripeCustomerPayload> {
userId: string
}

export default class RegisterStripeCustomer
implements JobHandlerContract<RegisterStripeCustomerPayload>
{
public async handle(job: Job<RegisterStripeCustomerPayload>) {
// Logic to register a Stripe customer
const { userId } = job.data;
const { userId } = job.data
// Perform Stripe registration process
}

public async failed(job: Job<RegisterStripeCustomerPayload>) {
// Logic to handle failed job attempts
const { userId } = job.data;
const { userId } = job.data
// Send notification or log failure
}
}
```

Register the new job into `start/jobs.ts`

```ts
// start/jobs.ts
import RegisterStripeCustomer from '#app/jobs/register_stripe_customer'
const jobs: Record<string, Function> = {
() => import('#app/jobs/register_stripe_customer.js'),
[RegisterStripeCustomer.name]: () => import('#app/jobs/register_stripe_customer'),
}

export { jobs }
```


### 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 @@ -104,9 +109,11 @@ Define the `handle` method to execute job logic and the `failed` method to handl
### Running the Queue Worker <a id="running-the-queue-worker"></a>

Initiate the queue worker using the `node ace queue:listen` command.

- Specify queues or run the UI for monitoring and managing queues.

Example:

```bash
node ace queue:listen:ui
```
Expand All @@ -128,14 +135,16 @@ In case you want to start a custom queue, do not forget to name it when you `dis
```ts
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 RegisterStripeCustomer, {
RegisterStripeCustomerPayload,
} from '#app/jobs/register_stripe_customer.js'

await app.booted(async () => {
bull.dispatch(
RegisterStripeCustomer.name,
{ userId: '123456' } as RegisterStripeCustomerPayload,
{
queueName: 'stripe'
queueName: 'stripe',
}
)
})
Expand All @@ -151,4 +160,5 @@ This command starts the queue worker and launches the UI for convenient manageme
- **h3**: A library for generating unique hash codes.

# Author

Acidiney Dias
9 changes: 6 additions & 3 deletions stubs/jobs/main.stub
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
})
}}}

import app from '@adonisjs/core/services/app'
import type { JobHandlerContract, Job } from '@acidiney/bull-queue/types'

export type {{ name }}Payload = {}
Expand All @@ -18,10 +17,14 @@ export default class {{ name }}Job implements JobHandlerContract<{{ name }}Paylo
/**
* Base Entry point
*/
async handle(job: Job<{{ name }}Payload>) {}
async handle(job: Job<{{ name }}Payload>) {
throw new Error("Need to be implemented!")
}

/**
* This is an optional method that gets called if it exists when the retries has exceeded and is marked failed.
*/
async failed() {}
async failed()job: Job<{{ name }}Payload>) {
throw new Error("Need to be implemented!")
}
}

0 comments on commit e0aee37

Please sign in to comment.