From be691a57c590348fd36d6e11dc1fe4cc8eaa0719 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 3 Oct 2018 15:52:39 +0200 Subject: [PATCH] Check video exists before extending its expiration --- .../schedulers/videos-redundancy-scheduler.ts | 50 ++++++++++++------- 1 file changed, 32 insertions(+), 18 deletions(-) diff --git a/server/lib/schedulers/videos-redundancy-scheduler.ts b/server/lib/schedulers/videos-redundancy-scheduler.ts index 607bdf67c..c49a8c89a 100644 --- a/server/lib/schedulers/videos-redundancy-scheduler.ts +++ b/server/lib/schedulers/videos-redundancy-scheduler.ts @@ -70,14 +70,28 @@ export class VideosRedundancyScheduler extends AbstractScheduler { for (const redundancyModel of expired) { try { - const redundancy = CONFIG.REDUNDANCY.VIDEOS.STRATEGIES.find(s => s.strategy === redundancyModel.strategy) - await this.extendsExpirationOf(redundancyModel, redundancy.minLifetime) + await this.extendsOrDeleteRedundancy(redundancyModel) } catch (err) { logger.error('Cannot extend expiration of %s video from our redundancy system.', this.buildEntryLogId(redundancyModel)) } } } + private async extendsOrDeleteRedundancy (redundancyModel: VideoRedundancyModel) { + // Refresh the video, maybe it was deleted + const video = await this.loadAndRefreshVideo(redundancyModel.VideoFile.Video.url) + + if (!video) { + logger.info('Destroying existing redundancy %s, because the associated video does not exist anymore.', redundancyModel.url) + + await redundancyModel.destroy() + return + } + + const redundancy = CONFIG.REDUNDANCY.VIDEOS.STRATEGIES.find(s => s.strategy === redundancyModel.strategy) + await this.extendsExpirationOf(redundancyModel, redundancy.minLifetime) + } + private async purgeRemoteExpired () { const expired = await VideoRedundancyModel.listRemoteExpired() @@ -109,23 +123,11 @@ export class VideosRedundancyScheduler extends AbstractScheduler { const serverActor = await getServerActor() for (const file of filesToDuplicate) { - // We need more attributes and check if the video still exists - const getVideoOptions = { - videoObject: file.Video.url, - syncParam: { likes: false, dislikes: false, shares: false, comments: false, thumbnail: false, refreshVideo: true }, - fetchType: 'all' as 'all' - } - const { video } = await getOrCreateVideoAndAccountAndChannel(getVideoOptions) + const video = await this.loadAndRefreshVideo(file.Video.url) - const existing = await VideoRedundancyModel.loadLocalByFileId(file.id) - if (existing) { - if (video) { - await this.extendsExpirationOf(existing, redundancy.minLifetime) - } else { - logger.info('Destroying existing redundancy %s, because the associated video does not exist anymore.', existing.url) - - await existing.destroy() - } + const existingRedundancy = await VideoRedundancyModel.loadLocalByFileId(file.id) + if (existingRedundancy) { + await this.extendsOrDeleteRedundancy(existingRedundancy) continue } @@ -203,4 +205,16 @@ export class VideosRedundancyScheduler extends AbstractScheduler { return files.reduce(fileReducer, 0) } + + private async loadAndRefreshVideo (videoUrl: string) { + // We need more attributes and check if the video still exists + const getVideoOptions = { + videoObject: videoUrl, + syncParam: { likes: false, dislikes: false, shares: false, comments: false, thumbnail: false, refreshVideo: true }, + fetchType: 'all' as 'all' + } + const { video } = await getOrCreateVideoAndAccountAndChannel(getVideoOptions) + + return video + } } -- 2.41.0