]>
Commit | Line | Data |
---|---|---|
c48e82b5 | 1 | import { AbstractScheduler } from './abstract-scheduler' |
cf9166cf | 2 | import { CONFIG, REDUNDANCY, VIDEO_IMPORT_TIMEOUT } from '../../initializers' |
c48e82b5 | 3 | import { logger } from '../../helpers/logger' |
e5565833 | 4 | import { VideosRedundancy } from '../../../shared/models/redundancy' |
c48e82b5 C |
5 | import { VideoRedundancyModel } from '../../models/redundancy/video-redundancy' |
6 | import { VideoFileModel } from '../../models/video/video-file' | |
c48e82b5 C |
7 | import { downloadWebTorrentVideo } from '../../helpers/webtorrent' |
8 | import { join } from 'path' | |
f481c4f9 | 9 | import { move } from 'fs-extra' |
c48e82b5 C |
10 | import { getServerActor } from '../../helpers/utils' |
11 | import { sendCreateCacheFile, sendUpdateCacheFile } from '../activitypub/send' | |
c48e82b5 | 12 | import { getVideoCacheFileActivityPubUrl } from '../activitypub/url' |
e5565833 | 13 | import { removeVideoRedundancy } from '../redundancy' |
26649b42 | 14 | import { getOrCreateVideoAndAccountAndChannel } from '../activitypub' |
c48e82b5 C |
15 | |
16 | export class VideosRedundancyScheduler extends AbstractScheduler { | |
17 | ||
18 | private static instance: AbstractScheduler | |
c48e82b5 | 19 | |
f9f899b9 | 20 | protected schedulerIntervalMs = CONFIG.REDUNDANCY.VIDEOS.CHECK_INTERVAL |
c48e82b5 C |
21 | |
22 | private constructor () { | |
23 | super() | |
24 | } | |
25 | ||
2f5c6b2f | 26 | protected async internalExecute () { |
d9bdd007 | 27 | for (const obj of CONFIG.REDUNDANCY.VIDEOS.STRATEGIES) { |
e5565833 | 28 | logger.info('Running redundancy scheduler for strategy %s.', obj.strategy) |
1cfa8d68 | 29 | |
c48e82b5 | 30 | try { |
3f6b6a56 | 31 | const videoToDuplicate = await this.findVideoToDuplicate(obj) |
c48e82b5 C |
32 | if (!videoToDuplicate) continue |
33 | ||
34 | const videoFiles = videoToDuplicate.VideoFiles | |
35 | videoFiles.forEach(f => f.Video = videoToDuplicate) | |
36 | ||
e5565833 C |
37 | await this.purgeCacheIfNeeded(obj, videoFiles) |
38 | ||
39 | if (await this.isTooHeavy(obj, videoFiles)) { | |
40 | logger.info('Video %s is too big for our cache, skipping.', videoToDuplicate.url) | |
c48e82b5 C |
41 | continue |
42 | } | |
43 | ||
44 | logger.info('Will duplicate video %s in redundancy scheduler "%s".', videoToDuplicate.url, obj.strategy) | |
45 | ||
e5565833 | 46 | await this.createVideoRedundancy(obj, videoFiles) |
c48e82b5 C |
47 | } catch (err) { |
48 | logger.error('Cannot run videos redundancy %s.', obj.strategy, { err }) | |
49 | } | |
50 | } | |
51 | ||
e5565833 C |
52 | await this.extendsLocalExpiration() |
53 | ||
54 | await this.purgeRemoteExpired() | |
f9f899b9 C |
55 | } |
56 | ||
57 | static get Instance () { | |
58 | return this.instance || (this.instance = new this()) | |
59 | } | |
60 | ||
e5565833 C |
61 | private async extendsLocalExpiration () { |
62 | const expired = await VideoRedundancyModel.listLocalExpired() | |
63 | ||
64 | for (const redundancyModel of expired) { | |
65 | try { | |
be691a57 | 66 | await this.extendsOrDeleteRedundancy(redundancyModel) |
e5565833 C |
67 | } catch (err) { |
68 | logger.error('Cannot extend expiration of %s video from our redundancy system.', this.buildEntryLogId(redundancyModel)) | |
69 | } | |
70 | } | |
71 | } | |
c48e82b5 | 72 | |
be691a57 C |
73 | private async extendsOrDeleteRedundancy (redundancyModel: VideoRedundancyModel) { |
74 | // Refresh the video, maybe it was deleted | |
75 | const video = await this.loadAndRefreshVideo(redundancyModel.VideoFile.Video.url) | |
76 | ||
77 | if (!video) { | |
78 | logger.info('Destroying existing redundancy %s, because the associated video does not exist anymore.', redundancyModel.url) | |
79 | ||
80 | await redundancyModel.destroy() | |
81 | return | |
82 | } | |
83 | ||
84 | const redundancy = CONFIG.REDUNDANCY.VIDEOS.STRATEGIES.find(s => s.strategy === redundancyModel.strategy) | |
85 | await this.extendsExpirationOf(redundancyModel, redundancy.minLifetime) | |
86 | } | |
87 | ||
e5565833 C |
88 | private async purgeRemoteExpired () { |
89 | const expired = await VideoRedundancyModel.listRemoteExpired() | |
c48e82b5 | 90 | |
e5565833 | 91 | for (const redundancyModel of expired) { |
c48e82b5 | 92 | try { |
e5565833 | 93 | await removeVideoRedundancy(redundancyModel) |
c48e82b5 | 94 | } catch (err) { |
e5565833 | 95 | logger.error('Cannot remove redundancy %s from our redundancy system.', this.buildEntryLogId(redundancyModel)) |
c48e82b5 C |
96 | } |
97 | } | |
c48e82b5 C |
98 | } |
99 | ||
3f6b6a56 C |
100 | private findVideoToDuplicate (cache: VideosRedundancy) { |
101 | if (cache.strategy === 'most-views') { | |
102 | return VideoRedundancyModel.findMostViewToDuplicate(REDUNDANCY.VIDEOS.RANDOMIZED_FACTOR) | |
103 | } | |
104 | ||
105 | if (cache.strategy === 'trending') { | |
106 | return VideoRedundancyModel.findTrendingToDuplicate(REDUNDANCY.VIDEOS.RANDOMIZED_FACTOR) | |
107 | } | |
b36f41ca | 108 | |
3f6b6a56 | 109 | if (cache.strategy === 'recently-added') { |
7348b1fd | 110 | const minViews = cache.minViews |
3f6b6a56 C |
111 | return VideoRedundancyModel.findRecentlyAddedToDuplicate(REDUNDANCY.VIDEOS.RANDOMIZED_FACTOR, minViews) |
112 | } | |
c48e82b5 C |
113 | } |
114 | ||
e5565833 | 115 | private async createVideoRedundancy (redundancy: VideosRedundancy, filesToDuplicate: VideoFileModel[]) { |
c48e82b5 C |
116 | const serverActor = await getServerActor() |
117 | ||
118 | for (const file of filesToDuplicate) { | |
be691a57 | 119 | const video = await this.loadAndRefreshVideo(file.Video.url) |
26649b42 | 120 | |
be691a57 C |
121 | const existingRedundancy = await VideoRedundancyModel.loadLocalByFileId(file.id) |
122 | if (existingRedundancy) { | |
123 | await this.extendsOrDeleteRedundancy(existingRedundancy) | |
c48e82b5 | 124 | |
c48e82b5 C |
125 | continue |
126 | } | |
127 | ||
26649b42 C |
128 | if (!video) { |
129 | logger.info('Video %s we want to duplicate does not existing anymore, skipping.', file.Video.url) | |
130 | ||
131 | continue | |
132 | } | |
c48e82b5 | 133 | |
e5565833 | 134 | logger.info('Duplicating %s - %d in videos redundancy with "%s" strategy.', video.url, file.resolution, redundancy.strategy) |
c48e82b5 C |
135 | |
136 | const { baseUrlHttp, baseUrlWs } = video.getBaseUrls() | |
137 | const magnetUri = video.generateMagnetUri(file, baseUrlHttp, baseUrlWs) | |
138 | ||
cf9166cf | 139 | const tmpPath = await downloadWebTorrentVideo({ magnetUri }, VIDEO_IMPORT_TIMEOUT) |
c48e82b5 | 140 | |
b9fffa29 | 141 | const destPath = join(CONFIG.STORAGE.REDUNDANCY_DIR, video.getVideoFilename(file)) |
f481c4f9 | 142 | await move(tmpPath, destPath) |
c48e82b5 C |
143 | |
144 | const createdModel = await VideoRedundancyModel.create({ | |
e5565833 | 145 | expiresOn: this.buildNewExpiration(redundancy.minLifetime), |
c48e82b5 | 146 | url: getVideoCacheFileActivityPubUrl(file), |
b9fffa29 | 147 | fileUrl: video.getVideoRedundancyUrl(file, CONFIG.WEBSERVER.URL), |
e5565833 | 148 | strategy: redundancy.strategy, |
c48e82b5 C |
149 | videoFileId: file.id, |
150 | actorId: serverActor.id | |
151 | }) | |
152 | createdModel.VideoFile = file | |
153 | ||
154 | await sendCreateCacheFile(serverActor, createdModel) | |
792e5b8e C |
155 | |
156 | logger.info('Duplicated %s - %d -> %s.', video.url, file.resolution, createdModel.url) | |
c48e82b5 C |
157 | } |
158 | } | |
159 | ||
e5565833 C |
160 | private async extendsExpirationOf (redundancy: VideoRedundancyModel, expiresAfterMs: number) { |
161 | logger.info('Extending expiration of %s.', redundancy.url) | |
162 | ||
163 | const serverActor = await getServerActor() | |
164 | ||
165 | redundancy.expiresOn = this.buildNewExpiration(expiresAfterMs) | |
166 | await redundancy.save() | |
167 | ||
168 | await sendUpdateCacheFile(serverActor, redundancy) | |
169 | } | |
170 | ||
171 | private async purgeCacheIfNeeded (redundancy: VideosRedundancy, filesToDuplicate: VideoFileModel[]) { | |
172 | while (this.isTooHeavy(redundancy, filesToDuplicate)) { | |
173 | const toDelete = await VideoRedundancyModel.loadOldestLocalThatAlreadyExpired(redundancy.strategy, redundancy.minLifetime) | |
174 | if (!toDelete) return | |
175 | ||
176 | await removeVideoRedundancy(toDelete) | |
177 | } | |
178 | } | |
179 | ||
180 | private async isTooHeavy (redundancy: VideosRedundancy, filesToDuplicate: VideoFileModel[]) { | |
742ddee1 | 181 | const maxSize = redundancy.size |
c48e82b5 | 182 | |
e5565833 | 183 | const totalDuplicated = await VideoRedundancyModel.getTotalDuplicated(redundancy.strategy) |
742ddee1 | 184 | const totalWillDuplicate = totalDuplicated + this.getTotalFileSizes(filesToDuplicate) |
c48e82b5 | 185 | |
742ddee1 | 186 | return totalWillDuplicate > maxSize |
c48e82b5 C |
187 | } |
188 | ||
e5565833 C |
189 | private buildNewExpiration (expiresAfterMs: number) { |
190 | return new Date(Date.now() + expiresAfterMs) | |
c48e82b5 C |
191 | } |
192 | ||
193 | private buildEntryLogId (object: VideoRedundancyModel) { | |
194 | return `${object.VideoFile.Video.url}-${object.VideoFile.resolution}` | |
195 | } | |
196 | ||
197 | private getTotalFileSizes (files: VideoFileModel[]) { | |
198 | const fileReducer = (previous: number, current: VideoFileModel) => previous + current.size | |
199 | ||
200 | return files.reduce(fileReducer, 0) | |
201 | } | |
be691a57 C |
202 | |
203 | private async loadAndRefreshVideo (videoUrl: string) { | |
204 | // We need more attributes and check if the video still exists | |
205 | const getVideoOptions = { | |
206 | videoObject: videoUrl, | |
207 | syncParam: { likes: false, dislikes: false, shares: false, comments: false, thumbnail: false, refreshVideo: true }, | |
208 | fetchType: 'all' as 'all' | |
209 | } | |
210 | const { video } = await getOrCreateVideoAndAccountAndChannel(getVideoOptions) | |
211 | ||
212 | return video | |
213 | } | |
c48e82b5 | 214 | } |