PeerTube/server/core/controllers/api/jobs.ts

110 lines
2.8 KiB
TypeScript
Raw Normal View History

2022-08-08 03:42:08 -05:00
import { Job as BullJob } from 'bullmq'
2021-08-27 07:32:44 -05:00
import express from 'express'
import { HttpStatusCode, Job, JobState, JobType, ResultList, UserRight } from '@peertube/peertube-models'
import { isArray } from '../../helpers/custom-validators/misc.js'
import { JobQueue } from '../../lib/job-queue/index.js'
import {
2023-06-20 07:17:34 -05:00
apiRateLimiter,
asyncMiddleware,
authenticate,
ensureUserHasRight,
jobsSortValidator,
openapiOperationDoc,
2021-03-09 02:22:05 -06:00
paginationValidatorBuilder,
setDefaultPagination,
setDefaultSort
} from '../../middlewares/index.js'
import { listJobsValidator } from '../../middlewares/validators/jobs.js'
2017-11-30 03:51:13 -06:00
const jobsRouter = express.Router()
2023-06-20 07:17:34 -05:00
jobsRouter.use(apiRateLimiter)
jobsRouter.post('/pause',
authenticate,
ensureUserHasRight(UserRight.MANAGE_JOBS),
asyncMiddleware(pauseJobQueue)
)
jobsRouter.post('/resume',
authenticate,
ensureUserHasRight(UserRight.MANAGE_JOBS),
2023-02-16 04:56:58 -06:00
resumeJobQueue
)
2020-12-14 05:00:35 -06:00
jobsRouter.get('/:state?',
openapiOperationDoc({ operationId: 'getJobs' }),
2020-12-13 12:27:25 -06:00
authenticate,
ensureUserHasRight(UserRight.MANAGE_JOBS),
2021-03-09 02:22:05 -06:00
paginationValidatorBuilder([ 'jobs' ]),
2020-12-13 12:27:25 -06:00
jobsSortValidator,
setDefaultSort,
setDefaultPagination,
listJobsValidator,
asyncMiddleware(listJobs)
)
2017-11-30 03:51:13 -06:00
// ---------------------------------------------------------------------------
export {
jobsRouter
}
// ---------------------------------------------------------------------------
async function pauseJobQueue (req: express.Request, res: express.Response) {
await JobQueue.Instance.pause()
return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
}
2023-02-16 04:56:58 -06:00
function resumeJobQueue (req: express.Request, res: express.Response) {
JobQueue.Instance.resume()
return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
}
2019-10-21 07:50:55 -05:00
async function listJobs (req: express.Request, res: express.Response) {
2020-12-14 05:00:35 -06:00
const state = req.params.state as JobState
2018-07-10 10:02:20 -05:00
const asc = req.query.sort === 'createdAt'
2019-12-04 07:49:59 -06:00
const jobType = req.query.jobType
2019-12-04 07:49:59 -06:00
const jobs = await JobQueue.Instance.listForApi({
state,
start: req.query.start,
count: req.query.count,
asc,
jobType
})
2020-11-09 09:13:03 -06:00
const total = await JobQueue.Instance.count(state, jobType)
2020-01-31 09:56:52 -06:00
const result: ResultList<Job> = {
total,
2021-01-21 07:42:43 -06:00
data: await Promise.all(jobs.map(j => formatJob(j, state)))
}
2020-12-14 05:00:35 -06:00
return res.json(result)
}
2017-11-30 03:51:13 -06:00
2022-08-08 03:42:08 -05:00
async function formatJob (job: BullJob, state?: JobState): Promise<Job> {
2020-12-14 05:00:35 -06:00
const error = isArray(job.stacktrace) && job.stacktrace.length !== 0
? job.stacktrace[0]
: null
2018-07-10 10:02:20 -05:00
return {
id: job.id,
2021-01-21 07:42:43 -06:00
state: state || await job.getState(),
2022-08-08 03:42:08 -05:00
type: job.queueName as JobType,
data: job.data,
parent: job.parent
? { id: job.parent.id }
: undefined,
2022-08-08 03:42:08 -05:00
progress: job.progress as number,
priority: job.opts.priority,
2018-07-10 10:02:20 -05:00
error,
createdAt: new Date(job.timestamp),
finishedOn: new Date(job.finishedOn),
processedOn: new Date(job.processedOn)
}
2017-11-30 03:51:13 -06:00
}