1
0
Fork 0
peertube/server/controllers/api/jobs.ts
Chocobozzz 94a5ff8a4a
Move job queue to redis
We'll use it as cache in the future.

/!\ You'll loose your old jobs (pending jobs too) so upgrade only when
you don't have pending job anymore.
2018-01-25 18:41:17 +01:00

61 lines
1.7 KiB
TypeScript

import * as express from 'express'
import { ResultList } from '../../../shared'
import { Job, JobType, JobState } from '../../../shared/models'
import { UserRight } from '../../../shared/models/users'
import { JobQueue } from '../../lib/job-queue'
import {
asyncMiddleware,
authenticate,
ensureUserHasRight,
jobsSortValidator,
setDefaultPagination,
setDefaultSort
} from '../../middlewares'
import { paginationValidator } from '../../middlewares/validators'
import { listJobsValidator } from '../../middlewares/validators/jobs'
const jobsRouter = express.Router()
jobsRouter.get('/:state',
authenticate,
ensureUserHasRight(UserRight.MANAGE_JOBS),
paginationValidator,
jobsSortValidator,
setDefaultSort,
setDefaultPagination,
asyncMiddleware(listJobsValidator),
asyncMiddleware(listJobs)
)
// ---------------------------------------------------------------------------
export {
jobsRouter
}
// ---------------------------------------------------------------------------
async function listJobs (req: express.Request, res: express.Response, next: express.NextFunction) {
const sort = req.query.sort === 'createdAt' ? 'asc' : 'desc'
const jobs = await JobQueue.Instance.listForApi(req.params.state, req.query.start, req.query.count, sort)
const total = await JobQueue.Instance.count(req.params.state)
const result: ResultList<any> = {
total,
data: jobs.map(j => formatJob(j.toJSON()))
}
return res.json(result)
}
function formatJob (job: any): Job {
return {
id: job.id,
state: job.state as JobState,
type: job.type as JobType,
data: job.data,
error: job.error,
createdAt: new Date(parseInt(job.created_at, 10)),
updatedAt: new Date(parseInt(job.updated_at, 10))
}
}