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 elysia adapter #769

Merged
merged 9 commits into from
Nov 12, 2024
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
96 changes: 50 additions & 46 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ With this library you get a beautiful UI for visualizing what's happening with e
## Packages

| Name | Version |
|--------------------------------------------------------------------------|-------------------------------------------------------------------|
| ------------------------------------------------------------------------ | ----------------------------------------------------------------- |
| [@bull-board/api](https://www.npmjs.com/package/@bull-board/api) | ![npm (scoped)](https://img.shields.io/npm/v/@bull-board/api) |
| [@bull-board/ui](https://www.npmjs.com/package/@bull-board/ui) | ![npm (scoped)](https://img.shields.io/npm/v/@bull-board/ui) |
| [@bull-board/express](https://www.npmjs.com/package/@bull-board/express) | ![npm (scoped)](https://img.shields.io/npm/v/@bull-board/express) |
Expand All @@ -29,6 +29,7 @@ With this library you get a beautiful UI for visualizing what's happening with e
| [@bull-board/nestjs](https://www.npmjs.com/package/@bull-board/nestjs) | ![npm (scoped)](https://img.shields.io/npm/v/@bull-board/nestjs) |
| [@bull-board/hono](https://www.npmjs.com/package/@bull-board/hono) | ![npm (scoped)](https://img.shields.io/npm/v/@bull-board/hono) |
| [@bull-board/h3](https://www.npmjs.com/package/@bull-board/h3) | ![npm (scoped)](https://img.shields.io/npm/v/@bull-board/h3) |
| [@bull-board/elysia](https://www.npmjs.com/package/@bull-board/elysia) | ![npm (scoped)](https://img.shields.io/npm/v/@bull-board/elysia) |

## Notes

Expand Down Expand Up @@ -59,9 +60,12 @@ yarn add @bull-board/nestjs
yarn add @bull-board/hono
# or
yarn add @bull-board/h3
# or
bun add @bull-board/elysia
```

### NestJS specific setup

@bull-board provides a module for easy integration with NestJS, for reference on how to use the module refer to the [NestJS Module](https://github.com/felixmosh/bull-board/tree/master/packages/nestjs) package

## Hello World
Expand Down Expand Up @@ -104,8 +108,8 @@ app.listen(3000, () => {

That's it! Now you can access the `/admin/queues` route, and you will be able to monitor everything that is happening in your queues 😁


For more advanced usages check the `examples` folder, currently it contains:

1. [Basic authentication example](https://github.com/felixmosh/bull-board/tree/master/examples/with-express-auth)
2. [Multiple instance of the board](https://github.com/felixmosh/bull-board/tree/master/examples/with-multiple-instances)
3. [With Fastify server](https://github.com/felixmosh/bull-board/tree/master/examples/with-fastify)
Expand All @@ -114,23 +118,24 @@ For more advanced usages check the `examples` folder, currently it contains:
6. [With Nest.js server using the built-in module](https://github.com/felixmosh/bull-board/tree/master/examples/with-nestjs-module) (Thanx to @dennissnijder)
7. [With Nest.js server using the express adapter](https://github.com/felixmosh/bull-board/tree/master/examples/with-nestjs) (Thanx to @lodi-g)
8. [With Hono server](https://github.com/felixmosh/bull-board/tree/master/examples/with-hono) (Thanks to @nihalgonsalves)
8. [With H3 server using the h3 adapter](https://github.com/felixmosh/bull-board/tree/master/examples/with-h3) (Thanx to @genu)

9. [With H3 server using the h3 adapter](https://github.com/felixmosh/bull-board/tree/master/examples/with-h3) (Thanx to @genu)
10. [With Elysia server using the Elysia adapter](https://github.com/felixmosh/bull-board/tree/master/examples/with-elysia) (Thanx to @genu)

### Board options

1. `uiConfig.boardTitle` (default: `Bull Dashboard`)
The Board and page titles
The Board and page titles
2. `uiConfig.boardLogo` (default: `empty`) `{ path: string; width?: number | string; height?: number | string }`
An object that allows you to specify a different logo
An object that allows you to specify a different logo
3. `uiConfig.miscLinks` (default: `empty`) `Array< { text: string; url: string }>`
An array of misc link that you can add to the dashboard, such as logout link.
An array of misc link that you can add to the dashboard, such as logout link.
4. uiConfig.favIcon (default: `{ default: 'static/images/logo.svg', alternative: 'static/favicon-32x32.png', }`) `{ default: string; alternative: 'string' }`
An object that allows you to specify the default and alternative favicons.
An object that allows you to specify the default and alternative favicons.

```js
const QueueMQ = require('bullmq');
const {createBullBoard} = require('@bull-board/api');
const {BullMQAdapter} = require('@bull-board/api/bullMQAdapter');
const { createBullBoard } = require('@bull-board/api');
const { BullMQAdapter } = require('@bull-board/api/bullMQAdapter');

const queueMQ = new QueueMQ();

Expand All @@ -145,7 +150,7 @@ createBullBoard({
width: '100px',
height: 200,
},
miscLinks: [{text: 'Logout', url: '/logout'}],
miscLinks: [{ text: 'Logout', url: '/logout' }],
favIcon: {
default: 'static/images/logo.svg',
alternative: 'static/favicon-32x32.png',
Expand All @@ -156,45 +161,46 @@ createBullBoard({
```

### Queue options

1. `readOnlyMode` (default: `false`)
Makes the UI as read only, hides all queue & job related actions
Makes the UI as read only, hides all queue & job related actions

```js
const Queue = require('bull')
const QueueMQ = require('bullmq')
const { createBullBoard } = require('@bull-board/api')
const { BullMQAdapter } = require('@bull-board/api/bullMQAdapter')
const { BullAdapter } = require('@bull-board/api/bullAdapter')
const Queue = require('bull');
const QueueMQ = require('bullmq');
const { createBullBoard } = require('@bull-board/api');
const { BullMQAdapter } = require('@bull-board/api/bullMQAdapter');
const { BullAdapter } = require('@bull-board/api/bullAdapter');

const someQueue = new Queue()
const queueMQ = new QueueMQ()
const someQueue = new Queue();
const queueMQ = new QueueMQ();

createBullBoard({
queues: [
new BullAdapter(someQueue, { readOnlyMode: true }), // only this queue will be in read only mode
new BullMQAdapter(queueMQ, { readOnlyMode: true }),
]
})
],
});
```

2. `allowRetries` (default: `true`)
When set to `false` the UI removes the job retry buttons for a queue. This option will be ignored if `readOnlyMode` is `true`.
When set to `false` the UI removes the job retry buttons for a queue. This option will be ignored if `readOnlyMode` is `true`.

```js
const QueueMQ = require('bullmq')
const { createBullBoard } = require('@bull-board/api')
const { BullMQAdapter } = require('@bull-board/api/bullMQAdapter')
const { BullAdapter } = require('@bull-board/api/bullAdapter')
const QueueMQ = require('bullmq');
const { createBullBoard } = require('@bull-board/api');
const { BullMQAdapter } = require('@bull-board/api/bullMQAdapter');
const { BullAdapter } = require('@bull-board/api/bullAdapter');

const someQueue = new Queue()
const queueMQ = new QueueMQ()
const someQueue = new Queue();
const queueMQ = new QueueMQ();

createBullBoard({
queues: [
new BullAdapter(someQueue, { allowRetries: false }), // No retry buttons
new BullMQAdapter(queueMQ, { allowRetries: true, readOnlyMode: true }), // allowRetries will be ignored in this case in lieu of readOnlyMode
]
})
],
});
```

3. `description` (default: `empty`)
Expand All @@ -212,42 +218,40 @@ const { createBullBoard } = require('@bull-board/api');
const { BullMQAdapter } = require('@bull-board/api/bullMQAdapter');

const redact = fastRedact({
paths: ['headers.cookie', 'password', 'access_token']
})
paths: ['headers.cookie', 'password', 'access_token'],
});

const queueMQ = new QueueMQ()
const queueMQ = new QueueMQ();
const queueAdapter = new BullMQAdapter(queueMQ);
queueAdapter.setFormatter('name', (job) => `#Queue1 - ${job.name}`);
queueAdapter.setFormatter('data', (data) => redact(data));
queueAdapter.setFormatter('returnValue', (returnValue) => redact(returnValue));

createBullBoard({
queues: [queueAdapter]
})
queues: [queueAdapter],
});
```

### Hosting router on a sub path

If you host your express service on a different path than root (/) ie. https://<server_name>/<sub_path>/, then you can add the following code to provide the configuration to the bull-board router. In this example the sub path will be `my-base-path`.

```js
const Queue = require('bull')
const { createBullBoard } = require('@bull-board/api')
const { BullAdapter } = require('@bull-board/api/bullAdapter')
const { ExpressAdapter } = require('@bull-board/express')
const Queue = require('bull');
const { createBullBoard } = require('@bull-board/api');
const { BullAdapter } = require('@bull-board/api/bullAdapter');
const { ExpressAdapter } = require('@bull-board/express');

const basePath = '/my-base-path';

const someQueue = new Queue('someQueueName')
const someQueue = new Queue('someQueueName');
const serverAdapter = new ExpressAdapter();
serverAdapter.setBasePath(basePath)
serverAdapter.setBasePath(basePath);

createBullBoard({
queues: [
new BullAdapter(someQueue),
],
serverAdapter
})
queues: [new BullAdapter(someQueue)],
serverAdapter,
});

// ... express server configuration

Expand Down
3 changes: 3 additions & 0 deletions examples/with-elysia/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Elysia example

This example shows how to use [Elysia](https://elysiajs.com/) as a server for bull-board.
64 changes: 64 additions & 0 deletions examples/with-elysia/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
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 redisOptions = {
port: 6379,
host: "localhost",
password: "",
};

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}`);

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 exampleBullMq = createQueueMQ("BullMQ");

await setupBullMQProcessor(exampleBullMq.name);

const serverAdapter = new ElysiaAdapter("/ui");

createBullBoard({
queues: [new BullMQAdapter(exampleBullMq)],
serverAdapter,
});

const app = new Elysia()
.use(serverAdapter.registerPlugin())
.get("/add", async ({ query }) => {
await exampleBullMq.add("Add", { title: query.title });

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 */
});
18 changes: 18 additions & 0 deletions examples/with-elysia/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "bull-board-with-elysia",
"type": "module",
"version": "1.0.0",
"description": "Example of how to use Elysia server with bull-board",
"module": "index.ts",
"scripts": {
"start": "bun index.ts",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "kravetsone",
"license": "ISC",
"dependencies": {
"@bull-board/elysia": "^5.20.1",
"bullmq": "^4.6.0",
"elysia": "^1.0.22"
}
}
15 changes: 15 additions & 0 deletions examples/with-elysia/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"compilerOptions": {
"lib": ["ESNext"],
"module": "ESNext",
"target": "ESNext",
"moduleResolution": "Bundler",
"esModuleInterop": true,
"strict": true,
"skipLibCheck": true,
"allowSyntheticDefaultImports": true,
"rootDir": "./src",
"noEmit": true
},
"include": ["src"]
}
Loading
Loading