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'
7 export class PeerTubeRunnerProcess {
8 private app?: ChildProcess
10 constructor (private readonly server: PeerTubeServer) {
15 hideLogs?: boolean // default true
17 const { hideLogs = true } = options
19 return new Promise<void>((res, rej) => {
20 const args = [ 'server', '--verbose', ...this.buildIdArg() ]
26 this.app = fork(this.getRunnerPath(), args, forkOptions)
28 this.app.stdout.on('data', data => {
29 const str = data.toString() as string
40 registerPeerTubeInstance (options: {
41 registrationToken: string
43 runnerDescription?: string
45 const { registrationToken, runnerName, runnerDescription } = options
49 '--url', this.server.url,
50 '--registration-token', registrationToken,
51 '--runner-name', runnerName,
55 if (runnerDescription) {
56 args.push('--runner-description')
57 args.push(runnerDescription)
60 return execa.node(this.getRunnerPath(), args)
63 unregisterPeerTubeInstance (options: {
66 const { runnerName } = options
68 const args = [ 'unregister', '--url', this.server.url, '--runner-name', runnerName, ...this.buildIdArg() ]
69 return execa.node(this.getRunnerPath(), args)
72 async listRegisteredPeerTubeInstances () {
73 const args = [ 'list-registered', ...this.buildIdArg() ]
74 const { stdout } = await execa.node(this.getRunnerPath(), args)
82 process.kill(this.app.pid)
88 return 'test-' + this.server.internalServerNumber
91 private getRunnerPath () {
92 return join(root(), 'packages', 'peertube-runner', 'dist', 'peertube-runner.js')
95 private buildIdArg () {
96 return [ '--id', this.getId() ]