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

feat: add addQueue and removeQueue methods #278

Merged
merged 3 commits into from
May 21, 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const someQueue = new Queue('someQueueName')
const someOtherQueue = new Queue('someOtherQueueName')
const queueMQ = new QueueMQ('queueMQName')

const { router, setQueues, replaceQueues } = createBullBoard([
const { router, setQueues, replaceQueues, addQueue, removeQueue } = createBullBoard([
new BullAdapter(someQueue),
new BullAdapter(someOtherQueue),
new BullMQAdapter(queueMQ),
Expand Down
22 changes: 21 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export function createBullBoard(
router: Express;
setQueues: (newBullQueues: ReadonlyArray<BaseAdapter>) => void;
replaceQueues: (newBullQueues: ReadonlyArray<BaseAdapter>) => void;
addQueue: (queue: BaseAdapter) => void;
removeQueue: (queueOrName: string | BaseAdapter) => void;
} {
const bullBoardQueues: BullBoardQueues = new Map<string, BaseAdapter>();
const app: Express = express();
Expand All @@ -25,6 +27,18 @@ export function createBullBoard(
app.get(['/', '/queue/:queueName'], entryPoint);
app.use('/api', apiRouter);

function addQueue(queue: BaseAdapter): void {
const name = queue.getName();
bullBoardQueues.set(name, queue);
}

function removeQueue(queueOrName: string | BaseAdapter) {
const name =
typeof queueOrName === 'string' ? queueOrName : queueOrName.getName();

bullBoardQueues.delete(name);
}

function setQueues(newBullQueues: ReadonlyArray<BaseAdapter>): void {
newBullQueues.forEach((queue) => {
const name = queue.getName();
Expand All @@ -49,5 +63,11 @@ export function createBullBoard(

setQueues(bullQueues);

return { router: app, setQueues, replaceQueues };
return {
router: app,
setQueues,
replaceQueues,
addQueue,
removeQueue,
};
}
125 changes: 125 additions & 0 deletions tests/api/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,131 @@ describe('happy', () => {
});
});

it('should be able to add a queue', async () => {
const addedQueue = new Queue('AddedQueue', {
connection: {
host: 'localhost',
port: 6379,
},
});

const { router, addQueue } = createBullBoard([]);

addQueue(new BullMQAdapter(addedQueue));

await request(router)
.get('/api/queues')
.expect('Content-Type', /json/)
.expect(200)
.then((res) => {
expect(JSON.parse(res.text)).toMatchInlineSnapshot(
{
stats: {
blocked_clients: expect.any(String),
connected_clients: expect.any(String),
mem_fragmentation_ratio: expect.any(String),
redis_version: expect.any(String),
total_system_memory: expect.any(String),
used_memory: expect.any(String),
},
},
`
Object {
"queues": Array [
Object {
"counts": Object {
"active": 0,
"completed": 0,
"delayed": 0,
"failed": 0,
"paused": 0,
"waiting": 0,
},
"jobs": Array [],
"name": "AddedQueue",
"readOnlyMode": false,
},
],
"stats": Object {
"blocked_clients": Any<String>,
"connected_clients": Any<String>,
"mem_fragmentation_ratio": Any<String>,
"redis_version": Any<String>,
"total_system_memory": Any<String>,
"used_memory": Any<String>,
},
}
`
);
});
});

it('should be able to remove a queue when passed as queue object', async () => {
const addedQueue = new Queue('AddedQueue', {
connection: {
host: 'localhost',
port: 6379,
},
});

const { router, addQueue, removeQueue } = createBullBoard([]);

addQueue(new BullMQAdapter(addedQueue));
removeQueue(new BullMQAdapter(addedQueue));

await request(router)
.get('/api/queues')
.expect('Content-Type', /json/)
.expect(200)
.then((res) => {
expect(JSON.parse(res.text)).toMatchInlineSnapshot(
{
queues: [],
stats: {},
},
`
Object {
"queues": Array [],
"stats": Object {},
}
`
);
});
});

it('should be able to remove a queue when passed as string', async () => {
const addedQueue = new Queue('AddedQueue', {
connection: {
host: 'localhost',
port: 6379,
},
});

const { router, addQueue, removeQueue } = createBullBoard([]);

addQueue(new BullMQAdapter(addedQueue));
removeQueue('AddedQueue');

await request(router)
.get('/api/queues')
.expect('Content-Type', /json/)
.expect(200)
.then((res) => {
expect(JSON.parse(res.text)).toMatchInlineSnapshot(
{
queues: [],
stats: {},
},
`
Object {
"queues": Array [],
"stats": Object {},
}
`
);
});
});

it('should be able to replace queues without initial set', async () => {
const codeQueue = new Queue('Code', {
connection: {
Expand Down