-
-
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.
- Loading branch information
Showing
10 changed files
with
1,265 additions
and
17 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,4 @@ | ||
# Koa example | ||
|
||
This example shows how to use [Koa.js](https://koajs.com/) as a server for bull-board. | ||
|
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,87 @@ | ||
const { createBullBoard } = require('@bull-board/api'); | ||
const { BullMQAdapter } = require('@bull-board/api/bullMQAdapter'); | ||
const { KoaAdapter } = require('@bull-board/koa'); | ||
const { Queue: QueueMQ, Worker, QueueScheduler } = require('bullmq'); | ||
const Koa = require('koa'); | ||
const Router = require('koa-router'); | ||
|
||
const sleep = (t) => new Promise((resolve) => setTimeout(resolve, t * 1000)); | ||
|
||
const redisOptions = { | ||
port: 6379, | ||
host: 'localhost', | ||
password: '', | ||
tls: false, | ||
}; | ||
|
||
const createQueueMQ = (name) => new QueueMQ(name, { connection: redisOptions }); | ||
|
||
async function setupBullMQProcessor(queueName) { | ||
const queueScheduler = new QueueScheduler(queueName, { | ||
connection: redisOptions, | ||
}); | ||
await queueScheduler.waitUntilReady(); | ||
|
||
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})` }; | ||
}); | ||
} | ||
|
||
const run = async () => { | ||
const exampleBullMq = createQueueMQ('BullMQ'); | ||
|
||
await setupBullMQProcessor(exampleBullMq.name); | ||
|
||
const app = new Koa(); | ||
const router = new Router(); | ||
|
||
const serverAdapter = new KoaAdapter(); | ||
|
||
createBullBoard({ | ||
queues: [new BullMQAdapter(exampleBullMq)], | ||
serverAdapter, | ||
}); | ||
|
||
serverAdapter.setBasePath('/ui'); | ||
await app.use(serverAdapter.registerPlugin()); | ||
|
||
router.get('/add', async (ctx) => { | ||
const opts = ctx.query.opts || {}; | ||
|
||
if (opts.delay) { | ||
opts.delay = +opts.delay * 1000; // delay must be a number | ||
} | ||
|
||
await exampleBullMq.add('Add', { title: ctx.query.title }, opts); | ||
|
||
ctx.body = { | ||
ok: true, | ||
}; | ||
}); | ||
|
||
app.use(router.routes()).use(router.allowedMethods()); | ||
|
||
await app.listen(3000); | ||
// eslint-disable-next-line no-console | ||
console.log('Running on 3000...'); | ||
console.log('For the UI of instance1, open http://localhost:3000/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:3000/add?title=Example'); | ||
console.log('To populate the queue with custom options (opts), run:'); | ||
console.log(' curl http://localhost:3000/add?title=Test&opts[delay]=9'); | ||
}; | ||
|
||
run().catch((e) => { | ||
// eslint-disable-next-line no-console | ||
console.error(e); | ||
process.exit(1); | ||
}); |
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-koa", | ||
"version": "1.0.0", | ||
"description": "Example of how to use Hapi.js server with bull-board", | ||
"main": "index.js", | ||
"scripts": { | ||
"start": "node index.js", | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"author": "felixmosh", | ||
"license": "ISC", | ||
"dependencies": { | ||
"@bull-board/koa": "*", | ||
"bullmq": "^1.24.4", | ||
"koa": "^2.13.1", | ||
"koa-router": "^10.0.0" | ||
} | ||
} |
Oops, something went wrong.