]>
Commit | Line | Data |
---|---|---|
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 | constructor (private readonly server: PeerTubeServer) { | |
11 | ||
12 | } | |
13 | ||
14 | runServer (options: { | |
15 | hideLogs?: boolean // default true | |
16 | } = {}) { | |
17 | const { hideLogs = true } = options | |
18 | ||
19 | return new Promise<void>((res, rej) => { | |
20 | const args = [ 'server', '--verbose', ...this.buildIdArg() ] | |
21 | ||
22 | const forkOptions = { | |
23 | detached: false, | |
24 | silent: true | |
25 | } | |
26 | this.app = fork(this.getRunnerPath(), args, forkOptions) | |
27 | ||
28 | this.app.stdout.on('data', data => { | |
29 | const str = data.toString() as string | |
30 | ||
31 | if (!hideLogs) { | |
32 | console.log(str) | |
33 | } | |
34 | }) | |
35 | ||
36 | res() | |
37 | }) | |
38 | } | |
39 | ||
40 | registerPeerTubeInstance (options: { | |
41 | registrationToken: string | |
42 | runnerName: string | |
43 | runnerDescription?: string | |
44 | }) { | |
45 | const { registrationToken, runnerName, runnerDescription } = options | |
46 | ||
47 | const args = [ | |
48 | 'register', | |
49 | '--url', this.server.url, | |
50 | '--registration-token', registrationToken, | |
51 | '--runner-name', runnerName, | |
52 | ...this.buildIdArg() | |
53 | ] | |
54 | ||
55 | if (runnerDescription) { | |
56 | args.push('--runner-description') | |
57 | args.push(runnerDescription) | |
58 | } | |
59 | ||
60 | return execa.node(this.getRunnerPath(), args) | |
61 | } | |
62 | ||
63 | unregisterPeerTubeInstance (options: { | |
64 | runnerName: string | |
65 | }) { | |
66 | const { runnerName } = options | |
67 | ||
68 | const args = [ 'unregister', '--url', this.server.url, '--runner-name', runnerName, ...this.buildIdArg() ] | |
69 | return execa.node(this.getRunnerPath(), args) | |
70 | } | |
71 | ||
72 | async listRegisteredPeerTubeInstances () { | |
73 | const args = [ 'list-registered', ...this.buildIdArg() ] | |
74 | const { stdout } = await execa.node(this.getRunnerPath(), args) | |
75 | ||
76 | return stdout | |
77 | } | |
78 | ||
79 | kill () { | |
80 | if (!this.app) return | |
81 | ||
82 | process.kill(this.app.pid) | |
83 | ||
84 | this.app = null | |
85 | } | |
86 | ||
87 | getId () { | |
88 | return 'test-' + this.server.internalServerNumber | |
89 | } | |
90 | ||
91 | private getRunnerPath () { | |
92 | return join(root(), 'packages', 'peertube-runner', 'dist', 'peertube-runner.js') | |
93 | } | |
94 | ||
95 | private buildIdArg () { | |
96 | return [ '--id', this.getId() ] | |
97 | } | |
98 | } |