Use bull queues from your fastify application. Utilize fastify decorators to access shared connections, logger etc.
npm install fastify-bull --save
yarn add fastify-bull
An example queue handler.
// queues/my-queue/index.js
export const name = 'my-queue';
export async function handler(server, job, done){
/* queue processing logic here */
done()
}
- Supports fastify-redis
- Alternatively pass redis connection in options
import fastify from "fastify";
import { Server, IncomingMessage, ServerResponse } from "http";
const server: fastify.FastifyInstance<Server, IncomingMessage, ServerResponse> = fastify({logger:true});
server.register(require('fastify-redis'))
server.register(require('fastify-bull'))
const start = async () => {
try {
await server.listen(3000, "0.0.0.0");
// add an item to the queue
server.queues['my-queue'].add({hello:'world'}, {priority: 1})
} catch (err) {
console.log(err);
server.log.error(err);
process.exit(1);
}
};
process.on("uncaughtException", error => {
console.error(error);
});
process.on("unhandledRejection", error => {
console.error(error);
});
start();
fastify.queues['my-queue'].add({data:'some data'})
If you are testing any app, you need to close created queues.
We recommend follow after tutorial:
Sample code:
const Fastify = require('fastify')
const fp = require('fastify-plugin')
const App = require('../src')
function build (t) {
const app = Fastify()
app.register(fp(App), config())
t.tearDown(async () => {
await app.queues['my-queue'].close()
await app.close()
})
return app
}
module.exports = {
config,
build
}
- path: specify folder where queue handlers are present. Exclude to use the default folder 'queues'
- prefix: Prefix folders to scan for the 'queues'. Default is ['.', 'dist', 'src'].
- connection: Provide connection to Redis. Default assumes that fastify-redis has been registered.
Jerry Thomas Forked from @guivic/fastify-bull by Ludovic Lérus from Guivic
Licensed under MIT.