aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/+admin/system/runners/runner.service.ts
blob: 392ec82bcae79b472c0530a8ad4395e1eabdc361 (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import { SortMeta } from 'primeng/api'
import { catchError, concatMap, forkJoin, from, map, toArray } from 'rxjs'
import { HttpClient, HttpParams } from '@angular/common/http'
import { Injectable } from '@angular/core'
import { RestExtractor, RestPagination, RestService, ServerService } from '@app/core'
import { arrayify, peertubeTranslate } from '@shared/core-utils'
import { ResultList } from '@shared/models/common'
import { Runner, RunnerJob, RunnerJobAdmin, RunnerRegistrationToken } from '@shared/models/runners'
import { environment } from '../../../../environments/environment'

export type RunnerJobFormatted = RunnerJob & {
  payload: string
  privatePayload: string
}

@Injectable()
export class RunnerService {
  private static BASE_RUNNER_URL = environment.apiUrl + '/api/v1/runners'

  constructor (
    private authHttp: HttpClient,
    private server: ServerService,
    private restService: RestService,
    private restExtractor: RestExtractor
  ) {}

  listRegistrationTokens (options: {
    pagination: RestPagination
    sort: SortMeta
  }) {
    const { pagination, sort } = options

    let params = new HttpParams()
    params = this.restService.addRestGetParams(params, pagination, sort)

    return this.authHttp.get<ResultList<RunnerRegistrationToken>>(RunnerService.BASE_RUNNER_URL + '/registration-tokens', { params })
                .pipe(catchError(res => this.restExtractor.handleError(res)))
  }

  generateToken () {
    return this.authHttp.post(RunnerService.BASE_RUNNER_URL + '/registration-tokens/generate', {})
      .pipe(catchError(res => this.restExtractor.handleError(res)))
  }

  removeToken (token: RunnerRegistrationToken) {
    return this.authHttp.delete(RunnerService.BASE_RUNNER_URL + '/registration-tokens/' + token.id)
      .pipe(catchError(res => this.restExtractor.handleError(res)))
  }

  // ---------------------------------------------------------------------------

  listRunnerJobs (options: {
    pagination: RestPagination
    sort: SortMeta
    search?: string
  }) {
    const { pagination, sort, search } = options

    let params = new HttpParams()
    params = this.restService.addRestGetParams(params, pagination, sort)

    if (search) params = params.append('search', search)

    return forkJoin([
      this.authHttp.get<ResultList<RunnerJobAdmin>>(RunnerService.BASE_RUNNER_URL + '/jobs', { params }),
      this.server.getServerLocale()
    ]).pipe(
      map(([ res, translations ]) => {
        const newData = res.data.map(job => {
          return {
            ...job,

            state: {
              id: job.state.id,
              label: peertubeTranslate(job.state.label, translations)
            },
            payload: JSON.stringify(job.payload, null, 2),
            privatePayload: JSON.stringify(job.privatePayload, null, 2)
          } as RunnerJobFormatted
        })

        return {
          total: res.total,
          data: newData
        }
      }),
      map(res => this.restExtractor.convertResultListDateToHuman(res, [ 'createdAt', 'startedAt', 'finishedAt' ], 'precise')),
      catchError(res => this.restExtractor.handleError(res))
    )
  }

  cancelJobs (jobsArg: RunnerJob | RunnerJob[]) {
    const jobs = arrayify(jobsArg)

    return from(jobs)
      .pipe(
        concatMap(job => this.authHttp.post(RunnerService.BASE_RUNNER_URL + '/jobs/' + job.uuid + '/cancel', {})),
        toArray(),
        catchError(err => this.restExtractor.handleError(err))
      )
  }

  // ---------------------------------------------------------------------------

  listRunners (options: {
    pagination: RestPagination
    sort: SortMeta
  }) {
    const { pagination, sort } = options

    let params = new HttpParams()
    params = this.restService.addRestGetParams(params, pagination, sort)

    return this.authHttp.get<ResultList<Runner>>(RunnerService.BASE_RUNNER_URL + '/', { params })
                .pipe(catchError(res => this.restExtractor.handleError(res)))
  }

  deleteRunner (runner: Runner) {
    return this.authHttp.delete(RunnerService.BASE_RUNNER_URL + '/' + runner.id)
      .pipe(catchError(res => this.restExtractor.handleError(res)))
  }
}