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