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