1 import { AbstractScheduler } from './abstract-scheduler'
2 import { CONFIG, JOB_TTL, REDUNDANCY, SCHEDULER_INTERVALS_MS } from '../../initializers'
3 import { logger } from '../../helpers/logger'
4 import { VideoRedundancyStrategy } from '../../../shared/models/redundancy'
5 import { VideoRedundancyModel } from '../../models/redundancy/video-redundancy'
6 import { VideoFileModel } from '../../models/video/video-file'
7 import { sortBy } from 'lodash'
8 import { downloadWebTorrentVideo } from '../../helpers/webtorrent'
9 import { join } from 'path'
10 import { rename } from 'fs-extra'
11 import { getServerActor } from '../../helpers/utils'
12 import { sendCreateCacheFile, sendUpdateCacheFile } from '../activitypub/send'
13 import { VideoModel } from '../../models/video/video'
14 import { getVideoCacheFileActivityPubUrl } from '../activitypub/url'
15 import { removeVideoRedundancy } from '../redundancy'
16 import { isTestInstance } from '../../helpers/core-utils'
18 export class VideosRedundancyScheduler extends AbstractScheduler {
20 private static instance: AbstractScheduler
21 private executing = false
23 protected schedulerIntervalMs = SCHEDULER_INTERVALS_MS.videosRedundancy
25 private constructor () {
30 if (this.executing) return
34 for (const obj of CONFIG.REDUNDANCY.VIDEOS) {
37 const videoToDuplicate = await this.findVideoToDuplicate(obj.strategy)
38 if (!videoToDuplicate) continue
40 const videoFiles = videoToDuplicate.VideoFiles
41 videoFiles.forEach(f => f.Video = videoToDuplicate)
43 const videosRedundancy = await VideoRedundancyModel.getVideoFiles(obj.strategy)
44 if (this.isTooHeavy(videosRedundancy, videoFiles, obj.size)) {
45 if (!isTestInstance()) logger.info('Video %s is too big for our cache, skipping.', videoToDuplicate.url)
49 logger.info('Will duplicate video %s in redundancy scheduler "%s".', videoToDuplicate.url, obj.strategy)
51 await this.createVideoRedundancy(obj.strategy, videoFiles)
53 logger.error('Cannot run videos redundancy %s.', obj.strategy, { err })
57 const expired = await VideoRedundancyModel.listAllExpired()
59 for (const m of expired) {
60 logger.info('Removing expired video %s from our redundancy system.', this.buildEntryLogId(m))
65 logger.error('Cannot remove %s video from our redundancy system.', this.buildEntryLogId(m))
69 this.executing = false
72 static get Instance () {
73 return this.instance || (this.instance = new this())
76 private findVideoToDuplicate (strategy: VideoRedundancyStrategy) {
77 if (strategy === 'most-views') return VideoRedundancyModel.findMostViewToDuplicate(REDUNDANCY.VIDEOS.RANDOMIZED_FACTOR)
80 private async createVideoRedundancy (strategy: VideoRedundancyStrategy, filesToDuplicate: VideoFileModel[]) {
81 const serverActor = await getServerActor()
83 for (const file of filesToDuplicate) {
84 const existing = await VideoRedundancyModel.loadByFileId(file.id)
86 logger.info('Duplicating %s - %d in videos redundancy with "%s" strategy.', file.Video.url, file.resolution, strategy)
88 existing.expiresOn = this.buildNewExpiration()
91 await sendUpdateCacheFile(serverActor, existing)
95 // We need more attributes and check if the video still exists
96 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(file.Video.id)
99 logger.info('Duplicating %s - %d in videos redundancy with "%s" strategy.', video.url, file.resolution, strategy)
101 const { baseUrlHttp, baseUrlWs } = video.getBaseUrls()
102 const magnetUri = video.generateMagnetUri(file, baseUrlHttp, baseUrlWs)
104 const tmpPath = await downloadWebTorrentVideo({ magnetUri }, JOB_TTL['video-import'])
106 const destPath = join(CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename(file))
107 await rename(tmpPath, destPath)
109 const createdModel = await VideoRedundancyModel.create({
110 expiresOn: new Date(Date.now() + REDUNDANCY.VIDEOS.EXPIRES_AFTER_MS),
111 url: getVideoCacheFileActivityPubUrl(file),
112 fileUrl: video.getVideoFileUrl(file, CONFIG.WEBSERVER.URL),
114 videoFileId: file.id,
115 actorId: serverActor.id
117 createdModel.VideoFile = file
119 await sendCreateCacheFile(serverActor, createdModel)
123 // Unused, but could be useful in the future, with a custom strategy
124 private async purgeVideosIfNeeded (videosRedundancy: VideoRedundancyModel[], filesToDuplicate: VideoFileModel[], maxSize: number) {
125 const sortedVideosRedundancy = sortBy(videosRedundancy, 'createdAt')
127 while (this.isTooHeavy(sortedVideosRedundancy, filesToDuplicate, maxSize)) {
128 const toDelete = sortedVideosRedundancy.shift()
130 const videoFile = toDelete.VideoFile
131 logger.info('Purging video %s (resolution %d) from our redundancy system.', videoFile.Video.url, videoFile.resolution)
133 await removeVideoRedundancy(toDelete, undefined)
136 return sortedVideosRedundancy
139 private isTooHeavy (videosRedundancy: VideoRedundancyModel[], filesToDuplicate: VideoFileModel[], maxSizeArg: number) {
140 const maxSize = maxSizeArg - this.getTotalFileSizes(filesToDuplicate)
142 const redundancyReducer = (previous: number, current: VideoRedundancyModel) => previous + current.VideoFile.size
143 const totalDuplicated = videosRedundancy.reduce(redundancyReducer, 0)
145 return totalDuplicated > maxSize
148 private buildNewExpiration () {
149 return new Date(Date.now() + REDUNDANCY.VIDEOS.EXPIRES_AFTER_MS)
152 private buildEntryLogId (object: VideoRedundancyModel) {
153 return `${object.VideoFile.Video.url}-${object.VideoFile.resolution}`
156 private getTotalFileSizes (files: VideoFileModel[]) {
157 const fileReducer = (previous: number, current: VideoFileModel) => previous + current.size
159 return files.reduce(fileReducer, 0)