├── .gitattributes ├── .gitignore ├── tsconfig.json ├── README.md ├── src ├── env.ts ├── queue.ts └── index.ts ├── LICENSE └── package.json /.gitattributes: -------------------------------------------------------------------------------- 1 | * text eol=lf 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.log 2 | .DS_Store 3 | node_modules 4 | dist 5 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "@tsconfig/recommended/tsconfig.json", 3 | "include": ["src", "types"], 4 | "compilerOptions": { 5 | "module": "esnext", 6 | "moduleResolution": "node" 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # BullMQ with BullBoard 2 | 3 | [![Deploy on Railway](https://railway.app/button.svg)](https://railway.app/new/template/odzp-I) 4 | 5 | ## ✨ Features 6 | 7 | - A queueing system with BullMQ and Redis 8 | - A dashboard built with `bull-board` 9 | - A Fastify server to trigger jobs via an `/add-job` API endpoint 10 | -------------------------------------------------------------------------------- /src/env.ts: -------------------------------------------------------------------------------- 1 | import { envsafe, port, str } from 'envsafe'; 2 | 3 | export const env = envsafe({ 4 | REDISHOST: str(), 5 | REDISPORT: port(), 6 | REDISUSER: str(), 7 | REDISPASSWORD: str(), 8 | PORT: port({ 9 | devDefault: 3000, 10 | }), 11 | RAILWAY_STATIC_URL: str({ 12 | devDefault: 'http://localhost:3000', 13 | }), 14 | }); 15 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Faraz Patankar 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "queue-service", 3 | "version": "0.1.0", 4 | "license": "MIT", 5 | "author": "Faraz Patankar", 6 | "main": "dist/index.js", 7 | "module": "dist/queue-service.esm.js", 8 | "typings": "dist/index.d.ts", 9 | "files": [ 10 | "dist", 11 | "src" 12 | ], 13 | "scripts": { 14 | "build": "dts build", 15 | "lint": "dts lint", 16 | "watch": "dts watch", 17 | "start": "node dist/index.js" 18 | }, 19 | "husky": { 20 | "hooks": { 21 | "pre-commit": "dts lint" 22 | } 23 | }, 24 | "prettier": { 25 | "printWidth": 80, 26 | "semi": true, 27 | "singleQuote": true, 28 | "trailingComma": "es5" 29 | }, 30 | "peerDependencies": {}, 31 | "engines": { 32 | "node": ">=12" 33 | }, 34 | "devDependencies": { 35 | "@tsconfig/recommended": "^1.0.1", 36 | "dts-cli": "^1.6.0", 37 | "husky": "^8.0.1", 38 | "tslib": "^2.4.0", 39 | "typescript": "^4.8.3" 40 | }, 41 | "dependencies": { 42 | "@bull-board/api": "^4.2.2", 43 | "@bull-board/fastify": "^4.2.2", 44 | "@bull-board/ui": "^4.2.2", 45 | "bullmq": "^1.90.2", 46 | "envsafe": "^2.0.3", 47 | "fastify": "^4.5.3" 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/queue.ts: -------------------------------------------------------------------------------- 1 | import { ConnectionOptions, Queue, QueueScheduler, Worker } from 'bullmq'; 2 | 3 | import { env } from './env'; 4 | 5 | const connection: ConnectionOptions = { 6 | host: env.REDISHOST, 7 | port: env.REDISPORT, 8 | username: env.REDISUSER, 9 | password: env.REDISPASSWORD, 10 | }; 11 | 12 | export const createQueue = (name: string) => new Queue(name, { connection }); 13 | 14 | export const setupQueueProcessor = async (queueName: string) => { 15 | const queueScheduler = new QueueScheduler(queueName, { 16 | connection, 17 | }); 18 | await queueScheduler.waitUntilReady(); 19 | 20 | /** 21 | * This is a dummy worker set up to demonstrate job progress and to 22 | * randomly fail jobs to demonstrate the UI. 23 | * 24 | * In a real application, you would want to set up a worker that 25 | * actually does something useful. 26 | */ 27 | 28 | new Worker( 29 | queueName, 30 | async (job) => { 31 | for (let i = 0; i <= 100; i++) { 32 | await job.updateProgress(i); 33 | await job.log(`Processing job at interval ${i}`); 34 | 35 | if (Math.random() * 200 < 1) throw new Error(`Random error ${i}`); 36 | } 37 | 38 | return { jobId: `This is the return value of job (${job.id})` }; 39 | }, 40 | { connection } 41 | ); 42 | }; 43 | -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | import { createBullBoard } from '@bull-board/api'; 2 | import { BullMQAdapter } from '@bull-board/api/bullMQAdapter'; 3 | import { FastifyAdapter } from '@bull-board/fastify'; 4 | import fastify, { FastifyInstance, FastifyRequest } from 'fastify'; 5 | import { Server, IncomingMessage, ServerResponse } from 'http'; 6 | import { env } from './env'; 7 | 8 | import { createQueue, setupQueueProcessor } from './queue'; 9 | 10 | interface AddJobQueryString { 11 | id: string; 12 | email: string; 13 | } 14 | 15 | const run = async () => { 16 | const welcomeEmailQueue = createQueue('WelcomeEmailQueue'); 17 | await setupQueueProcessor(welcomeEmailQueue.name); 18 | 19 | const server: FastifyInstance = 20 | fastify(); 21 | 22 | const serverAdapter = new FastifyAdapter(); 23 | createBullBoard({ 24 | queues: [new BullMQAdapter(welcomeEmailQueue)], 25 | serverAdapter, 26 | }); 27 | serverAdapter.setBasePath('/'); 28 | server.register(serverAdapter.registerPlugin(), { 29 | prefix: '/', 30 | basePath: '/', 31 | }); 32 | 33 | server.get( 34 | '/add-job', 35 | { 36 | schema: { 37 | querystring: { 38 | type: 'object', 39 | properties: { 40 | title: { type: 'string' }, 41 | id: { type: 'string' }, 42 | }, 43 | }, 44 | }, 45 | }, 46 | (req: FastifyRequest<{ Querystring: AddJobQueryString }>, reply) => { 47 | if ( 48 | req.query == null || 49 | req.query.email == null || 50 | req.query.id == null 51 | ) { 52 | reply 53 | .status(400) 54 | .send({ error: 'Requests must contain both an id and a email' }); 55 | 56 | return; 57 | } 58 | 59 | const { email, id } = req.query; 60 | welcomeEmailQueue.add(`WelcomeEmail-${id}`, { email }); 61 | 62 | reply.send({ 63 | ok: true, 64 | }); 65 | } 66 | ); 67 | 68 | await server.listen({ port: env.PORT, host: '0.0.0.0' }); 69 | console.log( 70 | `To populate the queue and demo the UI, run: curl https://${env.RAILWAY_STATIC_URL}/add-job?id=1&email=hello%40world.com` 71 | ); 72 | }; 73 | 74 | run().catch((e) => { 75 | console.error(e); 76 | process.exit(1); 77 | }); 78 | --------------------------------------------------------------------------------