A plugin for implementing Bee Queue job/task queues in fastify applications.
npm i @autotelic/fastify-bee-queue
// Server.js
const Fastify = require('fastify')
const { fastifyBeeQueue } = require('@autotelic/fastify-bee-queue')
const fastify = Fastify()
const QUEUE_NAME = 'some-queue-name'
fastify.register(fastifyBeeQueue, {
redis: 'redis://127.0.0.1',
})
fastify.register(async (fastify, opts) => {
// Create a Queue instance. By default each Queue will create a connection
// to redis.
await fastify.bq.createProducer(QUEUE_NAME)
})
fastify.post('/queue', async (request, response) => {
const { queues } = fastify.bq
// Access the Queue Instance
const q = queues[QUEUE_NAME]
const { body } = request
const { x, y } = body
// Create and save a Job in the Queue.
q.createJob({ x: parseInt(x), y: parseInt(y) }).save()
response.send('Scheduled Job')
})
fastify.listen(3000)
// worker.js
// The workerBees function provides a convenience wrapper for creating
// and configuring Queue instances which will process jobs.
const { workerBees } = require('@autotelic/fastify-bee-queue')
const QUEUE_NAME = 'some-queue-name'
// The worker Queue instances to be created.
const workers = [
{
name: QUEUE_NAME,
processor: async (job) => job.data.x + job.data.y,
options: {}
}
]
// Base configuration applied to all worker Queues.
const queueOptions = {
redis: 'redis://127.0.0.1',
}
const { start } = workerBees({ workers, queueOptions })
// Start the workers and begin processing jobs. Start also returns a promise
// which resolves to an array of the running worker Queues.
const queues = await start()
We provide the following usage examples and recipes:
namespace
: The namespace that the plugin decorators will be added to. Defaults tobq
All remaining options will be passed to the Queue instances when they are created. For more details please refer to the Bee Queue settings documentation
The plugin exposes the following as decorators under the configured namespace.
By default this namespace is bq
creates a "Producer" Queue instance. producer Queues are unable to process jobs or receive messages. They are primarily used to add jobs to a Queue.
name
: The name of the Queue instance. This is also the lookup key in the queues
object.
opts
: The Queue instance settings.
Creates a worker Queue instance. This queue can process jobs and send/receive messages.
name
: The name of the Queue instance. This is also the lookup key in the queues
object.
opts
: The Queue instance settings.
The key/value store where all created Queue instances are stored. They may be accessed by queue name. The plugin will throw an error if duplicate keys are added to the queues object.
Utilities for creating bee-queue workers.
Creates and starts bee-queue workers for processing jobs.
configuration
The configuration object for worker bees, expects fields:queueOptions
: The Queue settings) which will be applied to all worker Queues.workers: Object
- A worker configuration Object which contains fields:name: string
- The Name of the queueprocessor: () => Promise<Any>
- An async method to process jobs from the queue.options: Object
- The Queue settings which will be applied only to this worker Queue.
- Trigger the release workflow via release tag
git checkout main && git pull npm version { minor | major | path } git push --follow-tags
This project is covered under the MIT license.