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