]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/schedulers/update-videos-scheduler.ts
Reduce AP context size on specific activities
[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'
cef534ed 7import { Notifier } from '../notifier'
74dc3bca 8import { sequelizeTypescript } from '../../initializers/database'
d7a25329 9import { MVideoFullLight } from '@server/typings/models'
2baea0c7
C
10
11export class UpdateVideosScheduler extends AbstractScheduler {
12
13 private static instance: AbstractScheduler
14
15 protected schedulerIntervalMs = SCHEDULER_INTERVALS_MS.updateVideos
16
2baea0c7
C
17 private constructor () {
18 super()
19 }
20
2f5c6b2f
C
21 protected async internalExecute () {
22 return retryTransactionWrapper(this.updateVideos.bind(this))
2baea0c7
C
23 }
24
bbe0f064
C
25 private async updateVideos () {
26 if (!await ScheduleVideoUpdateModel.areVideosToUpdate()) return undefined
27
dc133480 28 const publishedVideos = await sequelizeTypescript.transaction(async t => {
2baea0c7 29 const schedules = await ScheduleVideoUpdateModel.listVideosToUpdate(t)
d7a25329 30 const publishedVideos: MVideoFullLight[] = []
2baea0c7
C
31
32 for (const schedule of schedules) {
33 const video = schedule.Video
34 logger.info('Executing scheduled video update on %s.', video.uuid)
35
36 if (schedule.privacy) {
22a73cb8
C
37 const wasConfidentialVideo = video.isConfidential()
38 const isNewVideo = video.isNewVideo(schedule.privacy)
2baea0c7 39
22a73cb8 40 video.setPrivacy(schedule.privacy)
77de223a 41 await video.save({ transaction: t })
2baea0c7 42 await federateVideoIfNeeded(video, isNewVideo, t)
cef534ed 43
22a73cb8 44 if (wasConfidentialVideo) {
d7a25329
C
45 const videoToPublish: MVideoFullLight = Object.assign(video, { ScheduleVideoUpdate: schedule, UserVideoHistories: [] })
46 publishedVideos.push(videoToPublish)
cef534ed 47 }
2baea0c7
C
48 }
49
50 await schedule.destroy({ transaction: t })
51 }
dc133480
C
52
53 return publishedVideos
2baea0c7 54 })
dc133480
C
55
56 for (const v of publishedVideos) {
5b77537c 57 Notifier.Instance.notifyOnNewVideoIfNeeded(v)
7ccddd7b 58 Notifier.Instance.notifyOnVideoPublishedAfterScheduledUpdate(v)
dc133480 59 }
2baea0c7
C
60 }
61
62 static get Instance () {
63 return this.instance || (this.instance = new this())
64 }
65}