]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+admin/system/runners/runner.service.ts
Bumped to version v5.2.1
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / system / runners / runner.service.ts
CommitLineData
118626c8
C
1
2import { SortMeta } from 'primeng/api'
476ce1d7 3import { catchError, concatMap, forkJoin, from, map, toArray } from 'rxjs'
118626c8
C
4import { HttpClient, HttpParams } from '@angular/common/http'
5import { Injectable } from '@angular/core'
6import { RestExtractor, RestPagination, RestService, ServerService } from '@app/core'
476ce1d7 7import { arrayify, peertubeTranslate } from '@shared/core-utils'
118626c8
C
8import { ResultList } from '@shared/models/common'
9import { Runner, RunnerJob, RunnerJobAdmin, RunnerRegistrationToken } from '@shared/models/runners'
10import { environment } from '../../../../environments/environment'
11
12export type RunnerJobFormatted = RunnerJob & {
13 payload: string
14 privatePayload: string
15}
16
17@Injectable()
18export class RunnerService {
19 private static BASE_RUNNER_URL = environment.apiUrl + '/api/v1/runners'
20
21 constructor (
22 private authHttp: HttpClient,
23 private server: ServerService,
24 private restService: RestService,
25 private restExtractor: RestExtractor
26 ) {}
27
28 listRegistrationTokens (options: {
29 pagination: RestPagination
30 sort: SortMeta
31 }) {
32 const { pagination, sort } = options
33
34 let params = new HttpParams()
35 params = this.restService.addRestGetParams(params, pagination, sort)
36
37 return this.authHttp.get<ResultList<RunnerRegistrationToken>>(RunnerService.BASE_RUNNER_URL + '/registration-tokens', { params })
38 .pipe(catchError(res => this.restExtractor.handleError(res)))
39 }
40
41 generateToken () {
42 return this.authHttp.post(RunnerService.BASE_RUNNER_URL + '/registration-tokens/generate', {})
43 .pipe(catchError(res => this.restExtractor.handleError(res)))
44 }
45
46 removeToken (token: RunnerRegistrationToken) {
47 return this.authHttp.delete(RunnerService.BASE_RUNNER_URL + '/registration-tokens/' + token.id)
48 .pipe(catchError(res => this.restExtractor.handleError(res)))
49 }
50
51 // ---------------------------------------------------------------------------
52
53 listRunnerJobs (options: {
54 pagination: RestPagination
55 sort: SortMeta
56 search?: string
57 }) {
58 const { pagination, sort, search } = options
59
60 let params = new HttpParams()
61 params = this.restService.addRestGetParams(params, pagination, sort)
62
63 if (search) params = params.append('search', search)
64
65 return forkJoin([
66 this.authHttp.get<ResultList<RunnerJobAdmin>>(RunnerService.BASE_RUNNER_URL + '/jobs', { params }),
67 this.server.getServerLocale()
68 ]).pipe(
69 map(([ res, translations ]) => {
70 const newData = res.data.map(job => {
71 return {
72 ...job,
73
74 state: {
75 id: job.state.id,
76 label: peertubeTranslate(job.state.label, translations)
77 },
78 payload: JSON.stringify(job.payload, null, 2),
79 privatePayload: JSON.stringify(job.privatePayload, null, 2)
80 } as RunnerJobFormatted
81 })
82
83 return {
84 total: res.total,
85 data: newData
86 }
87 }),
88 map(res => this.restExtractor.convertResultListDateToHuman(res, [ 'createdAt', 'startedAt', 'finishedAt' ], 'precise')),
89 catchError(res => this.restExtractor.handleError(res))
90 )
91 }
92
476ce1d7
C
93 cancelJobs (jobsArg: RunnerJob | RunnerJob[]) {
94 const jobs = arrayify(jobsArg)
95
96 return from(jobs)
97 .pipe(
98 concatMap(job => this.authHttp.post(RunnerService.BASE_RUNNER_URL + '/jobs/' + job.uuid + '/cancel', {})),
99 toArray(),
100 catchError(err => this.restExtractor.handleError(err))
101 )
118626c8
C
102 }
103
104 // ---------------------------------------------------------------------------
105
106 listRunners (options: {
107 pagination: RestPagination
108 sort: SortMeta
109 }) {
110 const { pagination, sort } = options
111
112 let params = new HttpParams()
113 params = this.restService.addRestGetParams(params, pagination, sort)
114
115 return this.authHttp.get<ResultList<Runner>>(RunnerService.BASE_RUNNER_URL + '/', { params })
116 .pipe(catchError(res => this.restExtractor.handleError(res)))
117 }
118
119 deleteRunner (runner: Runner) {
120 return this.authHttp.delete(RunnerService.BASE_RUNNER_URL + '/' + runner.id)
121 .pipe(catchError(res => this.restExtractor.handleError(res)))
122 }
123}