]>
Commit | Line | Data |
---|---|---|
d102de1b C |
1 | import { ChildProcess, fork } from 'child_process' |
2 | import execa from 'execa' | |
3 | import { join } from 'path' | |
4 | import { root } from '@shared/core-utils' | |
5 | import { PeerTubeServer } from '@shared/server-commands' | |
6 | ||
7 | export class PeerTubeRunnerProcess { | |
8 | private app?: ChildProcess | |
9 | ||
10 | runServer (options: { | |
11 | hideLogs?: boolean // default true | |
12 | } = {}) { | |
13 | const { hideLogs = true } = options | |
14 | ||
15 | return new Promise<void>((res, rej) => { | |
16 | const args = [ 'server', '--verbose', '--id', 'test' ] | |
17 | ||
18 | const forkOptions = { | |
19 | detached: false, | |
20 | silent: true | |
21 | } | |
22 | this.app = fork(this.getRunnerPath(), args, forkOptions) | |
23 | ||
24 | this.app.stdout.on('data', data => { | |
25 | const str = data.toString() as string | |
26 | ||
27 | if (!hideLogs) { | |
28 | console.log(str) | |
29 | } | |
30 | }) | |
31 | ||
32 | res() | |
33 | }) | |
34 | } | |
35 | ||
36 | registerPeerTubeInstance (options: { | |
37 | server: PeerTubeServer | |
38 | registrationToken: string | |
39 | runnerName: string | |
40 | runnerDescription?: string | |
41 | }) { | |
42 | const { server, registrationToken, runnerName, runnerDescription } = options | |
43 | ||
44 | const args = [ | |
45 | 'register', | |
46 | '--url', server.url, | |
47 | '--registration-token', registrationToken, | |
48 | '--runner-name', runnerName, | |
49 | '--id', 'test' | |
50 | ] | |
51 | ||
52 | if (runnerDescription) { | |
53 | args.push('--runner-description') | |
54 | args.push(runnerDescription) | |
55 | } | |
56 | ||
57 | return execa.node(this.getRunnerPath(), args) | |
58 | } | |
59 | ||
60 | unregisterPeerTubeInstance (options: { | |
61 | server: PeerTubeServer | |
62 | }) { | |
63 | const { server } = options | |
64 | ||
65 | const args = [ 'unregister', '--url', server.url, '--id', 'test' ] | |
66 | return execa.node(this.getRunnerPath(), args) | |
67 | } | |
68 | ||
69 | async listRegisteredPeerTubeInstances () { | |
70 | const args = [ 'list-registered', '--id', 'test' ] | |
71 | const { stdout } = await execa.node(this.getRunnerPath(), args) | |
72 | ||
73 | return stdout | |
74 | } | |
75 | ||
76 | kill () { | |
77 | if (!this.app) return | |
78 | ||
79 | process.kill(this.app.pid) | |
80 | ||
81 | this.app = null | |
82 | } | |
83 | ||
84 | private getRunnerPath () { | |
85 | return join(root(), 'packages', 'peertube-runner', 'dist', 'peertube-runner.js') | |
86 | } | |
87 | } |