]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/schedulers/update-videos-scheduler.ts
Add ability to schedule video publication
[github/Chocobozzz/PeerTube.git] / server / lib / schedulers / update-videos-scheduler.ts
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'
10
11 export class UpdateVideosScheduler extends AbstractScheduler {
12
13 private static instance: AbstractScheduler
14
15 protected schedulerIntervalMs = SCHEDULER_INTERVALS_MS.updateVideos
16
17 private isRunning = false
18
19 private constructor () {
20 super()
21 }
22
23 async execute () {
24 if (this.isRunning === true) return
25 this.isRunning = true
26
27 try {
28 await retryTransactionWrapper(this.updateVideos.bind(this))
29 } catch (err) {
30 logger.error('Cannot execute update videos scheduler.', { err })
31 } finally {
32 this.isRunning = false
33 }
34 }
35
36 private async updateVideos () {
37 if (!await ScheduleVideoUpdateModel.areVideosToUpdate()) return undefined
38
39 return sequelizeTypescript.transaction(async t => {
40 const schedules = await ScheduleVideoUpdateModel.listVideosToUpdate(t)
41
42 for (const schedule of schedules) {
43 const video = schedule.Video
44 logger.info('Executing scheduled video update on %s.', video.uuid)
45
46 if (schedule.privacy) {
47 const oldPrivacy = video.privacy
48
49 video.privacy = schedule.privacy
50 await video.save({ transaction: t })
51
52 const isNewVideo = oldPrivacy === VideoPrivacy.PRIVATE
53 await federateVideoIfNeeded(video, isNewVideo, t)
54 }
55
56 await schedule.destroy({ transaction: t })
57 }
58 })
59 }
60
61 static get Instance () {
62 return this.instance || (this.instance = new this())
63 }
64 }