Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit be691a5

Browse files
committed
Check video exists before extending its expiration
1 parent 5bc8745 commit be691a5

File tree

1 file changed

+32
-18
lines changed

1 file changed

+32
-18
lines changed

server/lib/schedulers/videos-redundancy-scheduler.ts

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,28 @@ export class VideosRedundancyScheduler extends AbstractScheduler {
7070

7171
for (const redundancyModel of expired) {
7272
try {
73-
const redundancy = CONFIG.REDUNDANCY.VIDEOS.STRATEGIES.find(s => s.strategy === redundancyModel.strategy)
74-
await this.extendsExpirationOf(redundancyModel, redundancy.minLifetime)
73+
await this.extendsOrDeleteRedundancy(redundancyModel)
7574
} catch (err) {
7675
logger.error('Cannot extend expiration of %s video from our redundancy system.', this.buildEntryLogId(redundancyModel))
7776
}
7877
}
7978
}
8079

80+
private async extendsOrDeleteRedundancy (redundancyModel: VideoRedundancyModel) {
81+
// Refresh the video, maybe it was deleted
82+
const video = await this.loadAndRefreshVideo(redundancyModel.VideoFile.Video.url)
83+
84+
if (!video) {
85+
logger.info('Destroying existing redundancy %s, because the associated video does not exist anymore.', redundancyModel.url)
86+
87+
await redundancyModel.destroy()
88+
return
89+
}
90+
91+
const redundancy = CONFIG.REDUNDANCY.VIDEOS.STRATEGIES.find(s => s.strategy === redundancyModel.strategy)
92+
await this.extendsExpirationOf(redundancyModel, redundancy.minLifetime)
93+
}
94+
8195
private async purgeRemoteExpired () {
8296
const expired = await VideoRedundancyModel.listRemoteExpired()
8397

@@ -109,23 +123,11 @@ export class VideosRedundancyScheduler extends AbstractScheduler {
109123
const serverActor = await getServerActor()
110124

111125
for (const file of filesToDuplicate) {
112-
// We need more attributes and check if the video still exists
113-
const getVideoOptions = {
114-
videoObject: file.Video.url,
115-
syncParam: { likes: false, dislikes: false, shares: false, comments: false, thumbnail: false, refreshVideo: true },
116-
fetchType: 'all' as 'all'
117-
}
118-
const { video } = await getOrCreateVideoAndAccountAndChannel(getVideoOptions)
126+
const video = await this.loadAndRefreshVideo(file.Video.url)
119127

120-
const existing = await VideoRedundancyModel.loadLocalByFileId(file.id)
121-
if (existing) {
122-
if (video) {
123-
await this.extendsExpirationOf(existing, redundancy.minLifetime)
124-
} else {
125-
logger.info('Destroying existing redundancy %s, because the associated video does not exist anymore.', existing.url)
126-
127-
await existing.destroy()
128-
}
128+
const existingRedundancy = await VideoRedundancyModel.loadLocalByFileId(file.id)
129+
if (existingRedundancy) {
130+
await this.extendsOrDeleteRedundancy(existingRedundancy)
129131

130132
continue
131133
}
@@ -203,4 +205,16 @@ export class VideosRedundancyScheduler extends AbstractScheduler {
203205

204206
return files.reduce(fileReducer, 0)
205207
}
208+
209+
private async loadAndRefreshVideo (videoUrl: string) {
210+
// We need more attributes and check if the video still exists
211+
const getVideoOptions = {
212+
videoObject: videoUrl,
213+
syncParam: { likes: false, dislikes: false, shares: false, comments: false, thumbnail: false, refreshVideo: true },
214+
fetchType: 'all' as 'all'
215+
}
216+
const { video } = await getOrCreateVideoAndAccountAndChannel(getVideoOptions)
217+
218+
return video
219+
}
206220
}

0 commit comments

Comments
 (0)