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