diff options
author | Chocobozzz <me@florianbigard.com> | 2018-09-14 11:05:38 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2018-09-14 11:05:38 +0200 |
commit | 3f6b6a565dc98a658ec9d8f697252788c0faa46d (patch) | |
tree | 9549d3722b2d2d6501d85b4a56e6bfd3ea14fed3 /server/lib/schedulers/videos-redundancy-scheduler.ts | |
parent | 780daa7e91336116a5163156f298c9ad875ec1e3 (diff) | |
download | PeerTube-3f6b6a565dc98a658ec9d8f697252788c0faa46d.tar.gz PeerTube-3f6b6a565dc98a658ec9d8f697252788c0faa46d.tar.zst PeerTube-3f6b6a565dc98a658ec9d8f697252788c0faa46d.zip |
Add recently added redundancy strategy
Diffstat (limited to 'server/lib/schedulers/videos-redundancy-scheduler.ts')
-rw-r--r-- | server/lib/schedulers/videos-redundancy-scheduler.ts | 44 |
1 files changed, 17 insertions, 27 deletions
diff --git a/server/lib/schedulers/videos-redundancy-scheduler.ts b/server/lib/schedulers/videos-redundancy-scheduler.ts index c1e619249..8b91d750b 100644 --- a/server/lib/schedulers/videos-redundancy-scheduler.ts +++ b/server/lib/schedulers/videos-redundancy-scheduler.ts | |||
@@ -1,7 +1,7 @@ | |||
1 | import { AbstractScheduler } from './abstract-scheduler' | 1 | import { AbstractScheduler } from './abstract-scheduler' |
2 | import { CONFIG, JOB_TTL, REDUNDANCY, SCHEDULER_INTERVALS_MS } from '../../initializers' | 2 | import { CONFIG, JOB_TTL, REDUNDANCY, SCHEDULER_INTERVALS_MS } from '../../initializers' |
3 | import { logger } from '../../helpers/logger' | 3 | import { logger } from '../../helpers/logger' |
4 | import { VideoRedundancyStrategy } from '../../../shared/models/redundancy' | 4 | import { RecentlyAddedStrategy, VideoRedundancyStrategy, VideosRedundancy } from '../../../shared/models/redundancy' |
5 | import { VideoRedundancyModel } from '../../models/redundancy/video-redundancy' | 5 | import { VideoRedundancyModel } from '../../models/redundancy/video-redundancy' |
6 | import { VideoFileModel } from '../../models/video/video-file' | 6 | import { VideoFileModel } from '../../models/video/video-file' |
7 | import { sortBy } from 'lodash' | 7 | import { sortBy } from 'lodash' |
@@ -32,16 +32,14 @@ export class VideosRedundancyScheduler extends AbstractScheduler { | |||
32 | this.executing = true | 32 | this.executing = true |
33 | 33 | ||
34 | for (const obj of CONFIG.REDUNDANCY.VIDEOS) { | 34 | for (const obj of CONFIG.REDUNDANCY.VIDEOS) { |
35 | |||
36 | try { | 35 | try { |
37 | const videoToDuplicate = await this.findVideoToDuplicate(obj.strategy) | 36 | const videoToDuplicate = await this.findVideoToDuplicate(obj) |
38 | if (!videoToDuplicate) continue | 37 | if (!videoToDuplicate) continue |
39 | 38 | ||
40 | const videoFiles = videoToDuplicate.VideoFiles | 39 | const videoFiles = videoToDuplicate.VideoFiles |
41 | videoFiles.forEach(f => f.Video = videoToDuplicate) | 40 | videoFiles.forEach(f => f.Video = videoToDuplicate) |
42 | 41 | ||
43 | const videosRedundancy = await VideoRedundancyModel.getVideoFiles(obj.strategy) | 42 | if (await this.isTooHeavy(obj.strategy, videoFiles, obj.size)) { |
44 | if (this.isTooHeavy(videosRedundancy, videoFiles, obj.size)) { | ||
45 | if (!isTestInstance()) logger.info('Video %s is too big for our cache, skipping.', videoToDuplicate.url) | 43 | if (!isTestInstance()) logger.info('Video %s is too big for our cache, skipping.', videoToDuplicate.url) |
46 | continue | 44 | continue |
47 | } | 45 | } |
@@ -73,10 +71,19 @@ export class VideosRedundancyScheduler extends AbstractScheduler { | |||
73 | return this.instance || (this.instance = new this()) | 71 | return this.instance || (this.instance = new this()) |
74 | } | 72 | } |
75 | 73 | ||
76 | private findVideoToDuplicate (strategy: VideoRedundancyStrategy) { | 74 | private findVideoToDuplicate (cache: VideosRedundancy) { |
77 | if (strategy === 'most-views') return VideoRedundancyModel.findMostViewToDuplicate(REDUNDANCY.VIDEOS.RANDOMIZED_FACTOR) | 75 | if (cache.strategy === 'most-views') { |
76 | return VideoRedundancyModel.findMostViewToDuplicate(REDUNDANCY.VIDEOS.RANDOMIZED_FACTOR) | ||
77 | } | ||
78 | |||
79 | if (cache.strategy === 'trending') { | ||
80 | return VideoRedundancyModel.findTrendingToDuplicate(REDUNDANCY.VIDEOS.RANDOMIZED_FACTOR) | ||
81 | } | ||
78 | 82 | ||
79 | if (strategy === 'trending') return VideoRedundancyModel.findTrendingToDuplicate(REDUNDANCY.VIDEOS.RANDOMIZED_FACTOR) | 83 | if (cache.strategy === 'recently-added') { |
84 | const minViews = (cache as RecentlyAddedStrategy).minViews | ||
85 | return VideoRedundancyModel.findRecentlyAddedToDuplicate(REDUNDANCY.VIDEOS.RANDOMIZED_FACTOR, minViews) | ||
86 | } | ||
80 | } | 87 | } |
81 | 88 | ||
82 | private async createVideoRedundancy (strategy: VideoRedundancyStrategy, filesToDuplicate: VideoFileModel[]) { | 89 | private async createVideoRedundancy (strategy: VideoRedundancyStrategy, filesToDuplicate: VideoFileModel[]) { |
@@ -122,27 +129,10 @@ export class VideosRedundancyScheduler extends AbstractScheduler { | |||
122 | } | 129 | } |
123 | } | 130 | } |
124 | 131 | ||
125 | // Unused, but could be useful in the future, with a custom strategy | 132 | private async isTooHeavy (strategy: VideoRedundancyStrategy, filesToDuplicate: VideoFileModel[], maxSizeArg: number) { |
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) | 133 | const maxSize = maxSizeArg - this.getTotalFileSizes(filesToDuplicate) |
143 | 134 | ||
144 | const redundancyReducer = (previous: number, current: VideoRedundancyModel) => previous + current.VideoFile.size | 135 | const totalDuplicated = await VideoRedundancyModel.getTotalDuplicated(strategy) |
145 | const totalDuplicated = videosRedundancy.reduce(redundancyReducer, 0) | ||
146 | 136 | ||
147 | return totalDuplicated > maxSize | 137 | return totalDuplicated > maxSize |
148 | } | 138 | } |