2023-04-21 08:05:27 -05:00
|
|
|
import { ensureDir, readdir, remove } from 'fs-extra'
|
|
|
|
import { join } from 'path'
|
|
|
|
import { io, Socket } from 'socket.io-client'
|
2023-05-19 03:16:43 -05:00
|
|
|
import { pick, wait } from '@shared/core-utils'
|
2023-04-21 08:05:27 -05:00
|
|
|
import { PeerTubeProblemDocument, ServerErrorCode } from '@shared/models'
|
|
|
|
import { PeerTubeServer as PeerTubeServerCommand } from '@shared/server-commands'
|
|
|
|
import { ConfigManager } from '../shared'
|
|
|
|
import { IPCServer } from '../shared/ipc'
|
|
|
|
import { logger } from '../shared/logger'
|
|
|
|
import { JobWithToken, processJob } from './process'
|
2023-05-04 08:29:34 -05:00
|
|
|
import { isJobSupported } from './shared'
|
2023-04-21 08:05:27 -05:00
|
|
|
|
|
|
|
type PeerTubeServer = PeerTubeServerCommand & {
|
|
|
|
runnerToken: string
|
|
|
|
runnerName: string
|
|
|
|
runnerDescription?: string
|
|
|
|
}
|
|
|
|
|
|
|
|
export class RunnerServer {
|
|
|
|
private static instance: RunnerServer
|
|
|
|
|
|
|
|
private servers: PeerTubeServer[] = []
|
|
|
|
private processingJobs: { job: JobWithToken, server: PeerTubeServer }[] = []
|
|
|
|
|
|
|
|
private checkingAvailableJobs = false
|
|
|
|
|
2023-05-02 06:51:06 -05:00
|
|
|
private cleaningUp = false
|
|
|
|
|
2023-04-21 08:05:27 -05:00
|
|
|
private readonly sockets = new Map<PeerTubeServer, Socket>()
|
|
|
|
|
|
|
|
private constructor () {}
|
|
|
|
|
|
|
|
async run () {
|
|
|
|
logger.info('Running PeerTube runner in server mode')
|
|
|
|
|
|
|
|
await ConfigManager.Instance.load()
|
|
|
|
|
|
|
|
for (const registered of ConfigManager.Instance.getConfig().registeredInstances) {
|
|
|
|
const serverCommand = new PeerTubeServerCommand({ url: registered.url })
|
|
|
|
|
|
|
|
this.loadServer(Object.assign(serverCommand, registered))
|
|
|
|
|
|
|
|
logger.info(`Loading registered instance ${registered.url}`)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run IPC
|
|
|
|
const ipcServer = new IPCServer()
|
|
|
|
try {
|
|
|
|
await ipcServer.run(this)
|
|
|
|
} catch (err) {
|
2023-05-02 06:51:06 -05:00
|
|
|
logger.error('Cannot start local socket for IPC communication', err)
|
2023-04-21 08:05:27 -05:00
|
|
|
process.exit(-1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cleanup on exit
|
|
|
|
for (const code of [ 'SIGINT', 'SIGUSR1', 'SIGUSR2', 'uncaughtException' ]) {
|
2023-05-02 06:51:06 -05:00
|
|
|
process.on(code, async (err, origin) => {
|
|
|
|
if (code === 'uncaughtException') {
|
|
|
|
logger.error({ err, origin }, 'uncaughtException')
|
|
|
|
}
|
|
|
|
|
2023-04-21 08:05:27 -05:00
|
|
|
await this.onExit()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process jobs
|
|
|
|
await ensureDir(ConfigManager.Instance.getTranscodingDirectory())
|
|
|
|
await this.cleanupTMP()
|
|
|
|
|
|
|
|
logger.info(`Using ${ConfigManager.Instance.getTranscodingDirectory()} for transcoding directory`)
|
|
|
|
|
|
|
|
await this.checkAvailableJobs()
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
async registerRunner (options: {
|
|
|
|
url: string
|
|
|
|
registrationToken: string
|
|
|
|
runnerName: string
|
|
|
|
runnerDescription?: string
|
|
|
|
}) {
|
|
|
|
const { url, registrationToken, runnerName, runnerDescription } = options
|
|
|
|
|
|
|
|
logger.info(`Registering runner ${runnerName} on ${url}...`)
|
|
|
|
|
|
|
|
const serverCommand = new PeerTubeServerCommand({ url })
|
|
|
|
const { runnerToken } = await serverCommand.runners.register({ name: runnerName, description: runnerDescription, registrationToken })
|
|
|
|
|
|
|
|
const server: PeerTubeServer = Object.assign(serverCommand, {
|
|
|
|
runnerToken,
|
|
|
|
runnerName,
|
|
|
|
runnerDescription
|
|
|
|
})
|
|
|
|
|
|
|
|
this.loadServer(server)
|
|
|
|
await this.saveRegisteredInstancesInConf()
|
|
|
|
|
|
|
|
logger.info(`Registered runner ${runnerName} on ${url}`)
|
|
|
|
|
|
|
|
await this.checkAvailableJobs()
|
|
|
|
}
|
|
|
|
|
|
|
|
private loadServer (server: PeerTubeServer) {
|
|
|
|
this.servers.push(server)
|
|
|
|
|
|
|
|
const url = server.url + '/runners'
|
|
|
|
const socket = io(url, {
|
|
|
|
auth: {
|
|
|
|
runnerToken: server.runnerToken
|
|
|
|
},
|
|
|
|
transports: [ 'websocket' ]
|
|
|
|
})
|
|
|
|
|
|
|
|
socket.on('connect_error', err => logger.warn({ err }, `Cannot connect to ${url} socket`))
|
|
|
|
socket.on('connect', () => logger.info(`Connected to ${url} socket`))
|
|
|
|
socket.on('available-jobs', () => this.checkAvailableJobs())
|
|
|
|
|
|
|
|
this.sockets.set(server, socket)
|
|
|
|
}
|
|
|
|
|
|
|
|
async unregisterRunner (options: {
|
|
|
|
url: string
|
2023-05-26 02:03:50 -05:00
|
|
|
runnerName: string
|
2023-04-21 08:05:27 -05:00
|
|
|
}) {
|
2023-05-26 02:03:50 -05:00
|
|
|
const { url, runnerName } = options
|
2023-04-21 08:05:27 -05:00
|
|
|
|
2023-05-26 02:03:50 -05:00
|
|
|
const server = this.servers.find(s => s.url === url && s.runnerName === runnerName)
|
2023-04-21 08:05:27 -05:00
|
|
|
if (!server) {
|
2023-05-26 02:03:50 -05:00
|
|
|
logger.error(`Unknown server ${url} - ${runnerName} to unregister`)
|
2023-04-21 08:05:27 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-26 02:03:50 -05:00
|
|
|
logger.info(`Unregistering runner ${runnerName} on ${url}...`)
|
2023-04-21 08:05:27 -05:00
|
|
|
|
|
|
|
try {
|
|
|
|
await server.runners.unregister({ runnerToken: server.runnerToken })
|
|
|
|
} catch (err) {
|
2023-05-26 02:03:50 -05:00
|
|
|
logger.error({ err }, `Cannot unregister runner ${runnerName} on ${url}`)
|
2023-04-21 08:05:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
this.unloadServer(server)
|
|
|
|
await this.saveRegisteredInstancesInConf()
|
|
|
|
|
2023-05-26 02:03:50 -05:00
|
|
|
logger.info(`Unregistered runner ${runnerName} on ${url}`)
|
2023-04-21 08:05:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
private unloadServer (server: PeerTubeServer) {
|
|
|
|
this.servers = this.servers.filter(s => s !== server)
|
|
|
|
|
|
|
|
const socket = this.sockets.get(server)
|
|
|
|
socket.disconnect()
|
|
|
|
|
|
|
|
this.sockets.delete(server)
|
|
|
|
}
|
|
|
|
|
|
|
|
listRegistered () {
|
|
|
|
return {
|
|
|
|
servers: this.servers.map(s => {
|
|
|
|
return {
|
|
|
|
url: s.url,
|
|
|
|
runnerName: s.runnerName,
|
|
|
|
runnerDescription: s.runnerDescription
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
private async checkAvailableJobs () {
|
|
|
|
if (this.checkingAvailableJobs) return
|
|
|
|
|
|
|
|
this.checkingAvailableJobs = true
|
|
|
|
|
2023-05-19 02:23:20 -05:00
|
|
|
let hadAvailableJob = false
|
|
|
|
|
2023-04-21 08:05:27 -05:00
|
|
|
for (const server of this.servers) {
|
|
|
|
try {
|
2023-05-19 02:23:20 -05:00
|
|
|
logger.info('Checking available jobs on ' + server.url)
|
|
|
|
|
2023-04-21 08:05:27 -05:00
|
|
|
const job = await this.requestJob(server)
|
|
|
|
if (!job) continue
|
|
|
|
|
2023-05-19 02:23:20 -05:00
|
|
|
hadAvailableJob = true
|
|
|
|
|
2023-04-21 08:05:27 -05:00
|
|
|
await this.tryToExecuteJobAsync(server, job)
|
|
|
|
} catch (err) {
|
2023-05-19 02:23:20 -05:00
|
|
|
const code = (err.res?.body as PeerTubeProblemDocument)?.code
|
|
|
|
|
|
|
|
if (code === ServerErrorCode.RUNNER_JOB_NOT_IN_PROCESSING_STATE) {
|
|
|
|
logger.debug({ err }, 'Runner job is not in processing state anymore, retry later')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (code === ServerErrorCode.UNKNOWN_RUNNER_TOKEN) {
|
2023-04-21 08:05:27 -05:00
|
|
|
logger.error({ err }, `Unregistering ${server.url} as the runner token ${server.runnerToken} is invalid`)
|
|
|
|
|
2023-05-26 02:03:50 -05:00
|
|
|
await this.unregisterRunner({ url: server.url, runnerName: server.runnerName })
|
2023-04-21 08:05:27 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.error({ err }, `Cannot request/accept job on ${server.url} for runner ${server.runnerName}`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.checkingAvailableJobs = false
|
2023-05-19 02:23:20 -05:00
|
|
|
|
|
|
|
if (hadAvailableJob && this.canProcessMoreJobs()) {
|
2023-05-19 03:16:43 -05:00
|
|
|
await wait(2500)
|
|
|
|
|
2023-05-19 02:23:20 -05:00
|
|
|
this.checkAvailableJobs()
|
|
|
|
.catch(err => logger.error({ err }, 'Cannot check more available jobs'))
|
|
|
|
}
|
2023-04-21 08:05:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
private async requestJob (server: PeerTubeServer) {
|
|
|
|
logger.debug(`Requesting jobs on ${server.url} for runner ${server.runnerName}`)
|
|
|
|
|
|
|
|
const { availableJobs } = await server.runnerJobs.request({ runnerToken: server.runnerToken })
|
|
|
|
|
2023-05-04 08:29:34 -05:00
|
|
|
const filtered = availableJobs.filter(j => isJobSupported(j))
|
|
|
|
|
|
|
|
if (filtered.length === 0) {
|
2023-04-21 08:05:27 -05:00
|
|
|
logger.debug(`No job available on ${server.url} for runner ${server.runnerName}`)
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
|
2023-05-04 08:29:34 -05:00
|
|
|
return filtered[0]
|
2023-04-21 08:05:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
private async tryToExecuteJobAsync (server: PeerTubeServer, jobToAccept: { uuid: string }) {
|
2023-05-19 02:23:20 -05:00
|
|
|
if (!this.canProcessMoreJobs()) return
|
2023-04-21 08:05:27 -05:00
|
|
|
|
|
|
|
const { job } = await server.runnerJobs.accept({ runnerToken: server.runnerToken, jobUUID: jobToAccept.uuid })
|
|
|
|
|
|
|
|
const processingJob = { job, server }
|
|
|
|
this.processingJobs.push(processingJob)
|
|
|
|
|
|
|
|
processJob({ server, job, runnerToken: server.runnerToken })
|
|
|
|
.catch(err => {
|
|
|
|
logger.error({ err }, 'Cannot process job')
|
|
|
|
|
|
|
|
server.runnerJobs.error({ jobToken: job.jobToken, jobUUID: job.uuid, runnerToken: server.runnerToken, message: err.message })
|
|
|
|
.catch(err2 => logger.error({ err: err2 }, 'Cannot abort job after error'))
|
|
|
|
})
|
|
|
|
.finally(() => {
|
|
|
|
this.processingJobs = this.processingJobs.filter(p => p !== processingJob)
|
|
|
|
|
|
|
|
return this.checkAvailableJobs()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
private saveRegisteredInstancesInConf () {
|
|
|
|
const data = this.servers.map(s => {
|
|
|
|
return pick(s, [ 'url', 'runnerToken', 'runnerName', 'runnerDescription' ])
|
|
|
|
})
|
|
|
|
|
|
|
|
return ConfigManager.Instance.setRegisteredInstances(data)
|
|
|
|
}
|
|
|
|
|
2023-05-19 02:23:20 -05:00
|
|
|
private canProcessMoreJobs () {
|
|
|
|
return this.processingJobs.length < ConfigManager.Instance.getConfig().jobs.concurrency
|
|
|
|
}
|
|
|
|
|
2023-04-21 08:05:27 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
private async cleanupTMP () {
|
|
|
|
const files = await readdir(ConfigManager.Instance.getTranscodingDirectory())
|
|
|
|
|
|
|
|
for (const file of files) {
|
|
|
|
await remove(join(ConfigManager.Instance.getTranscodingDirectory(), file))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async onExit () {
|
2023-05-02 06:51:06 -05:00
|
|
|
if (this.cleaningUp) return
|
|
|
|
this.cleaningUp = true
|
|
|
|
|
|
|
|
logger.info('Cleaning up after program exit')
|
|
|
|
|
2023-04-21 08:05:27 -05:00
|
|
|
try {
|
|
|
|
for (const { server, job } of this.processingJobs) {
|
|
|
|
await server.runnerJobs.abort({
|
|
|
|
jobToken: job.jobToken,
|
|
|
|
jobUUID: job.uuid,
|
|
|
|
reason: 'Runner stopped',
|
|
|
|
runnerToken: server.runnerToken
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.cleanupTMP()
|
|
|
|
} catch (err) {
|
2023-05-02 06:51:06 -05:00
|
|
|
logger.error(err)
|
2023-04-21 08:05:27 -05:00
|
|
|
process.exit(-1)
|
|
|
|
}
|
|
|
|
|
|
|
|
process.exit()
|
|
|
|
}
|
|
|
|
|
|
|
|
static get Instance () {
|
|
|
|
return this.instance || (this.instance = new this())
|
|
|
|
}
|
|
|
|
}
|