Last active
February 21, 2024 20:41
-
-
Save jtomchak/4dedf7a11f9905a69dd254a6d9d670a8 to your computer and use it in GitHub Desktop.
RedwoodJS v7 Server Config with BullMQ & BullBoard
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { createBullBoard } from '@bull-board/api' | |
import { BullMQAdapter } from '@bull-board/api/bullMQAdapter' | |
import { FastifyAdapter } from '@bull-board/fastify' | |
import { createServer } from '@redwoodjs/api-server' | |
import { logger } from 'src/lib/logger' | |
// (↓) Enable bullMQ worker processing | |
import 'src/queue/worker' | |
import { defaultQueue } from 'src/queue/index' | |
async function main() { | |
const server = await createServer({ | |
logger, | |
}) | |
/** | |
* auto compress larger than 1024Mb | |
*/ | |
await server.register(import('@fastify/compress'), { | |
global: true, | |
threshold: 1024, | |
encodings: ['deflate', 'gzip'], | |
}) | |
/** | |
* Rate limit by IP to 1K within 5 minutes | |
*/ | |
await server.register(import('@fastify/rate-limit'), { | |
max: 1_000, | |
timeWindow: '5 minutes', | |
}) | |
/** | |
* BullMQ Bullboard | |
*/ | |
const serverAdapter = new FastifyAdapter() | |
createBullBoard({ | |
queues: [new BullMQAdapter(defaultQueue)], | |
serverAdapter, | |
}) | |
serverAdapter.setBasePath('/bullmq') | |
await server.register(serverAdapter.registerPlugin(), { | |
prefix: '/bullmq', | |
basePath: '/bullmq', | |
}) | |
await server.start() | |
} | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// src/queue/worker | |
import { Worker } from 'bullmq' | |
import { welcomeMessage, redisOptions } from './index.js' | |
const jobHandlers = { | |
welcomeMessage: welcomeMessage, | |
} | |
const processJob = async (job) => { | |
const handler = jobHandlers[job.name] | |
if (handler) { | |
console.log(`Processing job: ${job.name}`) | |
await handler(job) | |
} | |
} | |
const worker = new Worker('defaultQueue', processJob, { | |
connection: redisOptions, | |
}) | |
worker.on('completed', (job) => { | |
console.log(`${job.id} has completed!`) | |
}) | |
worker.on('failed', (job, err) => { | |
console.log(`${job.id} has failed with ${err.message}`) | |
}) | |
console.log('Worker started!') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment