2023-04-21 08:00:01 -05:00
|
|
|
import { ChildProcess, fork } from 'child_process'
|
|
|
|
import execa from 'execa'
|
|
|
|
import { join } from 'path'
|
|
|
|
import { root } from '@shared/core-utils'
|
|
|
|
import { PeerTubeServer } from '@shared/server-commands'
|
|
|
|
|
|
|
|
export class PeerTubeRunnerProcess {
|
|
|
|
private app?: ChildProcess
|
|
|
|
|
2023-05-19 06:33:27 -05:00
|
|
|
constructor (private readonly server: PeerTubeServer) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-04-21 08:00:01 -05:00
|
|
|
runServer (options: {
|
|
|
|
hideLogs?: boolean // default true
|
|
|
|
} = {}) {
|
|
|
|
const { hideLogs = true } = options
|
|
|
|
|
|
|
|
return new Promise<void>((res, rej) => {
|
2023-05-19 06:33:27 -05:00
|
|
|
const args = [ 'server', '--verbose', ...this.buildIdArg() ]
|
2023-04-21 08:00:01 -05:00
|
|
|
|
|
|
|
const forkOptions = {
|
|
|
|
detached: false,
|
|
|
|
silent: true
|
|
|
|
}
|
|
|
|
this.app = fork(this.getRunnerPath(), args, forkOptions)
|
|
|
|
|
|
|
|
this.app.stdout.on('data', data => {
|
|
|
|
const str = data.toString() as string
|
|
|
|
|
|
|
|
if (!hideLogs) {
|
|
|
|
console.log(str)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
res()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
registerPeerTubeInstance (options: {
|
|
|
|
registrationToken: string
|
|
|
|
runnerName: string
|
|
|
|
runnerDescription?: string
|
|
|
|
}) {
|
2023-05-19 06:33:27 -05:00
|
|
|
const { registrationToken, runnerName, runnerDescription } = options
|
2023-04-21 08:00:01 -05:00
|
|
|
|
|
|
|
const args = [
|
|
|
|
'register',
|
2023-05-19 06:33:27 -05:00
|
|
|
'--url', this.server.url,
|
2023-04-21 08:00:01 -05:00
|
|
|
'--registration-token', registrationToken,
|
|
|
|
'--runner-name', runnerName,
|
2023-05-19 06:33:27 -05:00
|
|
|
...this.buildIdArg()
|
2023-04-21 08:00:01 -05:00
|
|
|
]
|
|
|
|
|
|
|
|
if (runnerDescription) {
|
|
|
|
args.push('--runner-description')
|
|
|
|
args.push(runnerDescription)
|
|
|
|
}
|
|
|
|
|
|
|
|
return execa.node(this.getRunnerPath(), args)
|
|
|
|
}
|
|
|
|
|
2023-05-26 02:03:50 -05:00
|
|
|
unregisterPeerTubeInstance (options: {
|
|
|
|
runnerName: string
|
|
|
|
}) {
|
|
|
|
const { runnerName } = options
|
|
|
|
|
|
|
|
const args = [ 'unregister', '--url', this.server.url, '--runner-name', runnerName, ...this.buildIdArg() ]
|
2023-04-21 08:00:01 -05:00
|
|
|
return execa.node(this.getRunnerPath(), args)
|
|
|
|
}
|
|
|
|
|
|
|
|
async listRegisteredPeerTubeInstances () {
|
2023-05-19 06:33:27 -05:00
|
|
|
const args = [ 'list-registered', ...this.buildIdArg() ]
|
2023-04-21 08:00:01 -05:00
|
|
|
const { stdout } = await execa.node(this.getRunnerPath(), args)
|
|
|
|
|
|
|
|
return stdout
|
|
|
|
}
|
|
|
|
|
|
|
|
kill () {
|
|
|
|
if (!this.app) return
|
|
|
|
|
|
|
|
process.kill(this.app.pid)
|
|
|
|
|
|
|
|
this.app = null
|
|
|
|
}
|
|
|
|
|
2023-05-19 07:35:03 -05:00
|
|
|
getId () {
|
|
|
|
return 'test-' + this.server.internalServerNumber
|
|
|
|
}
|
|
|
|
|
2023-04-21 08:00:01 -05:00
|
|
|
private getRunnerPath () {
|
|
|
|
return join(root(), 'packages', 'peertube-runner', 'dist', 'peertube-runner.js')
|
|
|
|
}
|
2023-05-19 06:33:27 -05:00
|
|
|
|
|
|
|
private buildIdArg () {
|
2023-05-19 07:35:03 -05:00
|
|
|
return [ '--id', this.getId() ]
|
2023-05-19 06:33:27 -05:00
|
|
|
}
|
2023-04-21 08:00:01 -05:00
|
|
|
}
|