]>
Commit | Line | Data |
---|---|---|
1 | import { logger } from '../../helpers/logger' | |
2 | import { AbstractScheduler } from './abstract-scheduler' | |
3 | import { ScheduleVideoUpdateModel } from '../../models/video/schedule-video-update' | |
4 | import { retryTransactionWrapper } from '../../helpers/database-utils' | |
5 | import { federateVideoIfNeeded } from '../activitypub' | |
6 | import { SCHEDULER_INTERVALS_MS, sequelizeTypescript } from '../../initializers' | |
7 | import { VideoPrivacy } from '../../../shared/models/videos' | |
8 | import { Notifier } from '../notifier' | |
9 | import { VideoModel } from '../../models/video/video' | |
10 | ||
11 | export class UpdateVideosScheduler extends AbstractScheduler { | |
12 | ||
13 | private static instance: AbstractScheduler | |
14 | ||
15 | protected schedulerIntervalMs = SCHEDULER_INTERVALS_MS.updateVideos | |
16 | ||
17 | private constructor () { | |
18 | super() | |
19 | } | |
20 | ||
21 | protected async internalExecute () { | |
22 | return retryTransactionWrapper(this.updateVideos.bind(this)) | |
23 | } | |
24 | ||
25 | private async updateVideos () { | |
26 | if (!await ScheduleVideoUpdateModel.areVideosToUpdate()) return undefined | |
27 | ||
28 | const publishedVideos = await sequelizeTypescript.transaction(async t => { | |
29 | const schedules = await ScheduleVideoUpdateModel.listVideosToUpdate(t) | |
30 | const publishedVideos: VideoModel[] = [] | |
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) { | |
37 | const oldPrivacy = video.privacy | |
38 | const isNewVideo = oldPrivacy === VideoPrivacy.PRIVATE | |
39 | ||
40 | video.privacy = schedule.privacy | |
41 | if (isNewVideo === true) video.publishedAt = new Date() | |
42 | ||
43 | await video.save({ transaction: t }) | |
44 | await federateVideoIfNeeded(video, isNewVideo, t) | |
45 | ||
46 | if (oldPrivacy === VideoPrivacy.UNLISTED || oldPrivacy === VideoPrivacy.PRIVATE) { | |
47 | video.ScheduleVideoUpdate = schedule | |
48 | publishedVideos.push(video) | |
49 | } | |
50 | } | |
51 | ||
52 | await schedule.destroy({ transaction: t }) | |
53 | } | |
54 | ||
55 | return publishedVideos | |
56 | }) | |
57 | ||
58 | for (const v of publishedVideos) { | |
59 | Notifier.Instance.notifyOnNewVideo(v) | |
60 | Notifier.Instance.notifyOnPendingVideoPublished(v) | |
61 | } | |
62 | } | |
63 | ||
64 | static get Instance () { | |
65 | return this.instance || (this.instance = new this()) | |
66 | } | |
67 | } |