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: allow to replace queues completely #88

Merged
merged 3 commits into from
Mar 27, 2020
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ yarn-error.log
*.rdb
website/build
docker-compose.dockest-generated.yml
dockest-error.json
128 changes: 127 additions & 1 deletion src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ describe('index', () => {
it('should save the interface', () => {
expect(bullBoard).toMatchInlineSnapshot(`
Object {
"replaceQueues": [Function],
"router": [Function],
"setQueues": [Function],
}
Expand All @@ -16,7 +17,7 @@ describe('index', () => {
})

describe('happy', () => {
const { router, setQueues } = bullBoard
const { router, setQueues, replaceQueues } = bullBoard

it('should be able to set queue', async () => {
const paintQueue = new Queue('Paint', {
Expand Down Expand Up @@ -73,4 +74,129 @@ describe('happy', () => {
)
})
})

it('should be able to replace queues', async () => {
const paintQueue = new Queue('Paint', {
connection: {
host: 'localhost',
port: 6379,
},
})
const drainQueue = new Queue('Drain', {
connection: {
host: 'localhost',
port: 6379,
},
})
const codeQueue = new Queue('Code', {
connection: {
host: 'localhost',
port: 6379,
},
})

setQueues([paintQueue, drainQueue])
replaceQueues([codeQueue])

await request(router)
.get('/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": "bull:Code:~",
},
],
"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 replace queues without initial set', async () => {
const codeQueue = new Queue('Code', {
connection: {
host: 'localhost',
port: 6379,
},
})

replaceQueues([codeQueue])

await request(router)
.get('/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": "bull:Code:~",
},
],
"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>,
},
}
`,
)
})
})
})
16 changes: 15 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ router.put('/queues/:queueName/:id/retry', wrapAsync(retryJob))
router.put('/queues/:queueName/:id/promote', wrapAsync(promoteJob))
router.put('/queues/:queueName/clean/:queueStatus', wrapAsync(cleanAll))

export const setQueues = (bullQueues: Queue[] | QueueMq[]) => {
type Q = Queue | QueueMq

export const setQueues = (bullQueues: ReadonlyArray<Q>) => {
bullQueues.forEach((queue: Queue | QueueMq) => {
const name = queue instanceof QueueMq ? queue.toKey('~') : queue.name

Expand All @@ -44,4 +46,16 @@ export const setQueues = (bullQueues: Queue[] | QueueMq[]) => {
})
}

export const replaceQueues = (bullQueues: ReadonlyArray<Q>) => {
const queuesToPersist: string[] = bullQueues.map(queue => queue.name)

Object.keys(bullBoardQueues).forEach(name => {
if (queuesToPersist.indexOf(name) === -1) {
delete bullBoardQueues[name]
}
})

return setQueues(bullQueues)
}

export { router }