]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/schedulers/update-videos-scheduler.ts
Constants consistency
[github/Chocobozzz/PeerTube.git] / server / lib / schedulers / update-videos-scheduler.ts
1 import { VideoModel } from '@server/models/video/video'
2 import { MVideoFullLight } from '@server/types/models'
3 import { logger } from '../../helpers/logger'
4 import { SCHEDULER_INTERVALS_MS } from '../../initializers/constants'
5 import { sequelizeTypescript } from '../../initializers/database'
6 import { ScheduleVideoUpdateModel } from '../../models/video/schedule-video-update'
7 import { federateVideoIfNeeded } from '../activitypub/videos'
8 import { Notifier } from '../notifier'
9 import { AbstractScheduler } from './abstract-scheduler'
10
11 export class UpdateVideosScheduler extends AbstractScheduler {
12
13 private static instance: AbstractScheduler
14
15 protected schedulerIntervalMs = SCHEDULER_INTERVALS_MS.UPDATE_VIDEOS
16
17 private constructor () {
18 super()
19 }
20
21 protected async internalExecute () {
22 return this.updateVideos()
23 }
24
25 private async updateVideos () {
26 if (!await ScheduleVideoUpdateModel.areVideosToUpdate()) return undefined
27
28 const schedules = await ScheduleVideoUpdateModel.listVideosToUpdate()
29 const publishedVideos: MVideoFullLight[] = []
30
31 for (const schedule of schedules) {
32 await sequelizeTypescript.transaction(async t => {
33 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(schedule.videoId, t)
34
35 logger.info('Executing scheduled video update on %s.', video.uuid)
36
37 if (schedule.privacy) {
38 const wasConfidentialVideo = video.isConfidential()
39 const isNewVideo = video.isNewVideo(schedule.privacy)
40
41 video.setPrivacy(schedule.privacy)
42 await video.save({ transaction: t })
43 await federateVideoIfNeeded(video, isNewVideo, t)
44
45 if (wasConfidentialVideo) {
46 publishedVideos.push(video)
47 }
48 }
49
50 await schedule.destroy({ transaction: t })
51 })
52 }
53
54 for (const v of publishedVideos) {
55 Notifier.Instance.notifyOnNewVideoIfNeeded(v)
56 Notifier.Instance.notifyOnVideoPublishedAfterScheduledUpdate(v)
57 }
58 }
59
60 static get Instance () {
61 return this.instance || (this.instance = new this())
62 }
63 }