Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"license": "MIT",
"peerDependencies": {
"fastify": ">=3.24.1",
"bullmq": "^1.59.4"
"bullmq": "^1.59.4 || ^5.0.0"
},
"devDependencies": {
"@types/node": "^16.11.12",
Expand Down
21 changes: 17 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { FastifyInstance } from 'fastify';
import fp from 'fastify-plugin';
import { Queue, Worker, ConnectionOptions } from 'bullmq';
import { Queue, Worker, ConnectionOptions, Job } from 'bullmq';
import * as fg from 'fast-glob';
import path from 'path';

Expand All @@ -20,10 +20,14 @@ const fastifyBullMQ = async (
) => {
const queues = {};
const workers = {};
// Detect BullMQ version by checking if Queue supports certain properties
const isBullMQ5 = Queue.prototype.hasOwnProperty('isPaused');

fastify.log.info(`Using BullMQ version ${isBullMQ5 ? '5.x' : '1.x'} compatibility mode`);

const files = fg.sync(opts.bullPath);

files.forEach(async (filePath) => {
for (const filePath of files) {
const parts = filePath.split('/');
// the queue name is defined by the name of the directory in which the files are
const queueName = parts[parts.length - 2];
Expand All @@ -45,17 +49,26 @@ const fastifyBullMQ = async (
`The queue ${queueName} does not have a worker function`
);
} else {
// Worker implementation compatible with both BullMQ 1.x and 5.x
(workers as any)[queueName] = new Worker(
queueName,
(job) => worker(fastify, job),
async (job: Job) => {
try {
return await worker(fastify, job);
} catch (error: unknown) {
const errorMessage = error instanceof Error ? error.message : String(error);
fastify.log.error(`Error processing job ${job.id} in queue ${queueName}: ${errorMessage}`);
throw error;
}
},
{
connection: opts.connection,
...(workerConfig && workerConfig),
}
);
fastify.log.info(`Created a worker for the queue ${queueName}`);
}
});
}

fastify.decorate('queues', queues);
fastify.decorate('workers', workers);
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"compilerOptions": {
"module": "commonjs",
"lib": ["dom", "esnext"],
"importHelpers": true,
"importHelpers": false,
"types": ["node"],
// output .d.ts declaration files for consumers
"declaration": true,
// output .js.map sourcemap files for consumers
Expand Down