]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/shared/peertube-runner-process.ts
b3c8dfe0e8698a25e0aeb288d1e3441186ecf88c
[github/Chocobozzz/PeerTube.git] / server / tests / shared / peertube-runner-process.ts
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 () {
64 const args = [ 'unregister', '--url', this.server.url, ...this.buildIdArg() ]
65 return execa.node(this.getRunnerPath(), args)
66 }
67
68 async listRegisteredPeerTubeInstances () {
69 const args = [ 'list-registered', ...this.buildIdArg() ]
70 const { stdout } = await execa.node(this.getRunnerPath(), args)
71
72 return stdout
73 }
74
75 kill () {
76 if (!this.app) return
77
78 process.kill(this.app.pid)
79
80 this.app = null
81 }
82
83 getId () {
84 return 'test-' + this.server.internalServerNumber
85 }
86
87 private getRunnerPath () {
88 return join(root(), 'packages', 'peertube-runner', 'dist', 'peertube-runner.js')
89 }
90
91 private buildIdArg () {
92 return [ '--id', this.getId() ]
93 }
94 }