2 import { SortMeta } from 'primeng/api'
3 import { catchError, concatMap, forkJoin, from, map, toArray } from 'rxjs'
4 import { HttpClient, HttpParams } from '@angular/common/http'
5 import { Injectable } from '@angular/core'
6 import { RestExtractor, RestPagination, RestService, ServerService } from '@app/core'
7 import { arrayify, peertubeTranslate } from '@shared/core-utils'
8 import { ResultList } from '@shared/models/common'
9 import { Runner, RunnerJob, RunnerJobAdmin, RunnerRegistrationToken } from '@shared/models/runners'
10 import { environment } from '../../../../environments/environment'
12 export type RunnerJobFormatted = RunnerJob & {
14 privatePayload: string
18 export class RunnerService {
19 private static BASE_RUNNER_URL = environment.apiUrl + '/api/v1/runners'
22 private authHttp: HttpClient,
23 private server: ServerService,
24 private restService: RestService,
25 private restExtractor: RestExtractor
28 listRegistrationTokens (options: {
29 pagination: RestPagination
32 const { pagination, sort } = options
34 let params = new HttpParams()
35 params = this.restService.addRestGetParams(params, pagination, sort)
37 return this.authHttp.get<ResultList<RunnerRegistrationToken>>(RunnerService.BASE_RUNNER_URL + '/registration-tokens', { params })
38 .pipe(catchError(res => this.restExtractor.handleError(res)))
42 return this.authHttp.post(RunnerService.BASE_RUNNER_URL + '/registration-tokens/generate', {})
43 .pipe(catchError(res => this.restExtractor.handleError(res)))
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)))
51 // ---------------------------------------------------------------------------
53 listRunnerJobs (options: {
54 pagination: RestPagination
58 const { pagination, sort, search } = options
60 let params = new HttpParams()
61 params = this.restService.addRestGetParams(params, pagination, sort)
63 if (search) params = params.append('search', search)
66 this.authHttp.get<ResultList<RunnerJobAdmin>>(RunnerService.BASE_RUNNER_URL + '/jobs', { params }),
67 this.server.getServerLocale()
69 map(([ res, translations ]) => {
70 const newData = res.data.map(job => {
76 label: peertubeTranslate(job.state.label, translations)
78 payload: JSON.stringify(job.payload, null, 2),
79 privatePayload: JSON.stringify(job.privatePayload, null, 2)
80 } as RunnerJobFormatted
88 map(res => this.restExtractor.convertResultListDateToHuman(res, [ 'createdAt', 'startedAt', 'finishedAt' ], 'precise')),
89 catchError(res => this.restExtractor.handleError(res))
93 cancelJobs (jobsArg: RunnerJob | RunnerJob[]) {
94 const jobs = arrayify(jobsArg)
98 concatMap(job => this.authHttp.post(RunnerService.BASE_RUNNER_URL + '/jobs/' + job.uuid + '/cancel', {})),
100 catchError(err => this.restExtractor.handleError(err))
104 // ---------------------------------------------------------------------------
106 listRunners (options: {
107 pagination: RestPagination
110 const { pagination, sort } = options
112 let params = new HttpParams()
113 params = this.restService.addRestGetParams(params, pagination, sort)
115 return this.authHttp.get<ResultList<Runner>>(RunnerService.BASE_RUNNER_URL + '/', { params })
116 .pipe(catchError(res => this.restExtractor.handleError(res)))
119 deleteRunner (runner: Runner) {
120 return this.authHttp.delete(RunnerService.BASE_RUNNER_URL + '/' + runner.id)
121 .pipe(catchError(res => this.restExtractor.handleError(res)))