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/videos'
6 import { SCHEDULER_INTERVALS_MS } from '../../initializers/constants'
7 import { Notifier } from '../notifier'
8 import { sequelizeTypescript } from '../../initializers/database'
9 import { MVideoFullLight } from '@server/types/models'
11 export class UpdateVideosScheduler extends AbstractScheduler {
13 private static instance: AbstractScheduler
15 protected schedulerIntervalMs = SCHEDULER_INTERVALS_MS.updateVideos
17 private constructor () {
21 protected async internalExecute () {
22 return retryTransactionWrapper(this.updateVideos.bind(this))
25 private async updateVideos () {
26 if (!await ScheduleVideoUpdateModel.areVideosToUpdate()) return undefined
28 const publishedVideos = await sequelizeTypescript.transaction(async t => {
29 const schedules = await ScheduleVideoUpdateModel.listVideosToUpdate(t)
30 const publishedVideos: MVideoFullLight[] = []
32 for (const schedule of schedules) {
33 const video = schedule.Video
34 logger.info('Executing scheduled video update on %s.', video.uuid)
36 if (schedule.privacy) {
37 const wasConfidentialVideo = video.isConfidential()
38 const isNewVideo = video.isNewVideo(schedule.privacy)
40 video.setPrivacy(schedule.privacy)
41 await video.save({ transaction: t })
42 await federateVideoIfNeeded(video, isNewVideo, t)
44 if (wasConfidentialVideo) {
45 const videoToPublish: MVideoFullLight = Object.assign(video, { ScheduleVideoUpdate: schedule, UserVideoHistories: [] })
46 publishedVideos.push(videoToPublish)
50 await schedule.destroy({ transaction: t })
53 return publishedVideos
56 for (const v of publishedVideos) {
57 Notifier.Instance.notifyOnNewVideoIfNeeded(v)
58 Notifier.Instance.notifyOnVideoPublishedAfterScheduledUpdate(v)
62 static get Instance () {
63 return this.instance || (this.instance = new this())