2021-07-29 10:27:24 +02:00
|
|
|
import { pick } from '@shared/core-utils'
|
2021-12-17 11:58:15 +01:00
|
|
|
import { HttpStatusCode, Job, JobState, JobType, ResultList } from '@shared/models'
|
2021-07-07 09:34:56 +02:00
|
|
|
import { AbstractCommand, OverrideCommandOptions } from '../shared'
|
|
|
|
|
|
|
|
export class JobsCommand extends AbstractCommand {
|
|
|
|
|
2021-08-17 08:26:20 +02:00
|
|
|
async getLatest (options: OverrideCommandOptions & {
|
|
|
|
jobType: JobType
|
|
|
|
}) {
|
2021-08-25 15:13:41 +02:00
|
|
|
const { data } = await this.list({ ...options, start: 0, count: 1, sort: '-createdAt' })
|
2021-08-17 08:26:20 +02:00
|
|
|
|
|
|
|
if (data.length === 0) return undefined
|
|
|
|
|
|
|
|
return data[0]
|
|
|
|
}
|
|
|
|
|
2022-01-19 14:23:00 +01:00
|
|
|
pauseJobQueue (options: OverrideCommandOptions = {}) {
|
|
|
|
const path = '/api/v1/jobs/pause'
|
|
|
|
|
|
|
|
return this.postBodyRequest({
|
|
|
|
...options,
|
|
|
|
|
|
|
|
path,
|
|
|
|
implicitToken: true,
|
|
|
|
defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
resumeJobQueue (options: OverrideCommandOptions = {}) {
|
|
|
|
const path = '/api/v1/jobs/resume'
|
|
|
|
|
|
|
|
return this.postBodyRequest({
|
|
|
|
...options,
|
|
|
|
|
|
|
|
path,
|
|
|
|
implicitToken: true,
|
|
|
|
defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-08-25 15:13:41 +02:00
|
|
|
list (options: OverrideCommandOptions & {
|
2021-07-07 09:34:56 +02:00
|
|
|
state?: JobState
|
|
|
|
jobType?: JobType
|
|
|
|
start?: number
|
|
|
|
count?: number
|
|
|
|
sort?: string
|
|
|
|
} = {}) {
|
|
|
|
const path = this.buildJobsUrl(options.state)
|
|
|
|
|
|
|
|
const query = pick(options, [ 'start', 'count', 'sort', 'jobType' ])
|
|
|
|
|
|
|
|
return this.getRequestBody<ResultList<Job>>({
|
|
|
|
...options,
|
|
|
|
|
|
|
|
path,
|
|
|
|
query,
|
2021-07-08 10:55:16 +02:00
|
|
|
implicitToken: true,
|
2021-07-07 09:34:56 +02:00
|
|
|
defaultExpectedStatus: HttpStatusCode.OK_200
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-11-18 14:35:08 +01:00
|
|
|
listFailed (options: OverrideCommandOptions & {
|
|
|
|
jobType?: JobType
|
|
|
|
}) {
|
|
|
|
const path = this.buildJobsUrl('failed')
|
|
|
|
|
|
|
|
return this.getRequestBody<ResultList<Job>>({
|
|
|
|
...options,
|
|
|
|
|
|
|
|
path,
|
|
|
|
query: { start: 0, count: 50 },
|
|
|
|
implicitToken: true,
|
|
|
|
defaultExpectedStatus: HttpStatusCode.OK_200
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-07-07 09:34:56 +02:00
|
|
|
private buildJobsUrl (state?: JobState) {
|
|
|
|
let path = '/api/v1/jobs'
|
|
|
|
|
|
|
|
if (state) path += '/' + state
|
|
|
|
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
}
|