import { copy } from 'fs-extra'
import { join } from 'path'
import { root } from '@server/helpers/core-utils'
-import { randomInt } from '../../core-utils/miscs/miscs'
-import { VideoChannel } from '../../models/videos'
+import { randomInt } from '@shared/core-utils'
+import { Video, VideoChannel, VideoCreateResult, VideoDetails } from '../../models/videos'
import { BulkCommand } from '../bulk'
import { CLICommand } from '../cli'
import { CustomPagesCommand } from '../custom-pages'
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
- execArgv?: string[]
+ nodeArgs?: string[]
+ peertubeArgs?: string[]
+ env?: { [ id: string ]: string }
}
export class PeerTubeServer {
port?: number
rtmpPort?: number
+ rtmpsPort?: number
parallel?: boolean
internalServerNumber: number
channel?: VideoChannel
- video?: {
- id: number
- uuid: string
- shortUUID: string
- name?: string
- url?: string
-
- account?: {
- name: string
- }
-
- embedPath?: string
- }
+ video?: Video
+ videoCreated?: VideoCreateResult
+ videoDetails?: VideoDetails
videos?: { id: number, uuid: string }[]
}
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}`
this.port = parseInt(parsed.port)
}
- async flushAndRun (configOverride?: Object, args = [], options: RunServerOptions = {}) {
+ async flushAndRun (configOverride?: Object, options: RunServerOptions = {}) {
await ServersCommand.flushTests(this.internalServerNumber)
- return this.run(configOverride, args, options)
+ return this.run(configOverride, options)
}
- async run (configOverrideArg?: any, args = [], options: RunServerOptions = {}) {
+ async run (configOverrideArg?: any, options: RunServerOptions = {}) {
// These actions are async so we need to be sure that they have both been done
const serverRunString = {
'HTTP server listening': false
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,
detached: true,
- execArgv: options.execArgv || []
+ execArgv: options.nodeArgs || []
}
- return new Promise<void>(res => {
+ return new Promise<void>((res, rej) => {
const self = this
- this.app = fork(join(root(), 'dist', 'server.js'), args, forkOptions)
+ this.app = fork(join(root(), 'dist', 'server.js'), options.peertubeArgs || [], forkOptions)
+
+ const onPeerTubeExit = () => rej(new Error('Process exited'))
+ 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
if (options.hideLogs === false) {
console.log(data.toString())
} else {
+ process.removeListener('exit', onParentExit)
self.app.stdout.removeListener('data', onStdout)
+ self.app.removeListener('exit', onPeerTubeExit)
}
- process.on('exit', () => {
- try {
- process.kill(self.server.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)
}
}