-
-
Notifications
You must be signed in to change notification settings - Fork 368
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: extract common control code to queueProvider & jobProvider (#…
…268)
- Loading branch information
Showing
10 changed files
with
120 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,10 @@ | ||
import { Request, RequestHandler, Response } from 'express-serve-static-core' | ||
import { BullBoardQueues } from '../../@types/app' | ||
|
||
export const jobLogs: RequestHandler = async (req: Request, res: Response) => { | ||
const { bullBoardQueues } = req.app.locals as { | ||
bullBoardQueues: BullBoardQueues | ||
} | ||
const { queueName, id } = req.params | ||
const queue = bullBoardQueues.get(queueName) | ||
const { jobId } = req.params | ||
const { queue } = res.locals | ||
|
||
if (!queue) { | ||
return res.status(404).send({ | ||
error: 'Queue not found', | ||
}) | ||
} | ||
|
||
const job = await queue.getJob(id) | ||
|
||
if (!job) { | ||
return res.status(404).send({ | ||
error: 'Job not found', | ||
}) | ||
} | ||
|
||
const logs = await queue.getJobLogs(id) | ||
const logs = await queue.getJobLogs(jobId) | ||
|
||
return res.json(logs) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { NextFunction, Request, Response } from 'express-serve-static-core' | ||
import { BaseAdapter } from '../../queueAdapters/base' | ||
|
||
export function jobProvider() { | ||
return async (req: Request, res: Response, next: NextFunction) => { | ||
const { jobId } = req.params | ||
const { queue } = res.locals as { queue: BaseAdapter } | ||
|
||
if (!jobId || !queue) { | ||
return next(new Error('Invalid data')) | ||
} | ||
|
||
const job = await queue.getJob(jobId) | ||
|
||
if (!job) { | ||
return res.status(404).send({ | ||
error: 'Job not found', | ||
}) | ||
} | ||
|
||
res.locals.job = job | ||
|
||
next() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { NextFunction, Request, Response } from 'express-serve-static-core' | ||
import { BullBoardQueues } from '../../@types/app' | ||
|
||
export function queueProvider({ | ||
skipReadOnlyModeCheck = false, | ||
}: { | ||
skipReadOnlyModeCheck?: boolean | ||
} = {}) { | ||
return async (req: Request, res: Response, next: NextFunction) => { | ||
const { queueName } = req.params | ||
|
||
if (typeof queueName === 'undefined') { | ||
return next() | ||
} | ||
|
||
const { bullBoardQueues } = req.app.locals as { | ||
bullBoardQueues: BullBoardQueues | ||
} | ||
|
||
const queue = bullBoardQueues.get(queueName) | ||
if (!queue) { | ||
return res.status(404).send({ error: 'Queue not found' }) | ||
} else if (queue.readOnlyMode && !skipReadOnlyModeCheck) { | ||
return res.status(405).send({ | ||
error: 'Method not allowed on read only queue', | ||
}) | ||
} | ||
|
||
res.locals.queue = queue | ||
|
||
next() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { ParamsDictionary, RequestHandler } from 'express-serve-static-core' | ||
|
||
export const wrapAsync = <Params extends ParamsDictionary>( | ||
fn: RequestHandler<Params>, | ||
): RequestHandler<Params> => async (req, res, next) => | ||
Promise.resolve(fn(req, res, next)).catch(next) |