1 import { isTestInstance } from '../../helpers/core-utils'
2 import { logger } from '../../helpers/logger'
3 import { JobQueue } from '../job-queue'
4 import { AbstractScheduler } from './abstract-scheduler'
5 import { ScheduleVideoUpdateModel } from '../../models/video/schedule-video-update'
6 import { retryTransactionWrapper } from '../../helpers/database-utils'
7 import { federateVideoIfNeeded } from '../activitypub'
8 import { SCHEDULER_INTERVALS_MS, sequelizeTypescript } from '../../initializers'
9 import { VideoPrivacy } from '../../../shared/models/videos'
11 export class UpdateVideosScheduler extends AbstractScheduler {
13 private static instance: AbstractScheduler
15 protected schedulerIntervalMs = SCHEDULER_INTERVALS_MS.updateVideos
17 private isRunning = false
19 private constructor () {
24 if (this.isRunning === true) return
28 await retryTransactionWrapper(this.updateVideos.bind(this))
30 logger.error('Cannot execute update videos scheduler.', { err })
32 this.isRunning = false
36 private async updateVideos () {
37 if (!await ScheduleVideoUpdateModel.areVideosToUpdate()) return undefined
39 return sequelizeTypescript.transaction(async t => {
40 const schedules = await ScheduleVideoUpdateModel.listVideosToUpdate(t)
42 for (const schedule of schedules) {
43 const video = schedule.Video
44 logger.info('Executing scheduled video update on %s.', video.uuid)
46 if (schedule.privacy) {
47 const oldPrivacy = video.privacy
49 video.privacy = schedule.privacy
50 await video.save({ transaction: t })
52 const isNewVideo = oldPrivacy === VideoPrivacy.PRIVATE
53 await federateVideoIfNeeded(video, isNewVideo, t)
56 await schedule.destroy({ transaction: t })
61 static get Instance () {
62 return this.instance || (this.instance = new this())