2022-08-08 03:42:08 -05:00
|
|
|
import { Job } from 'bullmq'
|
2021-08-17 01:26:20 -05:00
|
|
|
import { remove } from 'fs-extra'
|
|
|
|
import { join } from 'path'
|
2022-02-01 07:19:44 -06:00
|
|
|
import { logger, loggerTagsFactory } from '@server/helpers/logger'
|
2021-12-08 04:07:19 -06:00
|
|
|
import { updateTorrentMetadata } from '@server/helpers/webtorrent'
|
2021-09-07 08:16:26 -05:00
|
|
|
import { P2P_MEDIA_LOADER_PEER_VERSION } from '@server/initializers/constants'
|
2023-07-11 02:21:13 -05:00
|
|
|
import { storeHLSFileFromFilename, storeWebVideoFile } from '@server/lib/object-storage'
|
2021-08-17 01:26:20 -05:00
|
|
|
import { getHLSDirectory, getHlsResolutionPlaylistFilename } from '@server/lib/paths'
|
2022-10-12 09:09:02 -05:00
|
|
|
import { VideoPathManager } from '@server/lib/video-path-manager'
|
2021-12-23 04:09:31 -06:00
|
|
|
import { moveToFailedMoveToObjectStorageState, moveToNextState } from '@server/lib/video-state'
|
2021-08-17 01:26:20 -05:00
|
|
|
import { VideoModel } from '@server/models/video/video'
|
|
|
|
import { VideoJobInfoModel } from '@server/models/video/video-job-info'
|
|
|
|
import { MStreamingPlaylistVideo, MVideo, MVideoFile, MVideoWithAllFiles } from '@server/types/models'
|
2022-03-22 08:35:04 -05:00
|
|
|
import { MoveObjectStoragePayload, VideoState, VideoStorage } from '@shared/models'
|
2021-08-17 01:26:20 -05:00
|
|
|
|
2022-02-01 07:19:44 -06:00
|
|
|
const lTagsBase = loggerTagsFactory('move-object-storage')
|
|
|
|
|
2021-08-27 07:32:44 -05:00
|
|
|
export async function processMoveToObjectStorage (job: Job) {
|
2021-08-17 01:26:20 -05:00
|
|
|
const payload = job.data as MoveObjectStoragePayload
|
2022-08-08 08:48:17 -05:00
|
|
|
logger.info('Moving video %s in job %s.', payload.videoUUID, job.id)
|
2021-08-17 01:26:20 -05:00
|
|
|
|
2023-05-05 06:41:48 -05:00
|
|
|
const fileMutexReleaser = await VideoPathManager.Instance.lockFiles(payload.videoUUID)
|
|
|
|
|
2021-08-17 01:26:20 -05:00
|
|
|
const video = await VideoModel.loadWithFiles(payload.videoUUID)
|
|
|
|
// No video, maybe deleted?
|
|
|
|
if (!video) {
|
2022-02-01 07:19:44 -06:00
|
|
|
logger.info('Can\'t process job %d, video does not exist.', job.id, lTagsBase(payload.videoUUID))
|
2023-05-05 06:41:48 -05:00
|
|
|
fileMutexReleaser()
|
2021-08-17 01:26:20 -05:00
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
|
2022-02-01 07:19:44 -06:00
|
|
|
const lTags = lTagsBase(video.uuid, video.url)
|
|
|
|
|
2021-12-23 04:09:31 -06:00
|
|
|
try {
|
|
|
|
if (video.VideoFiles) {
|
2023-07-11 02:21:13 -05:00
|
|
|
logger.debug('Moving %d web video files for video %s.', video.VideoFiles.length, video.uuid, lTags)
|
2022-02-01 07:19:44 -06:00
|
|
|
|
2023-07-11 02:21:13 -05:00
|
|
|
await moveWebVideoFiles(video)
|
2021-12-23 04:09:31 -06:00
|
|
|
}
|
2021-08-17 01:26:20 -05:00
|
|
|
|
2021-12-23 04:09:31 -06:00
|
|
|
if (video.VideoStreamingPlaylists) {
|
2022-02-01 07:19:44 -06:00
|
|
|
logger.debug('Moving HLS playlist of %s.', video.uuid)
|
|
|
|
|
2021-12-23 04:09:31 -06:00
|
|
|
await moveHLSFiles(video)
|
|
|
|
}
|
|
|
|
|
|
|
|
const pendingMove = await VideoJobInfoModel.decrease(video.uuid, 'pendingMove')
|
|
|
|
if (pendingMove === 0) {
|
2022-08-08 08:48:17 -05:00
|
|
|
logger.info('Running cleanup after moving files to object storage (video %s in job %s)', video.uuid, job.id, lTags)
|
2022-02-01 07:19:44 -06:00
|
|
|
|
2022-03-22 08:35:04 -05:00
|
|
|
await doAfterLastJob({ video, previousVideoState: payload.previousVideoState, isNewVideo: payload.isNewVideo })
|
2021-12-23 04:09:31 -06:00
|
|
|
}
|
|
|
|
} catch (err) {
|
2022-06-16 07:29:53 -05:00
|
|
|
await onMoveToObjectStorageFailure(job, err)
|
2022-10-25 04:50:44 -05:00
|
|
|
|
|
|
|
throw err
|
|
|
|
} finally {
|
|
|
|
fileMutexReleaser()
|
2021-08-17 01:26:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return payload.videoUUID
|
|
|
|
}
|
|
|
|
|
2022-06-16 07:29:53 -05:00
|
|
|
export async function onMoveToObjectStorageFailure (job: Job, err: any) {
|
|
|
|
const payload = job.data as MoveObjectStoragePayload
|
|
|
|
|
|
|
|
const video = await VideoModel.loadWithFiles(payload.videoUUID)
|
|
|
|
if (!video) return
|
|
|
|
|
|
|
|
logger.error('Cannot move video %s to object storage.', video.url, { err, ...lTagsBase(video.uuid, video.url) })
|
|
|
|
|
|
|
|
await moveToFailedMoveToObjectStorageState(video)
|
|
|
|
await VideoJobInfoModel.abortAllTasks(video.uuid, 'pendingMove')
|
|
|
|
}
|
|
|
|
|
2021-08-17 01:26:20 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2023-07-11 02:21:13 -05:00
|
|
|
async function moveWebVideoFiles (video: MVideoWithAllFiles) {
|
2021-08-17 01:26:20 -05:00
|
|
|
for (const file of video.VideoFiles) {
|
|
|
|
if (file.storage !== VideoStorage.FILE_SYSTEM) continue
|
|
|
|
|
2023-07-11 02:21:13 -05:00
|
|
|
const fileUrl = await storeWebVideoFile(video, file)
|
2021-08-17 01:26:20 -05:00
|
|
|
|
2022-10-12 09:09:02 -05:00
|
|
|
const oldPath = VideoPathManager.Instance.getFSVideoFileOutputPath(video, file)
|
2021-08-17 01:26:20 -05:00
|
|
|
await onFileMoved({ videoOrPlaylist: video, file, fileUrl, oldPath })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function moveHLSFiles (video: MVideoWithAllFiles) {
|
|
|
|
for (const playlist of video.VideoStreamingPlaylists) {
|
2021-11-18 07:35:08 -06:00
|
|
|
const playlistWithVideo = playlist.withVideo(video)
|
2021-08-17 01:26:20 -05:00
|
|
|
|
|
|
|
for (const file of playlist.VideoFiles) {
|
|
|
|
if (file.storage !== VideoStorage.FILE_SYSTEM) continue
|
|
|
|
|
|
|
|
// Resolution playlist
|
|
|
|
const playlistFilename = getHlsResolutionPlaylistFilename(file.filename)
|
2022-10-04 03:03:17 -05:00
|
|
|
await storeHLSFileFromFilename(playlistWithVideo, playlistFilename)
|
2021-08-17 01:26:20 -05:00
|
|
|
|
|
|
|
// Resolution fragmented file
|
2022-10-04 03:03:17 -05:00
|
|
|
const fileUrl = await storeHLSFileFromFilename(playlistWithVideo, file.filename)
|
2021-08-17 01:26:20 -05:00
|
|
|
|
|
|
|
const oldPath = join(getHLSDirectory(video), file.filename)
|
|
|
|
|
|
|
|
await onFileMoved({ videoOrPlaylist: Object.assign(playlist, { Video: video }), file, fileUrl, oldPath })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-22 08:35:04 -05:00
|
|
|
async function doAfterLastJob (options: {
|
|
|
|
video: MVideoWithAllFiles
|
|
|
|
previousVideoState: VideoState
|
|
|
|
isNewVideo: boolean
|
|
|
|
}) {
|
|
|
|
const { video, previousVideoState, isNewVideo } = options
|
|
|
|
|
2021-08-17 01:26:20 -05:00
|
|
|
for (const playlist of video.VideoStreamingPlaylists) {
|
|
|
|
if (playlist.storage === VideoStorage.OBJECT_STORAGE) continue
|
|
|
|
|
2021-11-18 07:35:08 -06:00
|
|
|
const playlistWithVideo = playlist.withVideo(video)
|
|
|
|
|
2021-08-17 01:26:20 -05:00
|
|
|
// Master playlist
|
2022-10-04 03:03:17 -05:00
|
|
|
playlist.playlistUrl = await storeHLSFileFromFilename(playlistWithVideo, playlist.playlistFilename)
|
2021-08-17 01:26:20 -05:00
|
|
|
// Sha256 segments file
|
2022-10-04 03:03:17 -05:00
|
|
|
playlist.segmentsSha256Url = await storeHLSFileFromFilename(playlistWithVideo, playlist.segmentsSha256Filename)
|
2021-08-17 01:26:20 -05:00
|
|
|
|
|
|
|
playlist.storage = VideoStorage.OBJECT_STORAGE
|
|
|
|
|
2021-09-07 08:16:26 -05:00
|
|
|
playlist.assignP2PMediaLoaderInfoHashes(video, playlist.VideoFiles)
|
|
|
|
playlist.p2pMediaLoaderPeerVersion = P2P_MEDIA_LOADER_PEER_VERSION
|
|
|
|
|
2021-08-17 01:26:20 -05:00
|
|
|
await playlist.save()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove empty hls video directory
|
|
|
|
if (video.VideoStreamingPlaylists) {
|
|
|
|
await remove(getHLSDirectory(video))
|
|
|
|
}
|
|
|
|
|
2022-03-22 08:35:04 -05:00
|
|
|
await moveToNextState({ video, previousVideoState, isNewVideo })
|
2021-08-17 01:26:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
async function onFileMoved (options: {
|
|
|
|
videoOrPlaylist: MVideo | MStreamingPlaylistVideo
|
|
|
|
file: MVideoFile
|
|
|
|
fileUrl: string
|
|
|
|
oldPath: string
|
|
|
|
}) {
|
|
|
|
const { videoOrPlaylist, file, fileUrl, oldPath } = options
|
|
|
|
|
|
|
|
file.fileUrl = fileUrl
|
|
|
|
file.storage = VideoStorage.OBJECT_STORAGE
|
|
|
|
|
2021-12-08 04:07:19 -06:00
|
|
|
await updateTorrentMetadata(videoOrPlaylist, file)
|
2021-08-17 01:26:20 -05:00
|
|
|
await file.save()
|
|
|
|
|
|
|
|
logger.debug('Removing %s because it\'s now on object storage', oldPath)
|
|
|
|
await remove(oldPath)
|
|
|
|
}
|