More robust runner update handler
This commit is contained in:
parent
7d758898dc
commit
a34c612f38
|
@ -1,5 +1,4 @@
|
||||||
import { remove } from 'fs-extra'
|
import { remove } from 'fs-extra'
|
||||||
import { throttle } from 'lodash'
|
|
||||||
import { ConfigManager, downloadFile, logger } from 'packages/peertube-runner/shared'
|
import { ConfigManager, downloadFile, logger } from 'packages/peertube-runner/shared'
|
||||||
import { join } from 'path'
|
import { join } from 'path'
|
||||||
import { buildUUID } from '@shared/extra-utils'
|
import { buildUUID } from '@shared/extra-utils'
|
||||||
|
@ -60,17 +59,26 @@ export function buildFFmpegVOD (options: {
|
||||||
? 500
|
? 500
|
||||||
: 60000
|
: 60000
|
||||||
|
|
||||||
const updateJobProgress = throttle((progress: number) => {
|
let progress: number
|
||||||
if (progress < 0 || progress > 100) progress = undefined
|
|
||||||
|
|
||||||
|
const interval = setInterval(() => {
|
||||||
updateTranscodingProgress({ server, job, runnerToken, progress })
|
updateTranscodingProgress({ server, job, runnerToken, progress })
|
||||||
.catch(err => logger.error({ err }, 'Cannot send job progress'))
|
.catch(err => logger.error({ err }, 'Cannot send job progress'))
|
||||||
}, updateInterval, { trailing: false })
|
}, updateInterval)
|
||||||
|
|
||||||
return new FFmpegVOD({
|
return new FFmpegVOD({
|
||||||
...getCommonFFmpegOptions(),
|
...getCommonFFmpegOptions(),
|
||||||
|
|
||||||
updateJobProgress
|
onError: () => clearInterval(interval),
|
||||||
|
onEnd: () => clearInterval(interval),
|
||||||
|
|
||||||
|
updateJobProgress: arg => {
|
||||||
|
if (arg < 0 || arg > 100) {
|
||||||
|
progress = undefined
|
||||||
|
} else {
|
||||||
|
progress = arg
|
||||||
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -21,6 +21,8 @@ export interface FFmpegCommandWrapperOptions {
|
||||||
lTags?: { tags: string[] }
|
lTags?: { tags: string[] }
|
||||||
|
|
||||||
updateJobProgress?: (progress?: number) => void
|
updateJobProgress?: (progress?: number) => void
|
||||||
|
onEnd?: () => void
|
||||||
|
onError?: (err: Error) => void
|
||||||
}
|
}
|
||||||
|
|
||||||
export class FFmpegCommandWrapper {
|
export class FFmpegCommandWrapper {
|
||||||
|
@ -37,6 +39,8 @@ export class FFmpegCommandWrapper {
|
||||||
private readonly lTags: { tags: string[] }
|
private readonly lTags: { tags: string[] }
|
||||||
|
|
||||||
private readonly updateJobProgress: (progress?: number) => void
|
private readonly updateJobProgress: (progress?: number) => void
|
||||||
|
private readonly onEnd?: () => void
|
||||||
|
private readonly onError?: (err: Error) => void
|
||||||
|
|
||||||
private command: FfmpegCommand
|
private command: FfmpegCommand
|
||||||
|
|
||||||
|
@ -48,7 +52,11 @@ export class FFmpegCommandWrapper {
|
||||||
this.threads = options.threads
|
this.threads = options.threads
|
||||||
this.logger = options.logger
|
this.logger = options.logger
|
||||||
this.lTags = options.lTags || { tags: [] }
|
this.lTags = options.lTags || { tags: [] }
|
||||||
|
|
||||||
this.updateJobProgress = options.updateJobProgress
|
this.updateJobProgress = options.updateJobProgress
|
||||||
|
|
||||||
|
this.onEnd = options.onEnd
|
||||||
|
this.onError = options.onError
|
||||||
}
|
}
|
||||||
|
|
||||||
getAvailableEncoders () {
|
getAvailableEncoders () {
|
||||||
|
@ -101,12 +109,16 @@ export class FFmpegCommandWrapper {
|
||||||
this.command.on('error', (err, stdout, stderr) => {
|
this.command.on('error', (err, stdout, stderr) => {
|
||||||
if (silent !== true) this.logger.error('Error in ffmpeg.', { stdout, stderr, shellCommand, ...this.lTags })
|
if (silent !== true) this.logger.error('Error in ffmpeg.', { stdout, stderr, shellCommand, ...this.lTags })
|
||||||
|
|
||||||
|
if (this.onError) this.onError(err)
|
||||||
|
|
||||||
rej(err)
|
rej(err)
|
||||||
})
|
})
|
||||||
|
|
||||||
this.command.on('end', (stdout, stderr) => {
|
this.command.on('end', (stdout, stderr) => {
|
||||||
this.logger.debug('FFmpeg command ended.', { stdout, stderr, shellCommand, ...this.lTags })
|
this.logger.debug('FFmpeg command ended.', { stdout, stderr, shellCommand, ...this.lTags })
|
||||||
|
|
||||||
|
if (this.onEnd) this.onEnd()
|
||||||
|
|
||||||
res()
|
res()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue