aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/server-commands/server/jobs-command.ts
blob: b8790ea00df78ebb8b9969f49ec75f047bc57974 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import { pick } from '@shared/core-utils'
import { HttpStatusCode, Job, JobState, JobType, ResultList } from '@shared/models'
import { AbstractCommand, OverrideCommandOptions } from '../shared'

export class JobsCommand extends AbstractCommand {

  async getLatest (options: OverrideCommandOptions & {
    jobType: JobType
  }) {
    const { data } = await this.list({ ...options, start: 0, count: 1, sort: '-createdAt' })

    if (data.length === 0) return undefined

    return data[0]
  }

  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
    })
  }

  list (options: OverrideCommandOptions & {
    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,
      implicitToken: true,
      defaultExpectedStatus: HttpStatusCode.OK_200
    })
  }

  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
    })
  }

  private buildJobsUrl (state?: JobState) {
    let path = '/api/v1/jobs'

    if (state) path += '/' + state

    return path
  }
}