1
0
Fork 0
peertube/server/controllers/api/jobs.ts

74 lines
1.9 KiB
TypeScript
Raw Normal View History

2017-11-30 04:51:13 -05:00
import * as express from 'express'
import { ResultList } from '../../../shared'
import { Job, JobType, JobState } from '../../../shared/models'
2017-12-12 11:53:50 -05:00
import { UserRight } from '../../../shared/models/users'
import { JobQueue } from '../../lib/job-queue'
import {
asyncMiddleware,
authenticate,
ensureUserHasRight,
jobsSortValidator,
setDefaultPagination,
setDefaultSort
} from '../../middlewares'
2017-12-12 11:53:50 -05:00
import { paginationValidator } from '../../middlewares/validators'
import { listJobsValidator } from '../../middlewares/validators/jobs'
2018-07-10 11:02:20 -04:00
import { isArray } from '../../helpers/custom-validators/misc'
2017-11-30 04:51:13 -05:00
const jobsRouter = express.Router()
jobsRouter.get('/:state',
2017-11-30 04:51:13 -05:00
authenticate,
ensureUserHasRight(UserRight.MANAGE_JOBS),
paginationValidator,
jobsSortValidator,
2018-01-17 04:50:33 -05:00
setDefaultSort,
setDefaultPagination,
2019-12-04 08:49:59 -05:00
listJobsValidator,
2017-11-30 04:51:13 -05:00
asyncMiddleware(listJobs)
)
// ---------------------------------------------------------------------------
export {
jobsRouter
}
// ---------------------------------------------------------------------------
2019-10-21 08:50:55 -04:00
async function listJobs (req: express.Request, res: express.Response) {
const state = req.params.state as JobState
2018-07-10 11:02:20 -04:00
const asc = req.query.sort === 'createdAt'
2019-12-04 08:49:59 -05:00
const jobType = req.query.jobType
2019-12-04 08:49:59 -05:00
const jobs = await JobQueue.Instance.listForApi({
state,
start: req.query.start,
count: req.query.count,
asc,
jobType
})
2018-07-10 11:02:20 -04:00
const total = await JobQueue.Instance.count(state)
const result: ResultList<any> = {
total,
2018-07-10 11:02:20 -04:00
data: jobs.map(j => formatJob(j, state))
}
return res.json(result)
}
2017-11-30 04:51:13 -05:00
2018-07-10 11:02:20 -04:00
function formatJob (job: any, state: JobState): Job {
const error = isArray(job.stacktrace) && job.stacktrace.length !== 0 ? job.stacktrace[0] : null
return {
id: job.id,
2018-07-10 11:02:20 -04:00
state: state,
type: job.queue.name as JobType,
data: job.data,
2018-07-10 11:02:20 -04:00
error,
createdAt: new Date(job.timestamp),
finishedOn: new Date(job.finishedOn),
processedOn: new Date(job.processedOn)
}
2017-11-30 04:51:13 -05:00
}