]>
Commit | Line | Data |
---|---|---|
2baea0c7 | 1 | import { logger } from '../../helpers/logger' |
2baea0c7 C |
2 | import { AbstractScheduler } from './abstract-scheduler' |
3 | import { ScheduleVideoUpdateModel } from '../../models/video/schedule-video-update' | |
4 | import { retryTransactionWrapper } from '../../helpers/database-utils' | |
8dc8a34e | 5 | import { federateVideoIfNeeded } from '../activitypub/videos' |
74dc3bca | 6 | import { SCHEDULER_INTERVALS_MS } from '../../initializers/constants' |
cef534ed | 7 | import { Notifier } from '../notifier' |
74dc3bca | 8 | import { sequelizeTypescript } from '../../initializers/database' |
26d6bf65 | 9 | import { MVideoFullLight } from '@server/types/models' |
2baea0c7 C |
10 | |
11 | export class UpdateVideosScheduler extends AbstractScheduler { | |
12 | ||
13 | private static instance: AbstractScheduler | |
14 | ||
15 | protected schedulerIntervalMs = SCHEDULER_INTERVALS_MS.updateVideos | |
16 | ||
2baea0c7 C |
17 | private constructor () { |
18 | super() | |
19 | } | |
20 | ||
2f5c6b2f C |
21 | protected async internalExecute () { |
22 | return retryTransactionWrapper(this.updateVideos.bind(this)) | |
2baea0c7 C |
23 | } |
24 | ||
bbe0f064 C |
25 | private async updateVideos () { |
26 | if (!await ScheduleVideoUpdateModel.areVideosToUpdate()) return undefined | |
27 | ||
dc133480 | 28 | const publishedVideos = await sequelizeTypescript.transaction(async t => { |
2baea0c7 | 29 | const schedules = await ScheduleVideoUpdateModel.listVideosToUpdate(t) |
d7a25329 | 30 | const publishedVideos: MVideoFullLight[] = [] |
2baea0c7 C |
31 | |
32 | for (const schedule of schedules) { | |
33 | const video = schedule.Video | |
34 | logger.info('Executing scheduled video update on %s.', video.uuid) | |
35 | ||
36 | if (schedule.privacy) { | |
22a73cb8 C |
37 | const wasConfidentialVideo = video.isConfidential() |
38 | const isNewVideo = video.isNewVideo(schedule.privacy) | |
2baea0c7 | 39 | |
22a73cb8 | 40 | video.setPrivacy(schedule.privacy) |
77de223a | 41 | await video.save({ transaction: t }) |
2baea0c7 | 42 | await federateVideoIfNeeded(video, isNewVideo, t) |
cef534ed | 43 | |
22a73cb8 | 44 | if (wasConfidentialVideo) { |
d7a25329 C |
45 | const videoToPublish: MVideoFullLight = Object.assign(video, { ScheduleVideoUpdate: schedule, UserVideoHistories: [] }) |
46 | publishedVideos.push(videoToPublish) | |
cef534ed | 47 | } |
2baea0c7 C |
48 | } |
49 | ||
50 | await schedule.destroy({ transaction: t }) | |
51 | } | |
dc133480 C |
52 | |
53 | return publishedVideos | |
2baea0c7 | 54 | }) |
dc133480 C |
55 | |
56 | for (const v of publishedVideos) { | |
5b77537c | 57 | Notifier.Instance.notifyOnNewVideoIfNeeded(v) |
7ccddd7b | 58 | Notifier.Instance.notifyOnVideoPublishedAfterScheduledUpdate(v) |
dc133480 | 59 | } |
2baea0c7 C |
60 | } |
61 | ||
62 | static get Instance () { | |
63 | return this.instance || (this.instance = new this()) | |
64 | } | |
65 | } |