]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/controllers/api/jobs.ts
Fix incorrect error logs
[github/Chocobozzz/PeerTube.git] / server / controllers / api / jobs.ts
index 180a3fca6bb820d115b74f1712a936716399b690..c61b7362f22a8277bbf5cd49161c5056d836f9ec 100644 (file)
@@ -1,19 +1,42 @@
-import * as express from 'express'
-import { UserRight } from '../../../shared/models/users'
-import { getFormattedObjects } from '../../helpers/utils'
-import { asyncMiddleware, authenticate, ensureUserHasRight, jobsSortValidator, setDefaultSort, setPagination } from '../../middlewares'
-import { paginationValidator } from '../../middlewares/validators'
-import { JobModel } from '../../models/job/job'
+import express from 'express'
+import { HttpStatusCode, Job, JobState, JobType, ResultList, UserRight } from '@shared/models'
+import { isArray } from '../../helpers/custom-validators/misc'
+import { JobQueue } from '../../lib/job-queue'
+import {
+  asyncMiddleware,
+  authenticate,
+  ensureUserHasRight,
+  jobsSortValidator,
+  openapiOperationDoc,
+  paginationValidatorBuilder,
+  setDefaultPagination,
+  setDefaultSort
+} from '../../middlewares'
+import { listJobsValidator } from '../../middlewares/validators/jobs'
 
 const jobsRouter = express.Router()
 
-jobsRouter.get('/',
+jobsRouter.post('/pause',
+  authenticate,
+  ensureUserHasRight(UserRight.MANAGE_JOBS),
+  asyncMiddleware(pauseJobQueue)
+)
+
+jobsRouter.post('/resume',
   authenticate,
   ensureUserHasRight(UserRight.MANAGE_JOBS),
-  paginationValidator,
+  asyncMiddleware(resumeJobQueue)
+)
+
+jobsRouter.get('/:state?',
+  openapiOperationDoc({ operationId: 'getJobs' }),
+  authenticate,
+  ensureUserHasRight(UserRight.MANAGE_JOBS),
+  paginationValidatorBuilder([ 'jobs' ]),
   jobsSortValidator,
   setDefaultSort,
-  setPagination,
+  setDefaultPagination,
+  listJobsValidator,
   asyncMiddleware(listJobs)
 )
 
@@ -25,8 +48,55 @@ export {
 
 // ---------------------------------------------------------------------------
 
-async function listJobs (req: express.Request, res: express.Response, next: express.NextFunction) {
-  const resultList = await JobModel.listForApi(req.query.start, req.query.count, req.query.sort)
+async function pauseJobQueue (req: express.Request, res: express.Response) {
+  await JobQueue.Instance.pause()
+
+  return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
+}
+
+async function resumeJobQueue (req: express.Request, res: express.Response) {
+  await 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: any, state?: JobState): Promise<Job> {
+  const error = isArray(job.stacktrace) && job.stacktrace.length !== 0
+    ? job.stacktrace[0]
+    : null
 
-  return res.json(getFormattedObjects(resultList.data, resultList.total))
+  return {
+    id: job.id,
+    state: state || await job.getState(),
+    type: job.queue.name as JobType,
+    data: job.data,
+    progress: await job.progress(),
+    priority: job.opts.priority,
+    error,
+    createdAt: new Date(job.timestamp),
+    finishedOn: new Date(job.finishedOn),
+    processedOn: new Date(job.processedOn)
+  }
 }