1.0.1 • Published 5 months ago
elysia-bull v1.0.1
elysia-bull EXPERIMENTAL
A plugin for Elysia.js that integrates Bull for handling background jobs and cron tasks.
Features
- Simple integration with Elysia.js
- Support for background jobs and cron tasks
- Built on top of BullMQ
- Typed API with TypeScript
Installation
npm install elysia-bull
bun add elysia-bull
Usage
Basic setup
import { Elysia } from 'elysia';
import { elysiaBull } from 'elysia-bull';
const app = new Elysia()
.use(
elysiaBull({
connection: {
host: 'localhost',
port: 6379,
},
queues: [
{
name: 'emails',
// Can provide either inline processor function
processor: async (job) => {
console.log(`Processing email job: ${job.id}`);
// Send email logic
return { sent: true };
},
concurrency: 5,
},
{
name: 'reports',
// Or path to processor file (recommended for larger projects)
processorPath: './processors/report-processor.js',
concurrency: 2,
},
],
})
)
.get('/status', ({ bull }) => {
// Get access to bull instance
return { status: 'running' };
})
.post('/send-email', async ({ bull, body }) => {
// Add job to queue
const job = await bull.getQueue('emails').add(body);
return { jobId: job.id };
})
.post('/schedule-report', async ({ bull, body }) => {
// Add recurring job
const job = await bull.getQueue('reports').addCron(
'daily-report',
body,
'0 0 * * *' // Run at midnight every day
);
return { jobId: job.id };
})
.listen(3000);
console.log(`🦊 Server running at ${app.server?.hostname}:${app.server?.port}`);
Processor file example (./processors/report-processor.js)
export default async function (job) {
console.log(`Generating report: ${job.id}`);
// Report generation logic
return { reportGenerated: true };
}
API
elysiaBull(options)
Plugin factory function that accepts the following options:
connection
: Redis connection options (passed to ioredis)queues
: Array of queue configurationsprefix
: Optional prefix for Bull queue keys (default: 'elysia-bull')
QueueConfig
name
: Name of the queueprocessor
: Function to process jobs (optional)processorPath
: Path to processor module (optional, alternative to processor)options
: Bull queue optionsconcurrency
: Number of jobs to process concurrently (default: 1)
BullQueue
Methods available on queue instances:
add(data, options)
: Add a job to the queueaddBulk(jobs)
: Add multiple jobs at onceaddCron(name, data, cronExpression, options)
: Add a recurring job with cron patterngetJob(jobId)
: Get a job by IDgetJobs(status)
: Get jobs by statusremoveRepeatable(name, cronExpression)
: Remove a repeatable jobpause()
: Pause the queueresume()
: Resume the queueclose()
: Close the queue and workers
License
MIT