aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/server-commands/server/jobs-command.ts
diff options
context:
space:
mode:
Diffstat (limited to 'shared/server-commands/server/jobs-command.ts')
-rw-r--r--shared/server-commands/server/jobs-command.ts61
1 files changed, 61 insertions, 0 deletions
diff --git a/shared/server-commands/server/jobs-command.ts b/shared/server-commands/server/jobs-command.ts
new file mode 100644
index 000000000..6636e7e4d
--- /dev/null
+++ b/shared/server-commands/server/jobs-command.ts
@@ -0,0 +1,61 @@
1import { pick } from '@shared/core-utils'
2import { HttpStatusCode } from '@shared/models'
3import { Job, JobState, JobType, ResultList } from '../../models'
4import { AbstractCommand, OverrideCommandOptions } from '../shared'
5
6export class JobsCommand extends AbstractCommand {
7
8 async getLatest (options: OverrideCommandOptions & {
9 jobType: JobType
10 }) {
11 const { data } = await this.list({ ...options, start: 0, count: 1, sort: '-createdAt' })
12
13 if (data.length === 0) return undefined
14
15 return data[0]
16 }
17
18 list (options: OverrideCommandOptions & {
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,
34 implicitToken: true,
35 defaultExpectedStatus: HttpStatusCode.OK_200
36 })
37 }
38
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
54 private buildJobsUrl (state?: JobState) {
55 let path = '/api/v1/jobs'
56
57 if (state) path += '/' + state
58
59 return path
60 }
61}