2021-08-27 08:32:44 -04:00
|
|
|
import express from 'express'
|
2018-01-25 09:05:18 -05:00
|
|
|
import { ResultList } from '../../../shared'
|
2020-12-14 06:00:35 -05:00
|
|
|
import { Job, JobState, JobType } from '../../../shared/models'
|
2017-12-12 11:53:50 -05:00
|
|
|
import { UserRight } from '../../../shared/models/users'
|
2020-12-14 06:00:35 -05:00
|
|
|
import { isArray } from '../../helpers/custom-validators/misc'
|
2018-01-25 09:05:18 -05:00
|
|
|
import { JobQueue } from '../../lib/job-queue'
|
2018-01-18 04:53:54 -05:00
|
|
|
import {
|
2018-01-25 09:05:18 -05:00
|
|
|
asyncMiddleware,
|
|
|
|
authenticate,
|
|
|
|
ensureUserHasRight,
|
|
|
|
jobsSortValidator,
|
2021-06-04 03:16:23 -04:00
|
|
|
openapiOperationDoc,
|
2021-03-09 03:22:05 -05:00
|
|
|
paginationValidatorBuilder,
|
2018-01-25 09:05:18 -05:00
|
|
|
setDefaultPagination,
|
2018-01-18 04:53:54 -05:00
|
|
|
setDefaultSort
|
|
|
|
} from '../../middlewares'
|
2020-12-14 06:00:35 -05:00
|
|
|
import { listJobsValidator } from '../../middlewares/validators/jobs'
|
2017-11-30 04:51:13 -05:00
|
|
|
|
|
|
|
const jobsRouter = express.Router()
|
|
|
|
|
2020-12-14 06:00:35 -05:00
|
|
|
jobsRouter.get('/:state?',
|
2021-06-04 03:16:23 -04:00
|
|
|
openapiOperationDoc({ operationId: 'getJobs' }),
|
2020-12-13 13:27:25 -05:00
|
|
|
authenticate,
|
|
|
|
ensureUserHasRight(UserRight.MANAGE_JOBS),
|
2021-03-09 03:22:05 -05:00
|
|
|
paginationValidatorBuilder([ 'jobs' ]),
|
2020-12-13 13:27:25 -05:00
|
|
|
jobsSortValidator,
|
|
|
|
setDefaultSort,
|
|
|
|
setDefaultPagination,
|
|
|
|
listJobsValidator,
|
|
|
|
asyncMiddleware(listJobs)
|
|
|
|
)
|
|
|
|
|
2017-11-30 04:51:13 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
jobsRouter
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2019-10-21 08:50:55 -04:00
|
|
|
async function listJobs (req: express.Request, res: express.Response) {
|
2020-12-14 06:00:35 -05:00
|
|
|
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
|
2018-01-25 09:05:18 -05:00
|
|
|
|
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
|
|
|
|
})
|
2020-11-09 10:13:03 -05:00
|
|
|
const total = await JobQueue.Instance.count(state, jobType)
|
2018-01-25 09:05:18 -05:00
|
|
|
|
2020-01-31 10:56:52 -05:00
|
|
|
const result: ResultList<Job> = {
|
2018-01-25 09:05:18 -05:00
|
|
|
total,
|
2021-01-21 08:42:43 -05:00
|
|
|
data: await Promise.all(jobs.map(j => formatJob(j, state)))
|
2018-01-25 09:05:18 -05:00
|
|
|
}
|
2020-12-14 06:00:35 -05:00
|
|
|
|
2018-01-25 09:05:18 -05:00
|
|
|
return res.json(result)
|
|
|
|
}
|
2017-11-30 04:51:13 -05:00
|
|
|
|
2021-01-21 08:42:43 -05:00
|
|
|
async function formatJob (job: any, state?: JobState): Promise<Job> {
|
2020-12-14 06:00:35 -05:00
|
|
|
const error = isArray(job.stacktrace) && job.stacktrace.length !== 0
|
|
|
|
? job.stacktrace[0]
|
|
|
|
: null
|
2018-07-10 11:02:20 -04:00
|
|
|
|
2018-01-25 09:05:18 -05:00
|
|
|
return {
|
|
|
|
id: job.id,
|
2021-01-21 08:42:43 -05:00
|
|
|
state: state || await job.getState(),
|
2018-07-10 11:02:20 -04:00
|
|
|
type: job.queue.name as JobType,
|
2018-01-25 09:05:18 -05:00
|
|
|
data: job.data,
|
2021-01-21 08:42:43 -05:00
|
|
|
progress: await job.progress(),
|
2021-01-21 10:57:21 -05:00
|
|
|
priority: job.opts.priority,
|
2018-07-10 11:02:20 -04:00
|
|
|
error,
|
|
|
|
createdAt: new Date(job.timestamp),
|
|
|
|
finishedOn: new Date(job.finishedOn),
|
|
|
|
processedOn: new Date(job.processedOn)
|
2018-01-25 09:05:18 -05:00
|
|
|
}
|
2017-11-30 04:51:13 -05:00
|
|
|
}
|