aboutsummaryrefslogtreecommitdiffhomepage
path: root/server
diff options
context:
space:
mode:
Diffstat (limited to 'server')
-rw-r--r--server/controllers/api/jobs.ts23
-rw-r--r--server/helpers/custom-validators/jobs.ts1
-rw-r--r--server/lib/job-queue/job-queue.ts13
-rw-r--r--server/middlewares/validators/jobs.ts18
4 files changed, 44 insertions, 11 deletions
diff --git a/server/controllers/api/jobs.ts b/server/controllers/api/jobs.ts
index ed6c94533..1131a44d6 100644
--- a/server/controllers/api/jobs.ts
+++ b/server/controllers/api/jobs.ts
@@ -12,11 +12,23 @@ import {
12 setDefaultSort 12 setDefaultSort
13} from '../../middlewares' 13} from '../../middlewares'
14import { paginationValidator } from '../../middlewares/validators' 14import { paginationValidator } from '../../middlewares/validators'
15import { listJobsValidator } from '../../middlewares/validators/jobs' 15import { listJobsStateValidator, listJobsValidator } from '../../middlewares/validators/jobs'
16import { isArray } from '../../helpers/custom-validators/misc' 16import { isArray } from '../../helpers/custom-validators/misc'
17import { jobStates } from '@server/helpers/custom-validators/jobs'
17 18
18const jobsRouter = express.Router() 19const jobsRouter = express.Router()
19 20
21jobsRouter.get('/',
22 authenticate,
23 ensureUserHasRight(UserRight.MANAGE_JOBS),
24 paginationValidator,
25 jobsSortValidator,
26 setDefaultSort,
27 setDefaultPagination,
28 listJobsValidator,
29 asyncMiddleware(listJobs)
30)
31
20jobsRouter.get('/:state', 32jobsRouter.get('/:state',
21 authenticate, 33 authenticate,
22 ensureUserHasRight(UserRight.MANAGE_JOBS), 34 ensureUserHasRight(UserRight.MANAGE_JOBS),
@@ -25,6 +37,7 @@ jobsRouter.get('/:state',
25 setDefaultSort, 37 setDefaultSort,
26 setDefaultPagination, 38 setDefaultPagination,
27 listJobsValidator, 39 listJobsValidator,
40 listJobsStateValidator,
28 asyncMiddleware(listJobs) 41 asyncMiddleware(listJobs)
29) 42)
30 43
@@ -37,7 +50,7 @@ export {
37// --------------------------------------------------------------------------- 50// ---------------------------------------------------------------------------
38 51
39async function listJobs (req: express.Request, res: express.Response) { 52async function listJobs (req: express.Request, res: express.Response) {
40 const state = req.params.state as JobState 53 const state = req.params.state as JobState || jobStates
41 const asc = req.query.sort === 'createdAt' 54 const asc = req.query.sort === 'createdAt'
42 const jobType = req.query.jobType 55 const jobType = req.query.jobType
43 56
@@ -52,7 +65,11 @@ async function listJobs (req: express.Request, res: express.Response) {
52 65
53 const result: ResultList<Job> = { 66 const result: ResultList<Job> = {
54 total, 67 total,
55 data: jobs.map(j => formatJob(j, state)) 68 data: Array.isArray(state)
69 ? await Promise.all(
70 jobs.map(async j => formatJob(j, await j.getState() as JobState))
71 )
72 : jobs.map(j => formatJob(j, state))
56 } 73 }
57 return res.json(result) 74 return res.json(result)
58} 75}
diff --git a/server/helpers/custom-validators/jobs.ts b/server/helpers/custom-validators/jobs.ts
index dd33e85a3..72dc73ee4 100644
--- a/server/helpers/custom-validators/jobs.ts
+++ b/server/helpers/custom-validators/jobs.ts
@@ -15,6 +15,7 @@ function isValidJobType (value: any) {
15// --------------------------------------------------------------------------- 15// ---------------------------------------------------------------------------
16 16
17export { 17export {
18 jobStates,
18 isValidJobState, 19 isValidJobState,
19 isValidJobType 20 isValidJobType
20} 21}
diff --git a/server/lib/job-queue/job-queue.ts b/server/lib/job-queue/job-queue.ts
index 8d97434ac..49f06584d 100644
--- a/server/lib/job-queue/job-queue.ts
+++ b/server/lib/job-queue/job-queue.ts
@@ -154,13 +154,13 @@ class JobQueue {
154 } 154 }
155 155
156 async listForApi (options: { 156 async listForApi (options: {
157 state: JobState 157 state: JobState | JobState[]
158 start: number 158 start: number
159 count: number 159 count: number
160 asc?: boolean 160 asc?: boolean
161 jobType: JobType 161 jobType: JobType
162 }): Promise<Bull.Job[]> { 162 }): Promise<Bull.Job[]> {
163 const { state, start, count, asc, jobType } = options 163 const { state = Array.isArray(options.state) ? options.state : [ options.state ], start, count, asc, jobType } = options
164 let results: Bull.Job[] = [] 164 let results: Bull.Job[] = []
165 165
166 const filteredJobTypes = this.filterJobTypes(jobType) 166 const filteredJobTypes = this.filterJobTypes(jobType)
@@ -172,7 +172,7 @@ class JobQueue {
172 continue 172 continue
173 } 173 }
174 174
175 const jobs = await queue.getJobs([ state ], 0, start + count, asc) 175 const jobs = await queue.getJobs(state as Bull.JobStatus[], 0, start + count, asc)
176 results = results.concat(jobs) 176 results = results.concat(jobs)
177 } 177 }
178 178
@@ -188,7 +188,8 @@ class JobQueue {
188 return results.slice(start, start + count) 188 return results.slice(start, start + count)
189 } 189 }
190 190
191 async count (state: JobState, jobType?: JobType): Promise<number> { 191 async count (state: JobState | JobState[], jobType?: JobType): Promise<number> {
192 const states = Array.isArray(state) ? state : [ state ]
192 let total = 0 193 let total = 0
193 194
194 const filteredJobTypes = this.filterJobTypes(jobType) 195 const filteredJobTypes = this.filterJobTypes(jobType)
@@ -202,7 +203,9 @@ class JobQueue {
202 203
203 const counts = await queue.getJobCounts() 204 const counts = await queue.getJobCounts()
204 205
205 total += counts[state] 206 for (const s of states) {
207 total += counts[s]
208 }
206 } 209 }
207 210
208 return total 211 return total
diff --git a/server/middlewares/validators/jobs.ts b/server/middlewares/validators/jobs.ts
index b57615dbc..0fc183c1a 100644
--- a/server/middlewares/validators/jobs.ts
+++ b/server/middlewares/validators/jobs.ts
@@ -5,8 +5,6 @@ import { logger } from '../../helpers/logger'
5import { areValidationErrors } from './utils' 5import { areValidationErrors } from './utils'
6 6
7const listJobsValidator = [ 7const listJobsValidator = [
8 param('state')
9 .custom(isValidJobState).not().isEmpty().withMessage('Should have a valid job state'),
10 query('jobType') 8 query('jobType')
11 .optional() 9 .optional()
12 .custom(isValidJobType).withMessage('Should have a valid job state'), 10 .custom(isValidJobType).withMessage('Should have a valid job state'),
@@ -20,8 +18,22 @@ const listJobsValidator = [
20 } 18 }
21] 19]
22 20
21const listJobsStateValidator = [
22 param('state')
23 .custom(isValidJobState).not().isEmpty().withMessage('Should have a valid job state'),
24
25 (req: express.Request, res: express.Response, next: express.NextFunction) => {
26 logger.debug('Checking listJobsValidator parameters.', { parameters: req.params })
27
28 if (areValidationErrors(req, res)) return
29
30 return next()
31 }
32]
33
23// --------------------------------------------------------------------------- 34// ---------------------------------------------------------------------------
24 35
25export { 36export {
26 listJobsValidator 37 listJobsValidator,
38 listJobsStateValidator
27} 39}