-
-
Notifications
You must be signed in to change notification settings - Fork 368
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(WIP) Adding Job Scheduler support for BullMQ Queues
- Loading branch information
lpoulin
committed
Nov 27, 2024
1 parent
c47af53
commit 0ec8c82
Showing
81 changed files
with
29,438 additions
and
3,174 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
{ | ||
"version": "0.2.0", | ||
"compounds": [ | ||
{ | ||
"name": "Debug", | ||
"configurations": ["Debug API", "Debug UI"] | ||
} | ||
], | ||
"configurations": [ | ||
{ | ||
"type": "node", | ||
"request": "launch", | ||
"name": "Debug API", | ||
"skipFiles": ["<node_internals>/**"], | ||
"program": "${workspaceFolder}/example.ts", | ||
"runtimeExecutable": "yarn", | ||
"runtimeArgs": ["start:dev:server"], | ||
"outFiles": ["${workspaceFolder}/**/*.js"], | ||
"console": "integratedTerminal" | ||
}, | ||
{ | ||
"type": "node", | ||
"request": "launch", | ||
"name": "Debug UI", | ||
"skipFiles": ["<node_internals>/**"], | ||
"runtimeExecutable": "yarn", | ||
"runtimeArgs": ["lerna", "run", "--stream", "--scope", "@bull-board/ui", "dev"], | ||
"outFiles": ["${workspaceFolder}/packages/ui/dist/**/*.js"], | ||
"console": "integratedTerminal" | ||
} | ||
] | ||
} |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
services: | ||
redis: | ||
image: redis:latest | ||
hostname: redis | ||
restart: unless-stopped | ||
ports: | ||
- 6379:6379 | ||
volumes: | ||
- redis_data:/data | ||
volumes: | ||
redis_data: |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { createBullBoard } from '@bull-board/api'; | ||
import { BullMQAdapter } from '@bull-board/api/bullMQAdapter'; | ||
import { ElysiaAdapter } from '../../node_modules/@bull-board/elysia'; | ||
import { Queue as QueueMQ, Worker } from 'bullmq'; | ||
import Elysia from 'elysia'; | ||
|
||
const sleep = (t: number) => new Promise((resolve) => setTimeout(resolve, t * 1000)); | ||
|
||
const redisOptions = { | ||
port: 6379, | ||
host: 'localhost', | ||
password: '', | ||
}; | ||
|
||
const createQueueMQ = (name: string) => new QueueMQ(name, { connection: redisOptions }); | ||
|
||
function setupBullMQProcessor(queueName: string) { | ||
new Worker( | ||
queueName, | ||
async (job) => { | ||
for (let i = 0; i <= 100; i++) { | ||
await sleep(Math.random()); | ||
await job.updateProgress(i); | ||
await job.log(`Processing job at interval ${i}`); | ||
|
||
if (Math.random() * 200 < 1) throw new Error(`Random error ${i}`); | ||
} | ||
|
||
return { jobId: `This is the return value of job (${job.id})` }; | ||
}, | ||
{ connection: redisOptions } | ||
); | ||
} | ||
|
||
const exampleBullMq = createQueueMQ('BullMQ'); | ||
|
||
setupBullMQProcessor(exampleBullMq.name); | ||
|
||
const serverAdapter = new ElysiaAdapter('/ui'); | ||
|
||
createBullBoard({ | ||
queues: [new BullMQAdapter(exampleBullMq)], | ||
serverAdapter, | ||
}); | ||
|
||
const app = new Elysia() | ||
.onError(({ error, code, request }) => { | ||
console.error(error, code, request.method, request.url); | ||
if (code === 'NOT_FOUND') return 'NOT_FOUND'; | ||
}) | ||
.use(serverAdapter.registerPlugin()) | ||
.get('/add', async ({ query }) => { | ||
await exampleBullMq.add('Add', { title: query.title }); | ||
|
||
return { ok: true }; | ||
}); | ||
|
||
app.listen(3000, ({ port, url }) => { | ||
/* eslint-disable no-console */ | ||
console.log(`Running on ${url.hostname}:${port}...`); | ||
console.log(`For the UI of instance1, open http://localhost:${port}/ui`); | ||
console.log('Make sure Redis is running on port 6379 by default'); | ||
console.log('To populate the queue, run:'); | ||
console.log(` curl http://localhost:${port}/add?title=Example`); | ||
/* eslint-enable no-console */ | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"name": "bull-board-with-elysia", | ||
"type": "module", | ||
"version": "1.0.0", | ||
"description": "Example of how to use Elysia server with bull-board", | ||
"module": "index.ts", | ||
"scripts": { | ||
"dev": "bun --watch index.ts", | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"author": "kravetsone", | ||
"license": "ISC", | ||
"dependencies": { | ||
"@bull-board/elysia": "^5.20.1", | ||
"bullmq": "^5.13.2", | ||
"elysia": "^1.1.24" | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"compilerOptions": { | ||
"lib": ["ESNext", "DOM"], | ||
"module": "ESNext", | ||
"target": "ESNext", | ||
"moduleResolution": "Bundler", | ||
"esModuleInterop": true, | ||
"strict": true, | ||
"skipLibCheck": true, | ||
"allowSyntheticDefaultImports": true, | ||
"noUncheckedIndexedAccess": true, | ||
"verbatimModuleSyntax": true, | ||
"rootDir": "./src", | ||
"noEmit": true | ||
} | ||
} |
Oops, something went wrong.