X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Flib%2Fschedulers%2Fvideos-redundancy-scheduler.ts;h=c49a8c89ad0496bd1ce93a9ec873b379aa9c60ed;hb=2fbe7f1933f4bd5de96e6428234e56965616120e;hp=11ee05a537a5f447d55045116b2f96393a16bcce;hpb=26649b4215ac68eed5601d9412d2d7ddee98b543;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/lib/schedulers/videos-redundancy-scheduler.ts b/server/lib/schedulers/videos-redundancy-scheduler.ts index 11ee05a53..c49a8c89a 100644 --- a/server/lib/schedulers/videos-redundancy-scheduler.ts +++ b/server/lib/schedulers/videos-redundancy-scheduler.ts @@ -1,5 +1,5 @@ import { AbstractScheduler } from './abstract-scheduler' -import { CONFIG, JOB_TTL, REDUNDANCY } from '../../initializers' +import { CONFIG, REDUNDANCY, VIDEO_IMPORT_TIMEOUT } from '../../initializers' import { logger } from '../../helpers/logger' import { VideosRedundancy } from '../../../shared/models/redundancy' import { VideoRedundancyModel } from '../../models/redundancy/video-redundancy' @@ -9,7 +9,6 @@ import { join } from 'path' import { rename } from 'fs-extra' import { getServerActor } from '../../helpers/utils' 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' @@ -71,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() @@ -110,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: 'only-video' as 'only-video' - } - 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 } @@ -142,7 +143,7 @@ export class VideosRedundancyScheduler extends AbstractScheduler { const { baseUrlHttp, baseUrlWs } = video.getBaseUrls() const magnetUri = video.generateMagnetUri(file, baseUrlHttp, baseUrlWs) - const tmpPath = await downloadWebTorrentVideo({ magnetUri }, JOB_TTL['video-import']) + const tmpPath = await downloadWebTorrentVideo({ magnetUri }, VIDEO_IMPORT_TIMEOUT) const destPath = join(CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename(file)) await rename(tmpPath, destPath) @@ -158,6 +159,8 @@ export class VideosRedundancyScheduler extends AbstractScheduler { createdModel.VideoFile = file await sendCreateCacheFile(serverActor, createdModel) + + logger.info('Duplicated %s - %d -> %s.', video.url, file.resolution, createdModel.url) } } @@ -202,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 + } }