import { RedundancyCommand } from './redundancy-command'
import { ServersCommand } from './servers-command'
import { StatsCommand } from './stats-command'
+import { ObjectStorageCommand } from './object-storage-command'
export type RunServerOptions = {
hideLogs?: boolean
nodeArgs?: string[]
peertubeArgs?: string[]
+ env?: { [ id: string ]: string }
}
export class PeerTubeServer {
port?: number
rtmpPort?: number
+ rtmpsPort?: number
parallel?: boolean
internalServerNumber: number
servers?: ServersCommand
login?: LoginCommand
users?: UsersCommand
+ objectStorage?: ObjectStorageCommand
videos?: VideosCommand
constructor (options: { serverNumber: number } | { url: string }) {
this.internalServerNumber = this.parallel ? this.randomServer() : this.serverNumber
this.rtmpPort = this.parallel ? this.randomRTMP() : 1936
+ this.rtmpsPort = this.parallel ? this.randomRTMP() : 1937
this.port = 9000 + this.internalServerNumber
this.url = `http://localhost:${this.port}`
env['NODE_APP_INSTANCE'] = this.internalServerNumber.toString()
env['NODE_CONFIG'] = JSON.stringify(configOverride)
+ if (options.env) {
+ Object.assign(env, options.env)
+ }
+
const forkOptions = {
silent: true,
env,
execArgv: options.nodeArgs || []
}
- return new Promise<void>(res => {
+ return new Promise<void>((res, rej) => {
const self = this
+ let aggregatedLogs = ''
this.app = fork(join(root(), 'dist', 'server.js'), options.peertubeArgs || [], forkOptions)
+
+ const onPeerTubeExit = () => rej(new Error('Process exited:\n' + aggregatedLogs))
+ const onParentExit = () => {
+ if (!this.app || !this.app.pid) return
+
+ try {
+ process.kill(self.app.pid)
+ } catch { /* empty */ }
+ }
+
+ this.app.on('exit', onPeerTubeExit)
+ process.on('exit', onParentExit)
+
this.app.stdout.on('data', function onStdout (data) {
let dontContinue = false
+ const log: string = data.toString()
+ aggregatedLogs += log
+
// Capture things if we want to
for (const key of Object.keys(regexps)) {
const regexp = regexps[key]
- const matches = data.toString().match(regexp)
+ const matches = log.match(regexp)
if (matches !== null) {
if (key === 'client_id') self.store.client.id = matches[1]
else if (key === 'client_secret') self.store.client.secret = matches[1]
// Check if all required sentences are here
for (const key of Object.keys(serverRunString)) {
- if (data.toString().indexOf(key) !== -1) serverRunString[key] = true
+ if (log.includes(key)) serverRunString[key] = true
if (serverRunString[key] === false) dontContinue = true
}
if (dontContinue === true) return
if (options.hideLogs === false) {
- console.log(data.toString())
+ console.log(log)
} else {
+ process.removeListener('exit', onParentExit)
self.app.stdout.removeListener('data', onStdout)
+ self.app.removeListener('exit', onPeerTubeExit)
}
- process.on('exit', () => {
- try {
- process.kill(self.app.pid)
- } catch { /* empty */ }
- })
-
res()
})
})
},
storage: {
tmp: `test${this.internalServerNumber}/tmp/`,
+ bin: `test${this.internalServerNumber}/bin/`,
avatars: `test${this.internalServerNumber}/avatars/`,
videos: `test${this.internalServerNumber}/videos/`,
streaming_playlists: `test${this.internalServerNumber}/streaming-playlists/`,
this.login = new LoginCommand(this)
this.users = new UsersCommand(this)
this.videos = new VideosCommand(this)
+ this.objectStorage = new ObjectStorageCommand(this)
}
}