Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add koa adapter #285

Merged
merged 1 commit into from
Jun 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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