Skip to content

Commit

Permalink
refactor: Use Map to save given queues instead of POJO
Browse files Browse the repository at this point in the history
  • Loading branch information
felixmosh committed Apr 26, 2021
1 parent 2aa46c7 commit 6f2c487
Show file tree
Hide file tree
Showing 9 changed files with 18 additions and 24 deletions.
8 changes: 1 addition & 7 deletions src/@types/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,7 @@ export interface QueueAdapterOptions {
readOnlyMode: boolean
}

export interface BullBoardQueue {
queue: QueueAdapter
}

export interface BullBoardQueues {
[key: string]: BullBoardQueue
}
export type BullBoardQueues = Map<string, QueueAdapter>

export interface ValidMetrics {
total_system_memory: string
Expand Down
8 changes: 4 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { retryJob } from './routes/retryJob'
export { BullMQAdapter } from './queueAdapters/bullMQ'
export { BullAdapter } from './queueAdapters/bull'

const bullBoardQueues: BullBoardQueues = {}
const bullBoardQueues: BullBoardQueues = new Map<string, QueueAdapter>()

const wrapAsync = <Params extends ParamsDictionary>(
fn: RequestHandler<Params>,
Expand Down Expand Up @@ -49,7 +49,7 @@ export const setQueues = (bullQueues: ReadonlyArray<QueueAdapter>): void => {
bullQueues.forEach((queue) => {
const name = queue.getName()

bullBoardQueues[name] = { queue }
bullBoardQueues.set(name, queue)
})
}

Expand All @@ -58,9 +58,9 @@ export const replaceQueues = (
): void => {
const queuesToPersist: string[] = bullQueues.map((queue) => queue.getName())

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

Expand Down
2 changes: 1 addition & 1 deletion src/routes/cleanAll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const cleanAll: RequestHandler<RequestParams> = async (

const GRACE_TIME_MS = 5000

const { queue } = bullBoardQueues[queueName]
const queue = bullBoardQueues.get(queueName)
if (!queue) {
return res.status(404).send({
error: 'Queue not found',
Expand Down
2 changes: 1 addition & 1 deletion src/routes/cleanJob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const cleanJob: RequestHandler = async (req: Request, res: Response) => {
bullBoardQueues: BullBoardQueues
}
const { queueName, id } = req.params
const { queue } = bullBoardQueues[queueName]
const queue = bullBoardQueues.get(queueName)

if (!queue) {
return res.status(404).send({
Expand Down
2 changes: 1 addition & 1 deletion src/routes/jobLogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const jobLogs: RequestHandler = async (req: Request, res: Response) => {
bullBoardQueues: BullBoardQueues
}
const { queueName, id } = req.params
const { queue } = bullBoardQueues[queueName]
const queue = bullBoardQueues.get(queueName)

if (!queue) {
return res.status(404).send({
Expand Down
2 changes: 1 addition & 1 deletion src/routes/promoteJob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const promoteJob: RequestHandler = async (
bullBoardQueues: BullBoardQueues
}
const { queueName, id } = req.params
const { queue } = bullBoardQueues[queueName]
const queue = bullBoardQueues.get(queueName)

if (!queue) {
return res.status(404).send({
Expand Down
14 changes: 7 additions & 7 deletions src/routes/queues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { parse as parseRedisInfo } from 'redis-info'

import * as api from '../@types/api'
import * as app from '../@types/app'
import { JobStatus, QueueAdapter } from '../@types/app'
import { BullBoardQueues, JobStatus, QueueAdapter } from '../@types/app'
import { Status } from '../ui/components/constants'

type MetricName = keyof app.ValidMetrics
Expand All @@ -18,9 +18,7 @@ const metrics: MetricName[] = [
'blocked_clients',
]

const getStats = async ({
queue,
}: app.BullBoardQueue): Promise<app.ValidMetrics> => {
const getStats = async (queue: QueueAdapter): Promise<app.ValidMetrics> => {
const redisClient = await queue.getClient()
const redisInfoRaw = await redisClient.info()
const redisInfo = parseRedisInfo(redisInfoRaw)
Expand Down Expand Up @@ -73,7 +71,7 @@ const getDataForQueues = async (
req: Request,
): Promise<api.GetQueues> => {
const query = req.query || {}
const pairs = Object.entries(bullBoardQueues)
const pairs = [...bullBoardQueues.entries()]

if (pairs.length == 0) {
return {
Expand All @@ -83,7 +81,7 @@ const getDataForQueues = async (
}

const queues: app.AppQueue[] = await Promise.all(
pairs.map(async ([name, { queue }]) => {
pairs.map(async ([name, queue]) => {
const counts = await queue.getJobCounts(...statuses)
const status =
query[name] === 'latest' ? statuses : (query[name] as JobStatus[])
Expand All @@ -110,7 +108,9 @@ export const queuesHandler: RequestHandler = async (
req: Request,
res: Response,
) => {
const { bullBoardQueues } = req.app.locals
const { bullBoardQueues } = req.app.locals as {
bullBoardQueues: BullBoardQueues
}

res.json(await getDataForQueues(bullBoardQueues, req))
}
2 changes: 1 addition & 1 deletion src/routes/retryAll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const retryAll: RequestHandler = async (req: Request, res: Response) => {
bullBoardQueues: BullBoardQueues
}

const { queue } = bullBoardQueues[queueName]
const queue = bullBoardQueues.get(queueName)
if (!queue) {
return res.status(404).send({ error: 'queue not found' })
} else if (queue.readOnlyMode) {
Expand Down
2 changes: 1 addition & 1 deletion src/routes/retryJob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const retryJob: RequestHandler = async (req: Request, res: Response) => {
bullBoardQueues: BullBoardQueues
}
const { queueName, id } = req.params
const { queue } = bullBoardQueues[queueName]
const queue = bullBoardQueues.get(queueName)

if (!queue) {
return res.status(404).send({
Expand Down

0 comments on commit 6f2c487

Please sign in to comment.