mirror of
https://github.com/Chocobozzz/PeerTube.git
synced 2025-10-05 19:42:24 +02:00
server/server -> server/core
This commit is contained in:
parent
114327d4ce
commit
5a3d0650c9
838 changed files with 111 additions and 111 deletions
109
server/core/controllers/api/jobs.ts
Normal file
109
server/core/controllers/api/jobs.ts
Normal file
|
@ -0,0 +1,109 @@
|
|||
import { Job as BullJob } from 'bullmq'
|
||||
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 {
|
||||
apiRateLimiter,
|
||||
asyncMiddleware,
|
||||
authenticate,
|
||||
ensureUserHasRight,
|
||||
jobsSortValidator,
|
||||
openapiOperationDoc,
|
||||
paginationValidatorBuilder,
|
||||
setDefaultPagination,
|
||||
setDefaultSort
|
||||
} from '../../middlewares/index.js'
|
||||
import { listJobsValidator } from '../../middlewares/validators/jobs.js'
|
||||
|
||||
const jobsRouter = express.Router()
|
||||
|
||||
jobsRouter.use(apiRateLimiter)
|
||||
|
||||
jobsRouter.post('/pause',
|
||||
authenticate,
|
||||
ensureUserHasRight(UserRight.MANAGE_JOBS),
|
||||
asyncMiddleware(pauseJobQueue)
|
||||
)
|
||||
|
||||
jobsRouter.post('/resume',
|
||||
authenticate,
|
||||
ensureUserHasRight(UserRight.MANAGE_JOBS),
|
||||
resumeJobQueue
|
||||
)
|
||||
|
||||
jobsRouter.get('/:state?',
|
||||
openapiOperationDoc({ operationId: 'getJobs' }),
|
||||
authenticate,
|
||||
ensureUserHasRight(UserRight.MANAGE_JOBS),
|
||||
paginationValidatorBuilder([ 'jobs' ]),
|
||||
jobsSortValidator,
|
||||
setDefaultSort,
|
||||
setDefaultPagination,
|
||||
listJobsValidator,
|
||||
asyncMiddleware(listJobs)
|
||||
)
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export {
|
||||
jobsRouter
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
async function pauseJobQueue (req: express.Request, res: express.Response) {
|
||||
await JobQueue.Instance.pause()
|
||||
|
||||
return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
|
||||
}
|
||||
|
||||
function resumeJobQueue (req: express.Request, res: express.Response) {
|
||||
JobQueue.Instance.resume()
|
||||
|
||||
return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
|
||||
}
|
||||
|
||||
async function listJobs (req: express.Request, res: express.Response) {
|
||||
const state = req.params.state as JobState
|
||||
const asc = req.query.sort === 'createdAt'
|
||||
const jobType = req.query.jobType
|
||||
|
||||
const jobs = await JobQueue.Instance.listForApi({
|
||||
state,
|
||||
start: req.query.start,
|
||||
count: req.query.count,
|
||||
asc,
|
||||
jobType
|
||||
})
|
||||
const total = await JobQueue.Instance.count(state, jobType)
|
||||
|
||||
const result: ResultList<Job> = {
|
||||
total,
|
||||
data: await Promise.all(jobs.map(j => formatJob(j, state)))
|
||||
}
|
||||
|
||||
return res.json(result)
|
||||
}
|
||||
|
||||
async function formatJob (job: BullJob, state?: JobState): Promise<Job> {
|
||||
const error = isArray(job.stacktrace) && job.stacktrace.length !== 0
|
||||
? job.stacktrace[0]
|
||||
: null
|
||||
|
||||
return {
|
||||
id: job.id,
|
||||
state: state || await job.getState(),
|
||||
type: job.queueName as JobType,
|
||||
data: job.data,
|
||||
parent: job.parent
|
||||
? { id: job.parent.id }
|
||||
: undefined,
|
||||
progress: job.progress as number,
|
||||
priority: job.opts.priority,
|
||||
error,
|
||||
createdAt: new Date(job.timestamp),
|
||||
finishedOn: new Date(job.finishedOn),
|
||||
processedOn: new Date(job.processedOn)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue