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'
10 import { VideoModel } from '@server/models/video/video'
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 schedules = await ScheduleVideoUpdateModel.listVideosToUpdate()
30 const publishedVideos: MVideoFullLight[] = []
32 for (const schedule of schedules) {
33 await sequelizeTypescript.transaction(async t => {
34 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(schedule.videoId, t)
36 logger.info('Executing scheduled video update on %s.', video.uuid)
38 if (schedule.privacy) {
39 const wasConfidentialVideo = video.isConfidential()
40 const isNewVideo = video.isNewVideo(schedule.privacy)
42 video.setPrivacy(schedule.privacy)
43 await video.save({ transaction: t })
44 await federateVideoIfNeeded(video, isNewVideo, t)
46 if (wasConfidentialVideo) {
47 publishedVideos.push(video)
51 await schedule.destroy({ transaction: t })
55 for (const v of publishedVideos) {
56 Notifier.Instance.notifyOnNewVideoIfNeeded(v)
57 Notifier.Instance.notifyOnVideoPublishedAfterScheduledUpdate(v)
61 static get Instance () {
62 return this.instance || (this.instance = new this())