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