]>
Commit | Line | Data |
---|---|---|
c48e82b5 C |
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' | |
17 | ||
18 | export class VideosRedundancyScheduler extends AbstractScheduler { | |
19 | ||
20 | private static instance: AbstractScheduler | |
21 | private executing = false | |
22 | ||
23 | protected schedulerIntervalMs = SCHEDULER_INTERVALS_MS.videosRedundancy | |
24 | ||
25 | private constructor () { | |
26 | super() | |
27 | } | |
28 | ||
29 | async execute () { | |
30 | if (this.executing) return | |
31 | ||
32 | this.executing = true | |
33 | ||
34 | for (const obj of CONFIG.REDUNDANCY.VIDEOS) { | |
35 | ||
36 | try { | |
37 | const videoToDuplicate = await this.findVideoToDuplicate(obj.strategy) | |
38 | if (!videoToDuplicate) continue | |
39 | ||
40 | const videoFiles = videoToDuplicate.VideoFiles | |
41 | videoFiles.forEach(f => f.Video = videoToDuplicate) | |
42 | ||
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) | |
46 | continue | |
47 | } | |
48 | ||
49 | logger.info('Will duplicate video %s in redundancy scheduler "%s".', videoToDuplicate.url, obj.strategy) | |
50 | ||
51 | await this.createVideoRedundancy(obj.strategy, videoFiles) | |
52 | } catch (err) { | |
53 | logger.error('Cannot run videos redundancy %s.', obj.strategy, { err }) | |
54 | } | |
55 | } | |
56 | ||
57 | const expired = await VideoRedundancyModel.listAllExpired() | |
58 | ||
59 | for (const m of expired) { | |
60 | logger.info('Removing expired video %s from our redundancy system.', this.buildEntryLogId(m)) | |
61 | ||
62 | try { | |
63 | await m.destroy() | |
64 | } catch (err) { | |
65 | logger.error('Cannot remove %s video from our redundancy system.', this.buildEntryLogId(m)) | |
66 | } | |
67 | } | |
68 | ||
69 | this.executing = false | |
70 | } | |
71 | ||
72 | static get Instance () { | |
73 | return this.instance || (this.instance = new this()) | |
74 | } | |
75 | ||
76 | private findVideoToDuplicate (strategy: VideoRedundancyStrategy) { | |
77 | if (strategy === 'most-views') return VideoRedundancyModel.findMostViewToDuplicate(REDUNDANCY.VIDEOS.RANDOMIZED_FACTOR) | |
b36f41ca C |
78 | |
79 | if (strategy === 'trending') return VideoRedundancyModel.findTrendingToDuplicate(REDUNDANCY.VIDEOS.RANDOMIZED_FACTOR) | |
c48e82b5 C |
80 | } |
81 | ||
82 | private async createVideoRedundancy (strategy: VideoRedundancyStrategy, filesToDuplicate: VideoFileModel[]) { | |
83 | const serverActor = await getServerActor() | |
84 | ||
85 | for (const file of filesToDuplicate) { | |
86 | const existing = await VideoRedundancyModel.loadByFileId(file.id) | |
87 | if (existing) { | |
88 | logger.info('Duplicating %s - %d in videos redundancy with "%s" strategy.', file.Video.url, file.resolution, strategy) | |
89 | ||
90 | existing.expiresOn = this.buildNewExpiration() | |
91 | await existing.save() | |
92 | ||
93 | await sendUpdateCacheFile(serverActor, existing) | |
94 | continue | |
95 | } | |
96 | ||
97 | // We need more attributes and check if the video still exists | |
98 | const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(file.Video.id) | |
99 | if (!video) continue | |
100 | ||
101 | logger.info('Duplicating %s - %d in videos redundancy with "%s" strategy.', video.url, file.resolution, strategy) | |
102 | ||
103 | const { baseUrlHttp, baseUrlWs } = video.getBaseUrls() | |
104 | const magnetUri = video.generateMagnetUri(file, baseUrlHttp, baseUrlWs) | |
105 | ||
106 | const tmpPath = await downloadWebTorrentVideo({ magnetUri }, JOB_TTL['video-import']) | |
107 | ||
108 | const destPath = join(CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename(file)) | |
109 | await rename(tmpPath, destPath) | |
110 | ||
111 | const createdModel = await VideoRedundancyModel.create({ | |
112 | expiresOn: new Date(Date.now() + REDUNDANCY.VIDEOS.EXPIRES_AFTER_MS), | |
113 | url: getVideoCacheFileActivityPubUrl(file), | |
114 | fileUrl: video.getVideoFileUrl(file, CONFIG.WEBSERVER.URL), | |
115 | strategy, | |
116 | videoFileId: file.id, | |
117 | actorId: serverActor.id | |
118 | }) | |
119 | createdModel.VideoFile = file | |
120 | ||
121 | await sendCreateCacheFile(serverActor, createdModel) | |
122 | } | |
123 | } | |
124 | ||
125 | // Unused, but could be useful in the future, with a custom strategy | |
126 | private async purgeVideosIfNeeded (videosRedundancy: VideoRedundancyModel[], filesToDuplicate: VideoFileModel[], maxSize: number) { | |
127 | const sortedVideosRedundancy = sortBy(videosRedundancy, 'createdAt') | |
128 | ||
129 | while (this.isTooHeavy(sortedVideosRedundancy, filesToDuplicate, maxSize)) { | |
130 | const toDelete = sortedVideosRedundancy.shift() | |
131 | ||
132 | const videoFile = toDelete.VideoFile | |
133 | logger.info('Purging video %s (resolution %d) from our redundancy system.', videoFile.Video.url, videoFile.resolution) | |
134 | ||
135 | await removeVideoRedundancy(toDelete, undefined) | |
136 | } | |
137 | ||
138 | return sortedVideosRedundancy | |
139 | } | |
140 | ||
141 | private isTooHeavy (videosRedundancy: VideoRedundancyModel[], filesToDuplicate: VideoFileModel[], maxSizeArg: number) { | |
142 | const maxSize = maxSizeArg - this.getTotalFileSizes(filesToDuplicate) | |
143 | ||
144 | const redundancyReducer = (previous: number, current: VideoRedundancyModel) => previous + current.VideoFile.size | |
145 | const totalDuplicated = videosRedundancy.reduce(redundancyReducer, 0) | |
146 | ||
147 | return totalDuplicated > maxSize | |
148 | } | |
149 | ||
150 | private buildNewExpiration () { | |
151 | return new Date(Date.now() + REDUNDANCY.VIDEOS.EXPIRES_AFTER_MS) | |
152 | } | |
153 | ||
154 | private buildEntryLogId (object: VideoRedundancyModel) { | |
155 | return `${object.VideoFile.Video.url}-${object.VideoFile.resolution}` | |
156 | } | |
157 | ||
158 | private getTotalFileSizes (files: VideoFileModel[]) { | |
159 | const fileReducer = (previous: number, current: VideoFileModel) => previous + current.size | |
160 | ||
161 | return files.reduce(fileReducer, 0) | |
162 | } | |
163 | } |