@recordset/adonis6-bullmq
v1.2.0
Published
BullMQ provider for AdonisJS 6
Maintainers
Readme
@recordset/adonis6-bullmq
BullMQ provider for AdonisJS 6. Effortless background jobs and queue processing for modern AdonisJS apps.
Features
- BullMQ integration (v5+)
- AdonisJS 6 provider, DI, and ace commands
- Dynamic job auto-loading from
app/jobs - Concurrency, retries, and job options
- TypeScript-first, ESM-ready
Adonis6 BullMQ Setup
- Install dependencies:
npm install @recordset/adonis6-bullmq bullmq - Configure BullMQ:
node ace configure @recordset/adonis6-bullmq - Edit
config/bullmq.tsas needed. - Generate jobs with:
node ace make:job MyJob - Start the worker:
node ace bull:listen
Create a job class
app/jobs/email_job.ts:
import { BaseJob } from '@recordset/adonis6-bullmq/job'
export default class EmailJob extends BaseJob {
static get jobName() {
return 'MyJob'
}
async handle(context: JobContext): Promise<any> {
const { data, job } = context
// job.name
console.log('Job name:', job.name)
// Send email logic
console.log('Sending email to', data.email)
}
}Add a job from anywhere in your app
import bullMQManager from '#services/bullmq_manager'
const emailQueue = bullMQManager.queue('EmailJob')
// Add job normally
await emailQueue.add('email_job', { email: '[email protected]' })
// Add job with delay (run after 10 seconds)
await emailQueue.add('email_job', { email: '[email protected]' }, { delay: 10000 })
// Add job with repeat (run every day at 8:00 AM)
await emailQueue.add('email_job', { email: '[email protected]' }, { repeat: { cron: '0 8 * * *' } })
// Add job with custom attempts and backoff
await emailQueue.add(
'email_job',
{ email: '[email protected]' },
{ attempts: 5, backoff: { type: 'exponential', delay: 5000 } }
)Listen for job events
// Listen for job events (QueueEvents)
import bullMQManager from '#services/bullmq_manager'
const queueEvents = bullMQManager.events('EmailJob')
queueEvents.on('completed', ({ jobId }) => {
console.log('Email job completed:', jobId)
})
queueEvents.on('failed', ({ jobId, failedReason }) => {
console.error('Email job failed:', jobId, failedReason)
})