aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests/shared/peertube-runner-process.ts
blob: 84e2dc6dff14479c8bd54ff97f718ae21d2acaa9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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

  runServer (options: {
    hideLogs?: boolean // default true
  } = {}) {
    const { hideLogs = true } = options

    return new Promise<void>((res, rej) => {
      const args = [ 'server', '--verbose', '--id', 'test' ]

      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: {
    server: PeerTubeServer
    registrationToken: string
    runnerName: string
    runnerDescription?: string
  }) {
    const { server, registrationToken, runnerName, runnerDescription } = options

    const args = [
      'register',
      '--url', server.url,
      '--registration-token', registrationToken,
      '--runner-name', runnerName,
      '--id', 'test'
    ]

    if (runnerDescription) {
      args.push('--runner-description')
      args.push(runnerDescription)
    }

    return execa.node(this.getRunnerPath(), args)
  }

  unregisterPeerTubeInstance (options: {
    server: PeerTubeServer
  }) {
    const { server } = options

    const args = [ 'unregister', '--url', server.url, '--id', 'test' ]
    return execa.node(this.getRunnerPath(), args)
  }

  async listRegisteredPeerTubeInstances () {
    const args = [ 'list-registered', '--id', 'test' ]
    const { stdout } = await execa.node(this.getRunnerPath(), args)

    return stdout
  }

  kill () {
    if (!this.app) return

    process.kill(this.app.pid)

    this.app = null
  }

  private getRunnerPath () {
    return join(root(), 'packages', 'peertube-runner', 'dist', 'peertube-runner.js')
  }
}