import { Job } from 'bull'
import { remove } from 'fs-extra'
import { join } from 'path'
-import { logger } from '@server/helpers/logger'
+import { logger, loggerTagsFactory } from '@server/helpers/logger'
import { updateTorrentMetadata } from '@server/helpers/webtorrent'
import { CONFIG } from '@server/initializers/config'
import { P2P_MEDIA_LOADER_PEER_VERSION } from '@server/initializers/constants'
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'
-import { MoveObjectStoragePayload, VideoStorage } from '@shared/models'
+import { MoveObjectStoragePayload, VideoState, VideoStorage } from '@shared/models'
+
+const lTagsBase = loggerTagsFactory('move-object-storage')
export async function processMoveToObjectStorage (job: Job) {
const payload = job.data as MoveObjectStoragePayload
const video = await VideoModel.loadWithFiles(payload.videoUUID)
// No video, maybe deleted?
if (!video) {
- logger.info('Can\'t process job %d, video does not exist.', job.id)
+ logger.info('Can\'t process job %d, video does not exist.', job.id, lTagsBase(payload.videoUUID))
return undefined
}
+ const lTags = lTagsBase(video.uuid, video.url)
+
try {
if (video.VideoFiles) {
+ logger.debug('Moving %d webtorrent files for video %s.', video.VideoFiles.length, video.uuid, lTags)
+
await moveWebTorrentFiles(video)
}
if (video.VideoStreamingPlaylists) {
+ logger.debug('Moving HLS playlist of %s.', video.uuid)
+
await moveHLSFiles(video)
}
const pendingMove = await VideoJobInfoModel.decrease(video.uuid, 'pendingMove')
if (pendingMove === 0) {
- logger.info('Running cleanup after moving files to object storage (video %s in job %d)', video.uuid, job.id)
- await doAfterLastJob(video, payload.isNewVideo)
+ logger.info('Running cleanup after moving files to object storage (video %s in job %d)', video.uuid, job.id, lTags)
+
+ await doAfterLastJob({ video, previousVideoState: payload.previousVideoState, isNewVideo: payload.isNewVideo })
}
} catch (err) {
- logger.error('Cannot move video %s to object storage.', video.url, { err })
-
- await moveToFailedMoveToObjectStorageState(video)
- await VideoJobInfoModel.abortAllTasks(video.uuid, 'pendingMove')
+ await onMoveToObjectStorageFailure(job, err)
}
return payload.videoUUID
}
+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')
+}
+
// ---------------------------------------------------------------------------
async function moveWebTorrentFiles (video: MVideoWithAllFiles) {
}
}
-async function doAfterLastJob (video: MVideoWithAllFiles, isNewVideo: boolean) {
+async function doAfterLastJob (options: {
+ video: MVideoWithAllFiles
+ previousVideoState: VideoState
+ isNewVideo: boolean
+}) {
+ const { video, previousVideoState, isNewVideo } = options
+
for (const playlist of video.VideoStreamingPlaylists) {
if (playlist.storage === VideoStorage.OBJECT_STORAGE) continue
await remove(getHLSDirectory(video))
}
- await moveToNextState(video, isNewVideo)
+ await moveToNextState({ video, previousVideoState, isNewVideo })
}
async function onFileMoved (options: {