-
-
Notifications
You must be signed in to change notification settings - Fork 366
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
341 additions
and
454 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,65 @@ | ||
import { createBullBoard } from "@bull-board/api"; | ||
import { BullMQAdapter } from "@bull-board/api/bullMQAdapter"; | ||
import { ElysiaAdapter } from "@bull-board/elysia"; | ||
import { Queue as QueueMQ, Worker } from "bullmq"; | ||
import Elysia from "elysia"; | ||
import { createBullBoard } from '@bull-board/api'; | ||
import { BullMQAdapter } from '@bull-board/api/bullMQAdapter'; | ||
import { ElysiaAdapter } from '@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 sleep = (t: number) => new Promise((resolve) => setTimeout(resolve, t * 1000)); | ||
|
||
const redisOptions = { | ||
port: 6379, | ||
host: "localhost", | ||
password: "", | ||
port: 6379, | ||
host: 'localhost', | ||
password: '', | ||
}; | ||
|
||
const createQueueMQ = (name: string) => | ||
new QueueMQ(name, { connection: redisOptions }); | ||
const createQueueMQ = (name: string) => new QueueMQ(name, { connection: redisOptions }); | ||
|
||
async 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}`); | ||
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}`); | ||
} | ||
if (Math.random() * 200 < 1) throw new Error(`Random error ${i}`); | ||
} | ||
|
||
return { jobId: `This is the return value of job (${job.id})` }; | ||
}, | ||
{ connection: redisOptions }, | ||
); | ||
return { jobId: `This is the return value of job (${job.id})` }; | ||
}, | ||
{ connection: redisOptions } | ||
); | ||
} | ||
|
||
const exampleBullMq = createQueueMQ("BullMQ"); | ||
const exampleBullMq = createQueueMQ('BullMQ'); | ||
|
||
await setupBullMQProcessor(exampleBullMq.name); | ||
|
||
const serverAdapter = new ElysiaAdapter("/ui"); | ||
const serverAdapter = new ElysiaAdapter('/ui'); | ||
|
||
createBullBoard({ | ||
queues: [new BullMQAdapter(exampleBullMq)], | ||
serverAdapter, | ||
queues: [new BullMQAdapter(exampleBullMq)], | ||
serverAdapter, | ||
}); | ||
|
||
const app = new Elysia() | ||
.use(serverAdapter.registerPlugin()) | ||
.get("/add", async ({ query }) => { | ||
await exampleBullMq.add("Add", { title: query.title }); | ||
.onError(({ error, code, request }) => { | ||
console.error(error, code, request.method, request.url); | ||
}) | ||
.use(serverAdapter.registerPlugin()) | ||
.get('/add', async ({ query }) => { | ||
await exampleBullMq.add('Add', { title: query.title }); | ||
|
||
return { ok: true }; | ||
}); | ||
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 */ | ||
/* 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
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 |
---|---|---|
@@ -1,15 +1,16 @@ | ||
{ | ||
"compilerOptions": { | ||
"lib": ["ESNext"], | ||
"module": "ESNext", | ||
"target": "ESNext", | ||
"moduleResolution": "Bundler", | ||
"esModuleInterop": true, | ||
"strict": true, | ||
"skipLibCheck": true, | ||
"allowSyntheticDefaultImports": true, | ||
"rootDir": "./src", | ||
"noEmit": true | ||
}, | ||
"include": ["src"] | ||
"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.