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
11 hideLogs?: boolean // default true
13 const { hideLogs = true } = options
15 return new Promise<void>((res, rej) => {
16 const args = [ 'server', '--verbose', '--id', 'test' ]
22 this.app = fork(this.getRunnerPath(), args, forkOptions)
24 this.app.stdout.on('data', data => {
25 const str = data.toString() as string
36 registerPeerTubeInstance (options: {
37 server: PeerTubeServer
38 registrationToken: string
40 runnerDescription?: string
42 const { server, registrationToken, runnerName, runnerDescription } = options
47 '--registration-token', registrationToken,
48 '--runner-name', runnerName,
52 if (runnerDescription) {
53 args.push('--runner-description')
54 args.push(runnerDescription)
57 return execa.node(this.getRunnerPath(), args)
60 unregisterPeerTubeInstance (options: {
61 server: PeerTubeServer
63 const { server } = options
65 const args = [ 'unregister', '--url', server.url, '--id', 'test' ]
66 return execa.node(this.getRunnerPath(), args)
69 async listRegisteredPeerTubeInstances () {
70 const args = [ 'list-registered', '--id', 'test' ]
71 const { stdout } = await execa.node(this.getRunnerPath(), args)
79 process.kill(this.app.pid)
84 private getRunnerPath () {
85 return join(root(), 'packages', 'peertube-runner', 'dist', 'peertube-runner.js')