]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/schedulers/update-videos-scheduler.ts
Refactor schedule update
[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'
8dc8a34e 5import { federateVideoIfNeeded } from '../activitypub/videos'
74dc3bca 6import { SCHEDULER_INTERVALS_MS } from '../../initializers/constants'
cef534ed 7import { Notifier } from '../notifier'
74dc3bca 8import { sequelizeTypescript } from '../../initializers/database'
26d6bf65 9import { MVideoFullLight } from '@server/types/models'
fd6a74a8 10import { VideoModel } from '@server/models/video/video'
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
fd6a74a8
C
29 const schedules = await ScheduleVideoUpdateModel.listVideosToUpdate()
30 const publishedVideos: MVideoFullLight[] = []
31
32 for (const schedule of schedules) {
33 await sequelizeTypescript.transaction(async t => {
34 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(schedule.videoId, t)
2baea0c7 35
2baea0c7
C
36 logger.info('Executing scheduled video update on %s.', video.uuid)
37
38 if (schedule.privacy) {
22a73cb8
C
39 const wasConfidentialVideo = video.isConfidential()
40 const isNewVideo = video.isNewVideo(schedule.privacy)
2baea0c7 41
22a73cb8 42 video.setPrivacy(schedule.privacy)
77de223a 43 await video.save({ transaction: t })
2baea0c7 44 await federateVideoIfNeeded(video, isNewVideo, t)
cef534ed 45
22a73cb8 46 if (wasConfidentialVideo) {
fd6a74a8 47 publishedVideos.push(video)
cef534ed 48 }
2baea0c7
C
49 }
50
51 await schedule.destroy({ transaction: t })
fd6a74a8
C
52 })
53 }
dc133480
C
54
55 for (const v of publishedVideos) {
5b77537c 56 Notifier.Instance.notifyOnNewVideoIfNeeded(v)
7ccddd7b 57 Notifier.Instance.notifyOnVideoPublishedAfterScheduledUpdate(v)
dc133480 58 }
2baea0c7
C
59 }
60
61 static get Instance () {
62 return this.instance || (this.instance = new this())
63 }
64}