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

Refresh queues #12

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
24 changes: 12 additions & 12 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
root = true

[*]
end_of_line = crlf
insert_final_newline = true
charset = utf-8
indent_style = tab
indent_size = 4

[{package.json,**.yml}]
indent_style = space
indent_size = 2
root = true
[*]
end_of_line = lf
insert_final_newline = true
charset = utf-8
indent_style = tab
indent_size = 4
[{package.json,**.yml}]
indent_style = space
indent_size = 2
51 changes: 40 additions & 11 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const {createBullBoard} = require('@bull-board/api');
const {BullAdapter} = require('@bull-board/api/bullAdapter');
const {BullMQAdapter} = require('@bull-board/api//bullMQAdapter');
const {ExpressAdapter} = require('@bull-board/express');
const { isDeepStrictEqual } = require('util')
const Queue = require('bull');
const bullmq = require('bullmq');
const express = require('express');
Expand All @@ -26,25 +27,45 @@ const redisConfig = {

const serverAdapter = new ExpressAdapter();
const client = redis.createClient(redisConfig.redis);
const {setQueues} = createBullBoard({queues: [], serverAdapter});
const { setQueues, replaceQueues } = createBullBoard({queues: [], serverAdapter});
const router = serverAdapter.getRouter();

client.KEYS(`${config.BULL_PREFIX}:*`, (err, keys) => {
const uniqKeys = new Set(keys.map(key => key.replace(/^.+?:(.+?):.+?$/, '$1')));
const queueList = Array.from(uniqKeys).sort().map(
(item) => {
if (config.BULL_VERSION === 'BULLMQ') {
return new BullMQAdapter(new bullmq.Queue(item, {connection: redisConfig.redis}));
}
let lastValidQueues = null

return new BullAdapter(new Queue(item, redisConfig));
const createAdapters = (queues) => queues.map(
(item) => {
if (config.BULL_VERSION === 'BULLMQ') {
return new BullMQAdapter(new bullmq.Queue(item, {connection: redisConfig.redis}));
}
);

return new BullAdapter(new Queue(item, redisConfig));
}
);

client.KEYS(`${config.BULL_PREFIX}:*`, (err, keys) => {
const uniqKeys = new Set(keys.map(key => key.replace(/^.+?:(.+?):.+?$/, '$1')));
const actualQueues = Array.from(uniqKeys).sort();
lastValidQueues = actualQueues;
const queueList = createAdapters(actualQueues);

setQueues(queueList);
console.log('done!')
console.log('done!');
});

const updateQueues = () => {
client.KEYS(`${config.BULL_PREFIX}:*`, (err, keys) => {
const uniqKeys = new Set(keys.map(key => key.replace(/^.+?:(.+?):.+?$/, '$1')));
const actualQueues = Array.from(uniqKeys).sort();
if (isDeepStrictEqual(lastValidQueues, actualQueues)) return;

lastValidQueues = actualQueues;
const queueList = createAdapters(actualQueues);

replaceQueues(queueList);
console.log('detected queue change, updating UI');
});
}

const app = express();

app.set('views', __dirname + '/views');
Expand Down Expand Up @@ -87,7 +108,15 @@ if (config.AUTH_ENABLED) {
app.use(config.HOME_PAGE, router);
}

let updateQueuesInterval = null
const gracefullyShutdown = () => clearInterval(updateQueuesInterval)

app.listen(config.PORT, () => {
console.log(`bull-board is started http://localhost:${config.PORT}${config.HOME_PAGE}`);
console.log(`bull-board is fetching queue list, please wait...`);

// poor man queue update process
updateQueuesInterval = setInterval(updateQueues, 60 * 1000)
process.on('SIGINT', gracefullyShutdown)
process.on('SIGTERM', gracefullyShutdown)
});