adonis-jobs
v0.0.4
Published
This package is available in the npm registry.
Downloads
3
Readme
adonis-jobs
is a queue system based on BullMQ
for AdonisJS.
Note
You must have a Redis server running on your machine.
Getting Started
This package is available in the npm registry.
npm install adonis-jobs
Next, configure the package by running the following command.
node ace configure adonis-jobs
and... Voilà!
Usage
The Queue
provider gives you access to the dispatch
method.
It will dispatch the linked job to the queue with the given payload.
import { Queue } from '@ioc:Setten/Queue';
Queue.dispatch('App/Jobs/RegisterStripeCustomer', {...});
Your Job
can be stored anywhere in your application and is dispatched using its full path.
It must have a handle
method that will be executed by the queue worker.
// app/Jobs/RegisterStripeCustomer.ts
export type RegisterStripeCustomerPayload = {
userId: string
}
export default class RegisterStripeCustomer {
public async handle(payload: RegisterStripeCustomerPayload) {
// ...
}
}
Run the queue worker with the following ace command:
node ace queue:listen
Once done, you will see the message Queue processing started
.
Typings
You can define the payload's type for a given job inside the contracts/queue.ts
file.
import type { RegisterStripeCustomerPayload } from 'App/Jobs/RegisterStripeCustomer'
declare module '@ioc:Setten/Queue' {
interface JobsList {
'App/Jobs/RegisterStripeCustomer': RegisterStripeCustomerPayload;
}
}