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'
10 export class UpdateVideosScheduler extends AbstractScheduler {
12 private static instance: AbstractScheduler
14 protected schedulerIntervalMs = SCHEDULER_INTERVALS_MS.updateVideos
16 private constructor () {
20 protected async internalExecute () {
21 return retryTransactionWrapper(this.updateVideos.bind(this))
24 private async updateVideos () {
25 if (!await ScheduleVideoUpdateModel.areVideosToUpdate()) return undefined
27 return sequelizeTypescript.transaction(async t => {
28 const schedules = await ScheduleVideoUpdateModel.listVideosToUpdate(t)
30 for (const schedule of schedules) {
31 const video = schedule.Video
32 logger.info('Executing scheduled video update on %s.', video.uuid)
34 if (schedule.privacy) {
35 const oldPrivacy = video.privacy
36 const isNewVideo = oldPrivacy === VideoPrivacy.PRIVATE
38 video.privacy = schedule.privacy
39 if (isNewVideo === true) video.publishedAt = new Date()
41 await video.save({ transaction: t })
42 await federateVideoIfNeeded(video, isNewVideo, t)
44 if (oldPrivacy === VideoPrivacy.UNLISTED || oldPrivacy === VideoPrivacy.PRIVATE) {
45 Notifier.Instance.notifyOnNewVideo(video)
49 await schedule.destroy({ transaction: t })
54 static get Instance () {
55 return this.instance || (this.instance = new this())