]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/schedulers/update-videos-scheduler.ts
Merge branch 'release/4.3.0' into develop
[github/Chocobozzz/PeerTube.git] / server / lib / schedulers / update-videos-scheduler.ts
CommitLineData
c6406f47
C
1import { VideoModel } from '@server/models/video/video'
2import { MVideoFullLight } from '@server/types/models'
2baea0c7 3import { logger } from '../../helpers/logger'
c6406f47
C
4import { SCHEDULER_INTERVALS_MS } from '../../initializers/constants'
5import { sequelizeTypescript } from '../../initializers/database'
2baea0c7 6import { ScheduleVideoUpdateModel } from '../../models/video/schedule-video-update'
8dc8a34e 7import { federateVideoIfNeeded } from '../activitypub/videos'
cef534ed 8import { Notifier } from '../notifier'
c6406f47 9import { AbstractScheduler } from './abstract-scheduler'
2baea0c7
C
10
11export class UpdateVideosScheduler extends AbstractScheduler {
12
13 private static instance: AbstractScheduler
14
61953742 15 protected schedulerIntervalMs = SCHEDULER_INTERVALS_MS.UPDATE_VIDEOS
2baea0c7 16
2baea0c7
C
17 private constructor () {
18 super()
19 }
20
2f5c6b2f 21 protected async internalExecute () {
c6406f47 22 return this.updateVideos()
2baea0c7
C
23 }
24
bbe0f064
C
25 private async updateVideos () {
26 if (!await ScheduleVideoUpdateModel.areVideosToUpdate()) return undefined
27
fd6a74a8
C
28 const schedules = await ScheduleVideoUpdateModel.listVideosToUpdate()
29 const publishedVideos: MVideoFullLight[] = []
30
31 for (const schedule of schedules) {
32 await sequelizeTypescript.transaction(async t => {
4fae2b1f 33 const video = await VideoModel.loadFull(schedule.videoId, t)
2baea0c7 34
2baea0c7
C
35 logger.info('Executing scheduled video update on %s.', video.uuid)
36
37 if (schedule.privacy) {
22a73cb8
C
38 const wasConfidentialVideo = video.isConfidential()
39 const isNewVideo = video.isNewVideo(schedule.privacy)
2baea0c7 40
22a73cb8 41 video.setPrivacy(schedule.privacy)
77de223a 42 await video.save({ transaction: t })
2baea0c7 43 await federateVideoIfNeeded(video, isNewVideo, t)
cef534ed 44
22a73cb8 45 if (wasConfidentialVideo) {
fd6a74a8 46 publishedVideos.push(video)
cef534ed 47 }
2baea0c7
C
48 }
49
50 await schedule.destroy({ transaction: t })
fd6a74a8
C
51 })
52 }
dc133480
C
53
54 for (const v of publishedVideos) {
5b77537c 55 Notifier.Instance.notifyOnNewVideoIfNeeded(v)
7ccddd7b 56 Notifier.Instance.notifyOnVideoPublishedAfterScheduledUpdate(v)
dc133480 57 }
2baea0c7
C
58 }
59
60 static get Instance () {
61 return this.instance || (this.instance = new this())
62 }
63}