-
-
Notifications
You must be signed in to change notification settings - Fork 376
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add hono adapter * Update README.md
- Loading branch information
1 parent
92b6edc
commit 56a271c
Showing
11 changed files
with
773 additions
and
0 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,3 @@ | ||
# Hono example | ||
|
||
This example shows how to use [Hono](https://hono.dev) 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,79 @@ | ||
const { createBullBoard } = require('@bull-board/api'); | ||
const { BullMQAdapter } = require('@bull-board/api/bullMQAdapter'); | ||
const { HonoAdapter } = require('@bull-board/hono'); | ||
const { Queue: QueueMQ, Worker } = require('bullmq'); | ||
const { Hono } = require('hono'); | ||
const { showRoutes } = require('hono/dev'); | ||
const { serve } = require('@hono/node-server'); | ||
const { serveStatic } = require('@hono/node-server/serve-static'); | ||
|
||
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) { | ||
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 run = async () => { | ||
const exampleBullMq = createQueueMQ('BullMQ'); | ||
|
||
await setupBullMQProcessor(exampleBullMq.name); | ||
|
||
const app = new Hono(); | ||
|
||
const serverAdapter = new HonoAdapter(serveStatic); | ||
|
||
createBullBoard({ | ||
queues: [new BullMQAdapter(exampleBullMq)], | ||
serverAdapter, | ||
}); | ||
|
||
const basePath = '/ui' | ||
serverAdapter.setBasePath(basePath); | ||
app.route(basePath, serverAdapter.registerPlugin()); | ||
|
||
app.get('/add', async (c) => { | ||
await exampleBullMq.add('Add', { title: c.req.query('title') }); | ||
|
||
return c.json({ ok: true }) | ||
}); | ||
|
||
showRoutes(app); | ||
|
||
serve({ fetch: app.fetch, port: 3000 }, ({ address, port }) => { | ||
/* eslint-disable no-console */ | ||
console.log(`Running on ${address}:${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 */ | ||
}) | ||
}; | ||
|
||
run().catch((e) => { | ||
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-hono", | ||
"version": "1.0.0", | ||
"description": "Example of how to use Hono 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/hono": "../../packages/hono", | ||
"@hono/node-server": "^1.4.0", | ||
"bullmq": "^4.6.0", | ||
"hono": "^3.12.0" | ||
} | ||
} |
Oops, something went wrong.