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