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'
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: VideoModel[] = []
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 oldPrivacy = video.privacy
38 const isNewVideo = oldPrivacy === VideoPrivacy.PRIVATE
40 video.privacy = schedule.privacy
41 if (isNewVideo === true) video.publishedAt = new Date()
43 await video.save({ transaction: t })
44 await federateVideoIfNeeded(video, isNewVideo, t)
46 if (oldPrivacy === VideoPrivacy.UNLISTED || oldPrivacy === VideoPrivacy.PRIVATE) {
47 video.ScheduleVideoUpdate = schedule
48 publishedVideos.push(video)
52 await schedule.destroy({ transaction: t })
55 return publishedVideos
58 for (const v of publishedVideos) {
59 Notifier.Instance.notifyOnNewVideo(v)
60 Notifier.Instance.notifyOnVideoPublishedAfterScheduledUpdate(v)
64 static get Instance () {
65 return this.instance || (this.instance = new this())