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 } from '../../initializers/constants'
7 import { VideoPrivacy } from '../../../shared/models/videos'
8 import { Notifier } from '../notifier'
9 import { sequelizeTypescript } from '../../initializers/database'
10 import { MVideoFullLight } from '@server/typings/models'
12 export class UpdateVideosScheduler extends AbstractScheduler {
14 private static instance: AbstractScheduler
16 protected schedulerIntervalMs = SCHEDULER_INTERVALS_MS.updateVideos
18 private constructor () {
22 protected async internalExecute () {
23 return retryTransactionWrapper(this.updateVideos.bind(this))
26 private async updateVideos () {
27 if (!await ScheduleVideoUpdateModel.areVideosToUpdate()) return undefined
29 const publishedVideos = await sequelizeTypescript.transaction(async t => {
30 const schedules = await ScheduleVideoUpdateModel.listVideosToUpdate(t)
31 const publishedVideos: MVideoFullLight[] = []
33 for (const schedule of schedules) {
34 const video = schedule.Video
35 logger.info('Executing scheduled video update on %s.', video.uuid)
37 if (schedule.privacy) {
38 const wasConfidentialVideo = video.isConfidential()
39 const isNewVideo = video.isNewVideo(schedule.privacy)
41 video.setPrivacy(schedule.privacy)
42 await video.save({ transaction: t })
43 await federateVideoIfNeeded(video, isNewVideo, t)
45 if (wasConfidentialVideo) {
46 const videoToPublish: MVideoFullLight = Object.assign(video, { ScheduleVideoUpdate: schedule, UserVideoHistories: [] })
47 publishedVideos.push(videoToPublish)
51 await schedule.destroy({ transaction: t })
54 return publishedVideos
57 for (const v of publishedVideos) {
58 Notifier.Instance.notifyOnNewVideoIfNeeded(v)
59 Notifier.Instance.notifyOnVideoPublishedAfterScheduledUpdate(v)
63 static get Instance () {
64 return this.instance || (this.instance = new this())