]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/controllers/api/jobs.ts
Add user video list hooks
[github/Chocobozzz/PeerTube.git] / server / controllers / api / jobs.ts
index 132d110ad6b72bdb5c4e03d44364959ad526b639..e14ea25758e2f286696aad302144923d1cb9dada 100644 (file)
@@ -1,7 +1,8 @@
 import * as express from 'express'
 import { ResultList } from '../../../shared'
-import { Job, JobType, JobState } from '../../../shared/models'
+import { Job, JobState, JobType } from '../../../shared/models'
 import { UserRight } from '../../../shared/models/users'
+import { isArray } from '../../helpers/custom-validators/misc'
 import { JobQueue } from '../../lib/job-queue'
 import {
   asyncMiddleware,
@@ -16,14 +17,14 @@ import { listJobsValidator } from '../../middlewares/validators/jobs'
 
 const jobsRouter = express.Router()
 
-jobsRouter.get('/:state',
+jobsRouter.get('/:state?',
   authenticate,
   ensureUserHasRight(UserRight.MANAGE_JOBS),
   paginationValidator,
   jobsSortValidator,
   setDefaultSort,
   setDefaultPagination,
-  asyncMiddleware(listJobsValidator),
+  listJobsValidator,
   asyncMiddleware(listJobs)
 )
 
@@ -35,27 +36,47 @@ export {
 
 // ---------------------------------------------------------------------------
 
-async function listJobs (req: express.Request, res: express.Response, next: express.NextFunction) {
-  const sort = req.query.sort === 'createdAt' ? 'asc' : 'desc'
+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(req.params.state, req.query.start, req.query.count, sort)
-  const total = await JobQueue.Instance.count(req.params.state)
+  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<any> = {
+  const result: ResultList<Job> = {
     total,
-    data: jobs.map(j => formatJob(j.toJSON()))
+    data: state
+      ? jobs.map(j => formatJob(j, state))
+      : await Promise.all(jobs.map(j => formatJobWithUnknownState(j)))
   }
+
   return res.json(result)
 }
 
-function formatJob (job: any): Job {
+async function formatJobWithUnknownState (job: any) {
+  return formatJob(job, await job.getState())
+}
+
+function formatJob (job: any, state: JobState): Job {
+  const error = isArray(job.stacktrace) && job.stacktrace.length !== 0
+    ? job.stacktrace[0]
+    : null
+
   return {
     id: job.id,
-    state: job.state as JobState,
-    type: job.type as JobType,
+    state: state,
+    type: job.queue.name as JobType,
     data: job.data,
-    error: job.error,
-    createdAt: new Date(parseInt(job.created_at, 10)),
-    updatedAt: new Date(parseInt(job.updated_at, 10))
+    error,
+    createdAt: new Date(job.timestamp),
+    finishedOn: new Date(job.finishedOn),
+    processedOn: new Date(job.processedOn)
   }
 }