]>
Commit | Line | Data |
---|---|---|
5a921e7b | 1 | import { Job as BullJob } from 'bullmq' |
41fb13c3 | 2 | import express from 'express' |
419b520c | 3 | import { HttpStatusCode, Job, JobState, JobType, ResultList, UserRight } from '@shared/models' |
402145b8 | 4 | import { isArray } from '../../helpers/custom-validators/misc' |
94a5ff8a | 5 | import { JobQueue } from '../../lib/job-queue' |
f05a1c30 | 6 | import { |
e915cde3 | 7 | apiRateLimiter, |
94a5ff8a C |
8 | asyncMiddleware, |
9 | authenticate, | |
10 | ensureUserHasRight, | |
11 | jobsSortValidator, | |
1333ab1f | 12 | openapiOperationDoc, |
18b24b2d | 13 | paginationValidatorBuilder, |
94a5ff8a | 14 | setDefaultPagination, |
f05a1c30 C |
15 | setDefaultSort |
16 | } from '../../middlewares' | |
402145b8 | 17 | import { listJobsValidator } from '../../middlewares/validators/jobs' |
5cd80545 C |
18 | |
19 | const jobsRouter = express.Router() | |
20 | ||
e915cde3 C |
21 | jobsRouter.use(apiRateLimiter) |
22 | ||
419b520c C |
23 | jobsRouter.post('/pause', |
24 | authenticate, | |
25 | ensureUserHasRight(UserRight.MANAGE_JOBS), | |
26 | asyncMiddleware(pauseJobQueue) | |
27 | ) | |
28 | ||
29 | jobsRouter.post('/resume', | |
30 | authenticate, | |
31 | ensureUserHasRight(UserRight.MANAGE_JOBS), | |
e65ef81c | 32 | resumeJobQueue |
419b520c C |
33 | ) |
34 | ||
402145b8 | 35 | jobsRouter.get('/:state?', |
1333ab1f | 36 | openapiOperationDoc({ operationId: 'getJobs' }), |
040d6896 RK |
37 | authenticate, |
38 | ensureUserHasRight(UserRight.MANAGE_JOBS), | |
18b24b2d | 39 | paginationValidatorBuilder([ 'jobs' ]), |
040d6896 RK |
40 | jobsSortValidator, |
41 | setDefaultSort, | |
42 | setDefaultPagination, | |
43 | listJobsValidator, | |
44 | asyncMiddleware(listJobs) | |
45 | ) | |
46 | ||
5cd80545 C |
47 | // --------------------------------------------------------------------------- |
48 | ||
49 | export { | |
50 | jobsRouter | |
51 | } | |
52 | ||
53 | // --------------------------------------------------------------------------- | |
54 | ||
419b520c C |
55 | async function pauseJobQueue (req: express.Request, res: express.Response) { |
56 | await JobQueue.Instance.pause() | |
57 | ||
58 | return res.sendStatus(HttpStatusCode.NO_CONTENT_204) | |
59 | } | |
60 | ||
e65ef81c C |
61 | function resumeJobQueue (req: express.Request, res: express.Response) { |
62 | JobQueue.Instance.resume() | |
419b520c C |
63 | |
64 | return res.sendStatus(HttpStatusCode.NO_CONTENT_204) | |
65 | } | |
66 | ||
d5d9b6d7 | 67 | async function listJobs (req: express.Request, res: express.Response) { |
402145b8 | 68 | const state = req.params.state as JobState |
94831479 | 69 | const asc = req.query.sort === 'createdAt' |
1061c73f | 70 | const jobType = req.query.jobType |
94a5ff8a | 71 | |
1061c73f C |
72 | const jobs = await JobQueue.Instance.listForApi({ |
73 | state, | |
74 | start: req.query.start, | |
75 | count: req.query.count, | |
76 | asc, | |
77 | jobType | |
78 | }) | |
439d68e5 | 79 | const total = await JobQueue.Instance.count(state, jobType) |
94a5ff8a | 80 | |
a1587156 | 81 | const result: ResultList<Job> = { |
94a5ff8a | 82 | total, |
3b01f4c0 | 83 | data: await Promise.all(jobs.map(j => formatJob(j, state))) |
94a5ff8a | 84 | } |
402145b8 | 85 | |
94a5ff8a C |
86 | return res.json(result) |
87 | } | |
5cd80545 | 88 | |
5a921e7b | 89 | async function formatJob (job: BullJob, state?: JobState): Promise<Job> { |
402145b8 C |
90 | const error = isArray(job.stacktrace) && job.stacktrace.length !== 0 |
91 | ? job.stacktrace[0] | |
92 | : null | |
94831479 | 93 | |
94a5ff8a C |
94 | return { |
95 | id: job.id, | |
3b01f4c0 | 96 | state: state || await job.getState(), |
5a921e7b | 97 | type: job.queueName as JobType, |
94a5ff8a | 98 | data: job.data, |
0c9668f7 C |
99 | parent: job.parent |
100 | ? { id: job.parent.id } | |
101 | : undefined, | |
5a921e7b | 102 | progress: job.progress as number, |
77d7e851 | 103 | priority: job.opts.priority, |
94831479 C |
104 | error, |
105 | createdAt: new Date(job.timestamp), | |
106 | finishedOn: new Date(job.finishedOn), | |
107 | processedOn: new Date(job.processedOn) | |
94a5ff8a | 108 | } |
5cd80545 | 109 | } |