]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - shared/server-commands/server/jobs-command.ts
Move test functions outside extra-utils
[github/Chocobozzz/PeerTube.git] / shared / server-commands / server / jobs-command.ts
1 import { pick } from '@shared/core-utils'
2 import { HttpStatusCode, Job, JobState, JobType, ResultList } from '@shared/models'
3 import { AbstractCommand, OverrideCommandOptions } from '../shared'
4
5 export class JobsCommand extends AbstractCommand {
6
7 async getLatest (options: OverrideCommandOptions & {
8 jobType: JobType
9 }) {
10 const { data } = await this.list({ ...options, start: 0, count: 1, sort: '-createdAt' })
11
12 if (data.length === 0) return undefined
13
14 return data[0]
15 }
16
17 list (options: OverrideCommandOptions & {
18 state?: JobState
19 jobType?: JobType
20 start?: number
21 count?: number
22 sort?: string
23 } = {}) {
24 const path = this.buildJobsUrl(options.state)
25
26 const query = pick(options, [ 'start', 'count', 'sort', 'jobType' ])
27
28 return this.getRequestBody<ResultList<Job>>({
29 ...options,
30
31 path,
32 query,
33 implicitToken: true,
34 defaultExpectedStatus: HttpStatusCode.OK_200
35 })
36 }
37
38 listFailed (options: OverrideCommandOptions & {
39 jobType?: JobType
40 }) {
41 const path = this.buildJobsUrl('failed')
42
43 return this.getRequestBody<ResultList<Job>>({
44 ...options,
45
46 path,
47 query: { start: 0, count: 50 },
48 implicitToken: true,
49 defaultExpectedStatus: HttpStatusCode.OK_200
50 })
51 }
52
53 private buildJobsUrl (state?: JobState) {
54 let path = '/api/v1/jobs'
55
56 if (state) path += '/' + state
57
58 return path
59 }
60 }