Ensure video existence before duplicating it
authorChocobozzz <me@florianbigard.com>
Mon, 1 Oct 2018 08:31:42 +0000 (10:31 +0200)
committerChocobozzz <me@florianbigard.com>
Mon, 1 Oct 2018 08:31:42 +0000 (10:31 +0200)
server/lib/activitypub/videos.ts
server/lib/schedulers/videos-redundancy-scheduler.ts

index 3dccabe120387670e2dd5b67738026ef30a06a2b..b1e7f20b9686a4618086d106f04e1cdd77c020d3 100644 (file)
@@ -370,13 +370,15 @@ async function refreshVideoIfNeeded (options: {
   try {
     const { response, videoObject } = await fetchRemoteVideo(video.url)
     if (response.statusCode === 404) {
+      logger.info('Cannot refresh remote video %s: video does not exist anymore. Deleting it.', video.url)
+
       // Video does not exist anymore
       await video.destroy()
       return undefined
     }
 
     if (videoObject === undefined) {
-      logger.warn('Cannot refresh remote video: invalid body.')
+      logger.warn('Cannot refresh remote video %s: invalid body.', video.url)
       return video
     }
 
@@ -390,8 +392,10 @@ async function refreshVideoIfNeeded (options: {
       channel: channelActor.VideoChannel,
       updateViews: options.refreshViews
     }
-    await retryTransactionWrapper(updateVideoFromAP, updateOptions)
-    await syncVideoExternalAttributes(video, videoObject, options.syncParam)
+    const videoUpdated = await retryTransactionWrapper(updateVideoFromAP, updateOptions)
+    await syncVideoExternalAttributes(videoUpdated, videoObject, options.syncParam)
+
+    return videoUpdated
   } catch (err) {
     logger.warn('Cannot refresh video.', { err })
     return video
index d324adfd03a5a36286cdd9240e3b7a6a8c34c146..11ee05a537a5f447d55045116b2f96393a16bcce 100644 (file)
@@ -12,6 +12,7 @@ import { sendCreateCacheFile, sendUpdateCacheFile } from '../activitypub/send'
 import { VideoModel } from '../../models/video/video'
 import { getVideoCacheFileActivityPubUrl } from '../activitypub/url'
 import { removeVideoRedundancy } from '../redundancy'
+import { getOrCreateVideoAndAccountAndChannel } from '../activitypub'
 
 export class VideosRedundancyScheduler extends AbstractScheduler {
 
@@ -109,16 +110,32 @@ 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: 'only-video' as 'only-video'
+      }
+      const { video } = await getOrCreateVideoAndAccountAndChannel(getVideoOptions)
+
       const existing = await VideoRedundancyModel.loadLocalByFileId(file.id)
       if (existing) {
-        await this.extendsExpirationOf(existing, redundancy.minLifetime)
+        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()
+        }
 
         continue
       }
 
-      // We need more attributes and check if the video still exists
-      const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(file.Video.id)
-      if (!video) continue
+      if (!video) {
+        logger.info('Video %s we want to duplicate does not existing anymore, skipping.', file.Video.url)
+
+        continue
+      }
 
       logger.info('Duplicating %s - %d in videos redundancy with "%s" strategy.', video.url, file.resolution, redundancy.strategy)