PeerTube/server/lib/jobs/handlers/video-file-transcoder.ts

50 lines
1.5 KiB
TypeScript
Raw Normal View History

2017-05-22 18:58:25 +00:00
import { database as db } from '../../../initializers/database'
import { updateVideoToFriends } from '../../friends'
2017-05-15 20:22:03 +00:00
import { logger } from '../../../helpers'
2017-06-10 20:15:25 +00:00
import { VideoInstance } from '../../../models'
import { VideoResolution } from '../../../../shared'
async function process (data: { videoUUID: string, resolution: VideoResolution }, jobId: number) {
const video = await db.Video.loadByUUIDAndPopulateAuthorAndPodAndTags(data.videoUUID)
// No video, maybe deleted?
if (!video) {
logger.info('Do not process job %d, video does not exist.', jobId, { videoUUID: video.uuid })
return undefined
}
await video.transcodeOriginalVideofile(data.resolution)
return video
}
function onError (err: Error, jobId: number) {
2017-07-07 16:26:12 +00:00
logger.error('Error when transcoding video file in job %d.', jobId, err)
return Promise.resolve()
}
2017-10-26 12:22:37 +00:00
async function onSuccess (jobId: number, video: VideoInstance) {
2017-10-17 13:37:40 +00:00
if (video === undefined) return undefined
logger.info('Job %d is a success.', jobId)
2017-10-26 12:22:37 +00:00
// Maybe the video changed in database, refresh it
const videoDatabase = await db.Video.loadByUUIDAndPopulateAuthorAndPodAndTags(video.uuid)
// Video does not exist anymore
if (!videoDatabase) return undefined
const remoteVideo = videoDatabase.toUpdateRemoteJSON()
// Now we'll add the video's meta data to our friends
2017-10-26 12:22:37 +00:00
await updateVideoToFriends(remoteVideo, null)
return
}
// ---------------------------------------------------------------------------
2017-05-15 20:22:03 +00:00
export {
process,
onError,
onSuccess
}