diff options
author | Chocobozzz <me@florianbigard.com> | 2023-07-31 14:34:36 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2023-08-11 15:02:33 +0200 |
commit | 3a4992633ee62d5edfbb484d9c6bcb3cf158489d (patch) | |
tree | e4510b39bdac9c318fdb4b47018d08f15368b8f0 /packages/server-commands/src/runners | |
parent | 04d1da5621d25d59bd5fa1543b725c497bf5d9a8 (diff) | |
download | PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.gz PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.zst PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.zip |
Migrate server to ESM
Sorry for the very big commit that may lead to git log issues and merge
conflicts, but it's a major step forward:
* Server can be faster at startup because imports() are async and we can
easily lazy import big modules
* Angular doesn't seem to support ES import (with .js extension), so we
had to correctly organize peertube into a monorepo:
* Use yarn workspace feature
* Use typescript reference projects for dependencies
* Shared projects have been moved into "packages", each one is now a
node module (with a dedicated package.json/tsconfig.json)
* server/tools have been moved into apps/ and is now a dedicated app
bundled and published on NPM so users don't have to build peertube
cli tools manually
* server/tests have been moved into packages/ so we don't compile
them every time we want to run the server
* Use isolatedModule option:
* Had to move from const enum to const
(https://www.typescriptlang.org/docs/handbook/enums.html#objects-vs-enums)
* Had to explictely specify "type" imports when used in decorators
* Prefer tsx (that uses esbuild under the hood) instead of ts-node to
load typescript files (tests with mocha or scripts):
* To reduce test complexity as esbuild doesn't support decorator
metadata, we only test server files that do not import server
models
* We still build tests files into js files for a faster CI
* Remove unmaintained peertube CLI import script
* Removed some barrels to speed up execution (less imports)
Diffstat (limited to 'packages/server-commands/src/runners')
4 files changed, 440 insertions, 0 deletions
diff --git a/packages/server-commands/src/runners/index.ts b/packages/server-commands/src/runners/index.ts new file mode 100644 index 000000000..c868fa78e --- /dev/null +++ b/packages/server-commands/src/runners/index.ts | |||
@@ -0,0 +1,3 @@ | |||
1 | export * from './runner-jobs-command.js' | ||
2 | export * from './runner-registration-tokens-command.js' | ||
3 | export * from './runners-command.js' | ||
diff --git a/packages/server-commands/src/runners/runner-jobs-command.ts b/packages/server-commands/src/runners/runner-jobs-command.ts new file mode 100644 index 000000000..4e702199f --- /dev/null +++ b/packages/server-commands/src/runners/runner-jobs-command.ts | |||
@@ -0,0 +1,297 @@ | |||
1 | import { omit, pick, wait } from '@peertube/peertube-core-utils' | ||
2 | import { | ||
3 | AbortRunnerJobBody, | ||
4 | AcceptRunnerJobBody, | ||
5 | AcceptRunnerJobResult, | ||
6 | ErrorRunnerJobBody, | ||
7 | HttpStatusCode, | ||
8 | isHLSTranscodingPayloadSuccess, | ||
9 | isLiveRTMPHLSTranscodingUpdatePayload, | ||
10 | isWebVideoOrAudioMergeTranscodingPayloadSuccess, | ||
11 | ListRunnerJobsQuery, | ||
12 | RequestRunnerJobBody, | ||
13 | RequestRunnerJobResult, | ||
14 | ResultList, | ||
15 | RunnerJobAdmin, | ||
16 | RunnerJobLiveRTMPHLSTranscodingPayload, | ||
17 | RunnerJobPayload, | ||
18 | RunnerJobState, | ||
19 | RunnerJobStateType, | ||
20 | RunnerJobSuccessBody, | ||
21 | RunnerJobSuccessPayload, | ||
22 | RunnerJobType, | ||
23 | RunnerJobUpdateBody, | ||
24 | RunnerJobVODPayload, | ||
25 | VODHLSTranscodingSuccess, | ||
26 | VODWebVideoTranscodingSuccess | ||
27 | } from '@peertube/peertube-models' | ||
28 | import { unwrapBody } from '../requests/index.js' | ||
29 | import { waitJobs } from '../server/jobs.js' | ||
30 | import { AbstractCommand, OverrideCommandOptions } from '../shared/index.js' | ||
31 | |||
32 | export class RunnerJobsCommand extends AbstractCommand { | ||
33 | |||
34 | list (options: OverrideCommandOptions & ListRunnerJobsQuery = {}) { | ||
35 | const path = '/api/v1/runners/jobs' | ||
36 | |||
37 | return this.getRequestBody<ResultList<RunnerJobAdmin>>({ | ||
38 | ...options, | ||
39 | |||
40 | path, | ||
41 | query: pick(options, [ 'start', 'count', 'sort', 'search', 'stateOneOf' ]), | ||
42 | implicitToken: true, | ||
43 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
44 | }) | ||
45 | } | ||
46 | |||
47 | cancelByAdmin (options: OverrideCommandOptions & { jobUUID: string }) { | ||
48 | const path = '/api/v1/runners/jobs/' + options.jobUUID + '/cancel' | ||
49 | |||
50 | return this.postBodyRequest({ | ||
51 | ...options, | ||
52 | |||
53 | path, | ||
54 | implicitToken: true, | ||
55 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
56 | }) | ||
57 | } | ||
58 | |||
59 | deleteByAdmin (options: OverrideCommandOptions & { jobUUID: string }) { | ||
60 | const path = '/api/v1/runners/jobs/' + options.jobUUID | ||
61 | |||
62 | return this.deleteRequest({ | ||
63 | ...options, | ||
64 | |||
65 | path, | ||
66 | implicitToken: true, | ||
67 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
68 | }) | ||
69 | } | ||
70 | |||
71 | // --------------------------------------------------------------------------- | ||
72 | |||
73 | request (options: OverrideCommandOptions & RequestRunnerJobBody) { | ||
74 | const path = '/api/v1/runners/jobs/request' | ||
75 | |||
76 | return unwrapBody<RequestRunnerJobResult>(this.postBodyRequest({ | ||
77 | ...options, | ||
78 | |||
79 | path, | ||
80 | fields: pick(options, [ 'runnerToken' ]), | ||
81 | implicitToken: false, | ||
82 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
83 | })) | ||
84 | } | ||
85 | |||
86 | async requestVOD (options: OverrideCommandOptions & RequestRunnerJobBody) { | ||
87 | const vodTypes = new Set<RunnerJobType>([ 'vod-audio-merge-transcoding', 'vod-hls-transcoding', 'vod-web-video-transcoding' ]) | ||
88 | |||
89 | const { availableJobs } = await this.request(options) | ||
90 | |||
91 | return { | ||
92 | availableJobs: availableJobs.filter(j => vodTypes.has(j.type)) | ||
93 | } as RequestRunnerJobResult<RunnerJobVODPayload> | ||
94 | } | ||
95 | |||
96 | async requestLive (options: OverrideCommandOptions & RequestRunnerJobBody) { | ||
97 | const vodTypes = new Set<RunnerJobType>([ 'live-rtmp-hls-transcoding' ]) | ||
98 | |||
99 | const { availableJobs } = await this.request(options) | ||
100 | |||
101 | return { | ||
102 | availableJobs: availableJobs.filter(j => vodTypes.has(j.type)) | ||
103 | } as RequestRunnerJobResult<RunnerJobLiveRTMPHLSTranscodingPayload> | ||
104 | } | ||
105 | |||
106 | // --------------------------------------------------------------------------- | ||
107 | |||
108 | accept <T extends RunnerJobPayload = RunnerJobPayload> (options: OverrideCommandOptions & AcceptRunnerJobBody & { jobUUID: string }) { | ||
109 | const path = '/api/v1/runners/jobs/' + options.jobUUID + '/accept' | ||
110 | |||
111 | return unwrapBody<AcceptRunnerJobResult<T>>(this.postBodyRequest({ | ||
112 | ...options, | ||
113 | |||
114 | path, | ||
115 | fields: pick(options, [ 'runnerToken' ]), | ||
116 | implicitToken: false, | ||
117 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
118 | })) | ||
119 | } | ||
120 | |||
121 | abort (options: OverrideCommandOptions & AbortRunnerJobBody & { jobUUID: string }) { | ||
122 | const path = '/api/v1/runners/jobs/' + options.jobUUID + '/abort' | ||
123 | |||
124 | return this.postBodyRequest({ | ||
125 | ...options, | ||
126 | |||
127 | path, | ||
128 | fields: pick(options, [ 'reason', 'jobToken', 'runnerToken' ]), | ||
129 | implicitToken: false, | ||
130 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
131 | }) | ||
132 | } | ||
133 | |||
134 | update (options: OverrideCommandOptions & RunnerJobUpdateBody & { jobUUID: string }) { | ||
135 | const path = '/api/v1/runners/jobs/' + options.jobUUID + '/update' | ||
136 | |||
137 | const { payload } = options | ||
138 | const attaches: { [id: string]: any } = {} | ||
139 | let payloadWithoutFiles = payload | ||
140 | |||
141 | if (isLiveRTMPHLSTranscodingUpdatePayload(payload)) { | ||
142 | if (payload.masterPlaylistFile) { | ||
143 | attaches[`payload[masterPlaylistFile]`] = payload.masterPlaylistFile | ||
144 | } | ||
145 | |||
146 | attaches[`payload[resolutionPlaylistFile]`] = payload.resolutionPlaylistFile | ||
147 | attaches[`payload[videoChunkFile]`] = payload.videoChunkFile | ||
148 | |||
149 | payloadWithoutFiles = omit(payloadWithoutFiles, [ 'masterPlaylistFile', 'resolutionPlaylistFile', 'videoChunkFile' ]) | ||
150 | } | ||
151 | |||
152 | return this.postUploadRequest({ | ||
153 | ...options, | ||
154 | |||
155 | path, | ||
156 | fields: { | ||
157 | ...pick(options, [ 'progress', 'jobToken', 'runnerToken' ]), | ||
158 | |||
159 | payload: payloadWithoutFiles | ||
160 | }, | ||
161 | attaches, | ||
162 | implicitToken: false, | ||
163 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
164 | }) | ||
165 | } | ||
166 | |||
167 | error (options: OverrideCommandOptions & ErrorRunnerJobBody & { jobUUID: string }) { | ||
168 | const path = '/api/v1/runners/jobs/' + options.jobUUID + '/error' | ||
169 | |||
170 | return this.postBodyRequest({ | ||
171 | ...options, | ||
172 | |||
173 | path, | ||
174 | fields: pick(options, [ 'message', 'jobToken', 'runnerToken' ]), | ||
175 | implicitToken: false, | ||
176 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
177 | }) | ||
178 | } | ||
179 | |||
180 | success (options: OverrideCommandOptions & RunnerJobSuccessBody & { jobUUID: string }) { | ||
181 | const { payload } = options | ||
182 | |||
183 | const path = '/api/v1/runners/jobs/' + options.jobUUID + '/success' | ||
184 | const attaches: { [id: string]: any } = {} | ||
185 | let payloadWithoutFiles = payload | ||
186 | |||
187 | if ((isWebVideoOrAudioMergeTranscodingPayloadSuccess(payload) || isHLSTranscodingPayloadSuccess(payload)) && payload.videoFile) { | ||
188 | attaches[`payload[videoFile]`] = payload.videoFile | ||
189 | |||
190 | payloadWithoutFiles = omit(payloadWithoutFiles as VODWebVideoTranscodingSuccess, [ 'videoFile' ]) | ||
191 | } | ||
192 | |||
193 | if (isHLSTranscodingPayloadSuccess(payload) && payload.resolutionPlaylistFile) { | ||
194 | attaches[`payload[resolutionPlaylistFile]`] = payload.resolutionPlaylistFile | ||
195 | |||
196 | payloadWithoutFiles = omit(payloadWithoutFiles as VODHLSTranscodingSuccess, [ 'resolutionPlaylistFile' ]) | ||
197 | } | ||
198 | |||
199 | return this.postUploadRequest({ | ||
200 | ...options, | ||
201 | |||
202 | path, | ||
203 | attaches, | ||
204 | fields: { | ||
205 | ...pick(options, [ 'jobToken', 'runnerToken' ]), | ||
206 | |||
207 | payload: payloadWithoutFiles | ||
208 | }, | ||
209 | implicitToken: false, | ||
210 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
211 | }) | ||
212 | } | ||
213 | |||
214 | getJobFile (options: OverrideCommandOptions & { url: string, jobToken: string, runnerToken: string }) { | ||
215 | const { host, protocol, pathname } = new URL(options.url) | ||
216 | |||
217 | return this.postBodyRequest({ | ||
218 | url: `${protocol}//${host}`, | ||
219 | path: pathname, | ||
220 | |||
221 | fields: pick(options, [ 'jobToken', 'runnerToken' ]), | ||
222 | implicitToken: false, | ||
223 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
224 | }) | ||
225 | } | ||
226 | |||
227 | // --------------------------------------------------------------------------- | ||
228 | |||
229 | async autoAccept (options: OverrideCommandOptions & RequestRunnerJobBody & { type?: RunnerJobType }) { | ||
230 | const { availableJobs } = await this.request(options) | ||
231 | |||
232 | const job = options.type | ||
233 | ? availableJobs.find(j => j.type === options.type) | ||
234 | : availableJobs[0] | ||
235 | |||
236 | return this.accept({ ...options, jobUUID: job.uuid }) | ||
237 | } | ||
238 | |||
239 | async autoProcessWebVideoJob (runnerToken: string, jobUUIDToProcess?: string) { | ||
240 | let jobUUID = jobUUIDToProcess | ||
241 | |||
242 | if (!jobUUID) { | ||
243 | const { availableJobs } = await this.request({ runnerToken }) | ||
244 | jobUUID = availableJobs[0].uuid | ||
245 | } | ||
246 | |||
247 | const { job } = await this.accept({ runnerToken, jobUUID }) | ||
248 | const jobToken = job.jobToken | ||
249 | |||
250 | const payload: RunnerJobSuccessPayload = { videoFile: 'video_short.mp4' } | ||
251 | await this.success({ runnerToken, jobUUID, jobToken, payload }) | ||
252 | |||
253 | await waitJobs([ this.server ]) | ||
254 | |||
255 | return job | ||
256 | } | ||
257 | |||
258 | async cancelAllJobs (options: { state?: RunnerJobStateType } = {}) { | ||
259 | const { state } = options | ||
260 | |||
261 | const { data } = await this.list({ count: 100 }) | ||
262 | |||
263 | const allowedStates = new Set<RunnerJobStateType>([ | ||
264 | RunnerJobState.PENDING, | ||
265 | RunnerJobState.PROCESSING, | ||
266 | RunnerJobState.WAITING_FOR_PARENT_JOB | ||
267 | ]) | ||
268 | |||
269 | for (const job of data) { | ||
270 | if (state && job.state.id !== state) continue | ||
271 | else if (allowedStates.has(job.state.id) !== true) continue | ||
272 | |||
273 | await this.cancelByAdmin({ jobUUID: job.uuid }) | ||
274 | } | ||
275 | } | ||
276 | |||
277 | async getJob (options: OverrideCommandOptions & { uuid: string }) { | ||
278 | const { data } = await this.list({ ...options, count: 100, sort: '-updatedAt' }) | ||
279 | |||
280 | return data.find(j => j.uuid === options.uuid) | ||
281 | } | ||
282 | |||
283 | async requestLiveJob (runnerToken: string) { | ||
284 | let availableJobs: RequestRunnerJobResult<RunnerJobLiveRTMPHLSTranscodingPayload>['availableJobs'] = [] | ||
285 | |||
286 | while (availableJobs.length === 0) { | ||
287 | const result = await this.requestLive({ runnerToken }) | ||
288 | availableJobs = result.availableJobs | ||
289 | |||
290 | if (availableJobs.length === 1) break | ||
291 | |||
292 | await wait(150) | ||
293 | } | ||
294 | |||
295 | return availableJobs[0] | ||
296 | } | ||
297 | } | ||
diff --git a/packages/server-commands/src/runners/runner-registration-tokens-command.ts b/packages/server-commands/src/runners/runner-registration-tokens-command.ts new file mode 100644 index 000000000..86b6e5f93 --- /dev/null +++ b/packages/server-commands/src/runners/runner-registration-tokens-command.ts | |||
@@ -0,0 +1,55 @@ | |||
1 | import { pick } from '@peertube/peertube-core-utils' | ||
2 | import { HttpStatusCode, ResultList, RunnerRegistrationToken } from '@peertube/peertube-models' | ||
3 | import { AbstractCommand, OverrideCommandOptions } from '../shared/index.js' | ||
4 | |||
5 | export class RunnerRegistrationTokensCommand extends AbstractCommand { | ||
6 | |||
7 | list (options: OverrideCommandOptions & { | ||
8 | start?: number | ||
9 | count?: number | ||
10 | sort?: string | ||
11 | } = {}) { | ||
12 | const path = '/api/v1/runners/registration-tokens' | ||
13 | |||
14 | return this.getRequestBody<ResultList<RunnerRegistrationToken>>({ | ||
15 | ...options, | ||
16 | |||
17 | path, | ||
18 | query: pick(options, [ 'start', 'count', 'sort' ]), | ||
19 | implicitToken: true, | ||
20 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
21 | }) | ||
22 | } | ||
23 | |||
24 | generate (options: OverrideCommandOptions = {}) { | ||
25 | const path = '/api/v1/runners/registration-tokens/generate' | ||
26 | |||
27 | return this.postBodyRequest({ | ||
28 | ...options, | ||
29 | |||
30 | path, | ||
31 | implicitToken: true, | ||
32 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
33 | }) | ||
34 | } | ||
35 | |||
36 | delete (options: OverrideCommandOptions & { | ||
37 | id: number | ||
38 | }) { | ||
39 | const path = '/api/v1/runners/registration-tokens/' + options.id | ||
40 | |||
41 | return this.deleteRequest({ | ||
42 | ...options, | ||
43 | |||
44 | path, | ||
45 | implicitToken: true, | ||
46 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
47 | }) | ||
48 | } | ||
49 | |||
50 | async getFirstRegistrationToken (options: OverrideCommandOptions = {}) { | ||
51 | const { data } = await this.list(options) | ||
52 | |||
53 | return data[0].registrationToken | ||
54 | } | ||
55 | } | ||
diff --git a/packages/server-commands/src/runners/runners-command.ts b/packages/server-commands/src/runners/runners-command.ts new file mode 100644 index 000000000..376a1dff9 --- /dev/null +++ b/packages/server-commands/src/runners/runners-command.ts | |||
@@ -0,0 +1,85 @@ | |||
1 | import { pick } from '@peertube/peertube-core-utils' | ||
2 | import { | ||
3 | HttpStatusCode, | ||
4 | RegisterRunnerBody, | ||
5 | RegisterRunnerResult, | ||
6 | ResultList, | ||
7 | Runner, | ||
8 | UnregisterRunnerBody | ||
9 | } from '@peertube/peertube-models' | ||
10 | import { buildUUID } from '@peertube/peertube-node-utils' | ||
11 | import { unwrapBody } from '../requests/index.js' | ||
12 | import { AbstractCommand, OverrideCommandOptions } from '../shared/index.js' | ||
13 | |||
14 | export class RunnersCommand extends AbstractCommand { | ||
15 | |||
16 | list (options: OverrideCommandOptions & { | ||
17 | start?: number | ||
18 | count?: number | ||
19 | sort?: string | ||
20 | } = {}) { | ||
21 | const path = '/api/v1/runners' | ||
22 | |||
23 | return this.getRequestBody<ResultList<Runner>>({ | ||
24 | ...options, | ||
25 | |||
26 | path, | ||
27 | query: pick(options, [ 'start', 'count', 'sort' ]), | ||
28 | implicitToken: true, | ||
29 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
30 | }) | ||
31 | } | ||
32 | |||
33 | register (options: OverrideCommandOptions & RegisterRunnerBody) { | ||
34 | const path = '/api/v1/runners/register' | ||
35 | |||
36 | return unwrapBody<RegisterRunnerResult>(this.postBodyRequest({ | ||
37 | ...options, | ||
38 | |||
39 | path, | ||
40 | fields: pick(options, [ 'name', 'registrationToken', 'description' ]), | ||
41 | implicitToken: true, | ||
42 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
43 | })) | ||
44 | } | ||
45 | |||
46 | unregister (options: OverrideCommandOptions & UnregisterRunnerBody) { | ||
47 | const path = '/api/v1/runners/unregister' | ||
48 | |||
49 | return this.postBodyRequest({ | ||
50 | ...options, | ||
51 | |||
52 | path, | ||
53 | fields: pick(options, [ 'runnerToken' ]), | ||
54 | implicitToken: false, | ||
55 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
56 | }) | ||
57 | } | ||
58 | |||
59 | delete (options: OverrideCommandOptions & { | ||
60 | id: number | ||
61 | }) { | ||
62 | const path = '/api/v1/runners/' + options.id | ||
63 | |||
64 | return this.deleteRequest({ | ||
65 | ...options, | ||
66 | |||
67 | path, | ||
68 | implicitToken: true, | ||
69 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
70 | }) | ||
71 | } | ||
72 | |||
73 | // --------------------------------------------------------------------------- | ||
74 | |||
75 | async autoRegisterRunner () { | ||
76 | const { data } = await this.server.runnerRegistrationTokens.list({ sort: 'createdAt' }) | ||
77 | |||
78 | const { runnerToken } = await this.register({ | ||
79 | name: 'runner ' + buildUUID(), | ||
80 | registrationToken: data[0].registrationToken | ||
81 | }) | ||
82 | |||
83 | return runnerToken | ||
84 | } | ||
85 | } | ||