Skip to content

Commit

Permalink
Add koa adapter (#285)
Browse files Browse the repository at this point in the history
  • Loading branch information
felixmosh authored Jun 1, 2021
1 parent dfa15aa commit 1f04bcc
Show file tree
Hide file tree
Showing 10 changed files with 1,265 additions and 17 deletions.
4 changes: 4 additions & 0 deletions examples/with-koa/README.md
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.

87 changes: 87 additions & 0 deletions examples/with-koa/index.js
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);
});
18 changes: 18 additions & 0 deletions examples/with-koa/package.json
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"
}
}
Loading

0 comments on commit 1f04bcc

Please sign in to comment.