PeerTube/server/tests/shared/peertube-runner-process.ts

99 lines
2.2 KiB
TypeScript
Raw Normal View History

2023-04-21 15:00:01 +02: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 13:33:27 +02:00
constructor (private readonly server: PeerTubeServer) {
}
2023-04-21 15:00:01 +02:00
runServer (options: {
hideLogs?: boolean // default true
} = {}) {
const { hideLogs = true } = options
return new Promise<void>((res, rej) => {
2023-05-19 13:33:27 +02:00
const args = [ 'server', '--verbose', ...this.buildIdArg() ]
2023-04-21 15:00:01 +02: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 13:33:27 +02:00
const { registrationToken, runnerName, runnerDescription } = options
2023-04-21 15:00:01 +02:00
const args = [
'register',
2023-05-19 13:33:27 +02:00
'--url', this.server.url,
2023-04-21 15:00:01 +02:00
'--registration-token', registrationToken,
'--runner-name', runnerName,
2023-05-19 13:33:27 +02:00
...this.buildIdArg()
2023-04-21 15:00:01 +02:00
]
if (runnerDescription) {
args.push('--runner-description')
args.push(runnerDescription)
}
return execa.node(this.getRunnerPath(), args)
}
unregisterPeerTubeInstance (options: {
runnerName: string
}) {
const { runnerName } = options
const args = [ 'unregister', '--url', this.server.url, '--runner-name', runnerName, ...this.buildIdArg() ]
2023-04-21 15:00:01 +02:00
return execa.node(this.getRunnerPath(), args)
}
async listRegisteredPeerTubeInstances () {
2023-05-19 13:33:27 +02:00
const args = [ 'list-registered', ...this.buildIdArg() ]
2023-04-21 15:00:01 +02: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 14:35:03 +02:00
getId () {
return 'test-' + this.server.internalServerNumber
}
2023-04-21 15:00:01 +02:00
private getRunnerPath () {
return join(root(), 'packages', 'peertube-runner', 'dist', 'peertube-runner.js')
}
2023-05-19 13:33:27 +02:00
private buildIdArg () {
2023-05-19 14:35:03 +02:00
return [ '--id', this.getId() ]
2023-05-19 13:33:27 +02:00
}
2023-04-21 15:00:01 +02:00
}