Skip to content

Commit

Permalink
feat: add addQueue and removeQueue methods (#278)
Browse files Browse the repository at this point in the history
* feat: add addQueue and removeQueue methods.

* chore: fix linting errors.

* fix: lint issues

Co-authored-by: Felix Mosheev <[email protected]>
  • Loading branch information
ejhayes and felixmosh authored May 21, 2021
1 parent 644ce99 commit c4b6763
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 2 deletions.
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

0 comments on commit c4b6763

Please sign in to comment.