diff options
Diffstat (limited to 'server/controllers/api/jobs.ts')
-rw-r--r-- | server/controllers/api/jobs.ts | 40 |
1 files changed, 16 insertions, 24 deletions
diff --git a/server/controllers/api/jobs.ts b/server/controllers/api/jobs.ts index 1131a44d6..e14ea2575 100644 --- a/server/controllers/api/jobs.ts +++ b/server/controllers/api/jobs.ts | |||
@@ -1,7 +1,8 @@ | |||
1 | import * as express from 'express' | 1 | import * as express from 'express' |
2 | import { ResultList } from '../../../shared' | 2 | import { ResultList } from '../../../shared' |
3 | import { Job, JobType, JobState } from '../../../shared/models' | 3 | import { Job, JobState, JobType } from '../../../shared/models' |
4 | import { UserRight } from '../../../shared/models/users' | 4 | import { UserRight } from '../../../shared/models/users' |
5 | import { isArray } from '../../helpers/custom-validators/misc' | ||
5 | import { JobQueue } from '../../lib/job-queue' | 6 | import { JobQueue } from '../../lib/job-queue' |
6 | import { | 7 | import { |
7 | asyncMiddleware, | 8 | asyncMiddleware, |
@@ -12,13 +13,11 @@ import { | |||
12 | setDefaultSort | 13 | setDefaultSort |
13 | } from '../../middlewares' | 14 | } from '../../middlewares' |
14 | import { paginationValidator } from '../../middlewares/validators' | 15 | import { paginationValidator } from '../../middlewares/validators' |
15 | import { listJobsStateValidator, listJobsValidator } from '../../middlewares/validators/jobs' | 16 | import { listJobsValidator } from '../../middlewares/validators/jobs' |
16 | import { isArray } from '../../helpers/custom-validators/misc' | ||
17 | import { jobStates } from '@server/helpers/custom-validators/jobs' | ||
18 | 17 | ||
19 | const jobsRouter = express.Router() | 18 | const jobsRouter = express.Router() |
20 | 19 | ||
21 | jobsRouter.get('/', | 20 | jobsRouter.get('/:state?', |
22 | authenticate, | 21 | authenticate, |
23 | ensureUserHasRight(UserRight.MANAGE_JOBS), | 22 | ensureUserHasRight(UserRight.MANAGE_JOBS), |
24 | paginationValidator, | 23 | paginationValidator, |
@@ -29,18 +28,6 @@ jobsRouter.get('/', | |||
29 | asyncMiddleware(listJobs) | 28 | asyncMiddleware(listJobs) |
30 | ) | 29 | ) |
31 | 30 | ||
32 | jobsRouter.get('/:state', | ||
33 | authenticate, | ||
34 | ensureUserHasRight(UserRight.MANAGE_JOBS), | ||
35 | paginationValidator, | ||
36 | jobsSortValidator, | ||
37 | setDefaultSort, | ||
38 | setDefaultPagination, | ||
39 | listJobsValidator, | ||
40 | listJobsStateValidator, | ||
41 | asyncMiddleware(listJobs) | ||
42 | ) | ||
43 | |||
44 | // --------------------------------------------------------------------------- | 31 | // --------------------------------------------------------------------------- |
45 | 32 | ||
46 | export { | 33 | export { |
@@ -50,7 +37,7 @@ export { | |||
50 | // --------------------------------------------------------------------------- | 37 | // --------------------------------------------------------------------------- |
51 | 38 | ||
52 | async function listJobs (req: express.Request, res: express.Response) { | 39 | async function listJobs (req: express.Request, res: express.Response) { |
53 | const state = req.params.state as JobState || jobStates | 40 | const state = req.params.state as JobState |
54 | const asc = req.query.sort === 'createdAt' | 41 | const asc = req.query.sort === 'createdAt' |
55 | const jobType = req.query.jobType | 42 | const jobType = req.query.jobType |
56 | 43 | ||
@@ -65,17 +52,22 @@ async function listJobs (req: express.Request, res: express.Response) { | |||
65 | 52 | ||
66 | const result: ResultList<Job> = { | 53 | const result: ResultList<Job> = { |
67 | total, | 54 | total, |
68 | data: Array.isArray(state) | 55 | data: state |
69 | ? await Promise.all( | 56 | ? jobs.map(j => formatJob(j, state)) |
70 | jobs.map(async j => formatJob(j, await j.getState() as JobState)) | 57 | : await Promise.all(jobs.map(j => formatJobWithUnknownState(j))) |
71 | ) | ||
72 | : jobs.map(j => formatJob(j, state)) | ||
73 | } | 58 | } |
59 | |||
74 | return res.json(result) | 60 | return res.json(result) |
75 | } | 61 | } |
76 | 62 | ||
63 | async function formatJobWithUnknownState (job: any) { | ||
64 | return formatJob(job, await job.getState()) | ||
65 | } | ||
66 | |||
77 | function formatJob (job: any, state: JobState): Job { | 67 | function formatJob (job: any, state: JobState): Job { |
78 | const error = isArray(job.stacktrace) && job.stacktrace.length !== 0 ? job.stacktrace[0] : null | 68 | const error = isArray(job.stacktrace) && job.stacktrace.length !== 0 |
69 | ? job.stacktrace[0] | ||
70 | : null | ||
79 | 71 | ||
80 | return { | 72 | return { |
81 | id: job.id, | 73 | id: job.id, |