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'
9 export class UpdateVideosScheduler extends AbstractScheduler {
11 private static instance: AbstractScheduler
13 protected schedulerIntervalMs = SCHEDULER_INTERVALS_MS.updateVideos
15 private isRunning = false
17 private constructor () {
22 if (this.isRunning === true) return
26 await retryTransactionWrapper(this.updateVideos.bind(this))
28 logger.error('Cannot execute update videos scheduler.', { err })
30 this.isRunning = false
34 private async updateVideos () {
35 if (!await ScheduleVideoUpdateModel.areVideosToUpdate()) return undefined
37 return sequelizeTypescript.transaction(async t => {
38 const schedules = await ScheduleVideoUpdateModel.listVideosToUpdate(t)
40 for (const schedule of schedules) {
41 const video = schedule.Video
42 logger.info('Executing scheduled video update on %s.', video.uuid)
44 if (schedule.privacy) {
45 const oldPrivacy = video.privacy
46 const isNewVideo = oldPrivacy === VideoPrivacy.PRIVATE
48 video.privacy = schedule.privacy
49 if (isNewVideo === true) video.publishedAt = new Date()
51 await video.save({ transaction: t })
52 await federateVideoIfNeeded(video, isNewVideo, t)
55 await schedule.destroy({ transaction: t })
60 static get Instance () {
61 return this.instance || (this.instance = new this())