]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/schedulers/update-videos-scheduler.ts
Fix video import CLI script
[github/Chocobozzz/PeerTube.git] / server / lib / schedulers / update-videos-scheduler.ts
CommitLineData
2baea0c7 1import { logger } from '../../helpers/logger'
2baea0c7
C
2import { AbstractScheduler } from './abstract-scheduler'
3import { ScheduleVideoUpdateModel } from '../../models/video/schedule-video-update'
4import { retryTransactionWrapper } from '../../helpers/database-utils'
5import { federateVideoIfNeeded } from '../activitypub'
74dc3bca 6import { SCHEDULER_INTERVALS_MS } from '../../initializers/constants'
2baea0c7 7import { VideoPrivacy } from '../../../shared/models/videos'
cef534ed 8import { Notifier } from '../notifier'
dc133480 9import { VideoModel } from '../../models/video/video'
74dc3bca 10import { sequelizeTypescript } from '../../initializers/database'
2baea0c7
C
11
12export class UpdateVideosScheduler extends AbstractScheduler {
13
14 private static instance: AbstractScheduler
15
16 protected schedulerIntervalMs = SCHEDULER_INTERVALS_MS.updateVideos
17
2baea0c7
C
18 private constructor () {
19 super()
20 }
21
2f5c6b2f
C
22 protected async internalExecute () {
23 return retryTransactionWrapper(this.updateVideos.bind(this))
2baea0c7
C
24 }
25
bbe0f064
C
26 private async updateVideos () {
27 if (!await ScheduleVideoUpdateModel.areVideosToUpdate()) return undefined
28
dc133480 29 const publishedVideos = await sequelizeTypescript.transaction(async t => {
2baea0c7 30 const schedules = await ScheduleVideoUpdateModel.listVideosToUpdate(t)
dc133480 31 const publishedVideos: VideoModel[] = []
2baea0c7
C
32
33 for (const schedule of schedules) {
34 const video = schedule.Video
35 logger.info('Executing scheduled video update on %s.', video.uuid)
36
37 if (schedule.privacy) {
38 const oldPrivacy = video.privacy
77de223a 39 const isNewVideo = oldPrivacy === VideoPrivacy.PRIVATE
2baea0c7
C
40
41 video.privacy = schedule.privacy
77de223a 42 if (isNewVideo === true) video.publishedAt = new Date()
2baea0c7 43
77de223a 44 await video.save({ transaction: t })
2baea0c7 45 await federateVideoIfNeeded(video, isNewVideo, t)
cef534ed
C
46
47 if (oldPrivacy === VideoPrivacy.UNLISTED || oldPrivacy === VideoPrivacy.PRIVATE) {
dc133480
C
48 video.ScheduleVideoUpdate = schedule
49 publishedVideos.push(video)
cef534ed 50 }
2baea0c7
C
51 }
52
53 await schedule.destroy({ transaction: t })
54 }
dc133480
C
55
56 return publishedVideos
2baea0c7 57 })
dc133480
C
58
59 for (const v of publishedVideos) {
60 Notifier.Instance.notifyOnNewVideo(v)
7ccddd7b 61 Notifier.Instance.notifyOnVideoPublishedAfterScheduledUpdate(v)
dc133480 62 }
2baea0c7
C
63 }
64
65 static get Instance () {
66 return this.instance || (this.instance = new this())
67 }
68}