aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2020-12-14 12:00:35 +0100
committerChocobozzz <me@florianbigard.com>2020-12-14 12:00:35 +0100
commit402145b8630d1908c35f8c22846ddc4475f25d3e (patch)
treeff50cb09c5f56cc408ef20a7c959ef7a0642b76b /server/controllers
parentc04816108e8ec62eb29caf82806f3927dc5eb85a (diff)
downloadPeerTube-402145b8630d1908c35f8c22846ddc4475f25d3e.tar.gz
PeerTube-402145b8630d1908c35f8c22846ddc4475f25d3e.tar.zst
PeerTube-402145b8630d1908c35f8c22846ddc4475f25d3e.zip
Refactor jobs state
Diffstat (limited to 'server/controllers')
-rw-r--r--server/controllers/api/jobs.ts40
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 @@
1import * as express from 'express' 1import * as express from 'express'
2import { ResultList } from '../../../shared' 2import { ResultList } from '../../../shared'
3import { Job, JobType, JobState } from '../../../shared/models' 3import { Job, JobState, JobType } from '../../../shared/models'
4import { UserRight } from '../../../shared/models/users' 4import { UserRight } from '../../../shared/models/users'
5import { isArray } from '../../helpers/custom-validators/misc'
5import { JobQueue } from '../../lib/job-queue' 6import { JobQueue } from '../../lib/job-queue'
6import { 7import {
7 asyncMiddleware, 8 asyncMiddleware,
@@ -12,13 +13,11 @@ import {
12 setDefaultSort 13 setDefaultSort
13} from '../../middlewares' 14} from '../../middlewares'
14import { paginationValidator } from '../../middlewares/validators' 15import { paginationValidator } from '../../middlewares/validators'
15import { listJobsStateValidator, listJobsValidator } from '../../middlewares/validators/jobs' 16import { listJobsValidator } from '../../middlewares/validators/jobs'
16import { isArray } from '../../helpers/custom-validators/misc'
17import { jobStates } from '@server/helpers/custom-validators/jobs'
18 17
19const jobsRouter = express.Router() 18const jobsRouter = express.Router()
20 19
21jobsRouter.get('/', 20jobsRouter.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
32jobsRouter.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
46export { 33export {
@@ -50,7 +37,7 @@ export {
50// --------------------------------------------------------------------------- 37// ---------------------------------------------------------------------------
51 38
52async function listJobs (req: express.Request, res: express.Response) { 39async 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
63async function formatJobWithUnknownState (job: any) {
64 return formatJob(job, await job.getState())
65}
66
77function formatJob (job: any, state: JobState): Job { 67function 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,