]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/schedulers/update-videos-scheduler.ts
Add import finished and video published notifs
[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'
6import { SCHEDULER_INTERVALS_MS, sequelizeTypescript } from '../../initializers'
7import { VideoPrivacy } from '../../../shared/models/videos'
cef534ed 8import { Notifier } from '../notifier'
dc133480 9import { VideoModel } from '../../models/video/video'
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)
dc133480 30 const publishedVideos: VideoModel[] = []
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) {
37 const oldPrivacy = video.privacy
77de223a 38 const isNewVideo = oldPrivacy === VideoPrivacy.PRIVATE
2baea0c7
C
39
40 video.privacy = schedule.privacy
77de223a 41 if (isNewVideo === true) video.publishedAt = new Date()
2baea0c7 42
77de223a 43 await video.save({ transaction: t })
2baea0c7 44 await federateVideoIfNeeded(video, isNewVideo, t)
cef534ed
C
45
46 if (oldPrivacy === VideoPrivacy.UNLISTED || oldPrivacy === VideoPrivacy.PRIVATE) {
dc133480
C
47 video.ScheduleVideoUpdate = schedule
48 publishedVideos.push(video)
cef534ed 49 }
2baea0c7
C
50 }
51
52 await schedule.destroy({ transaction: t })
53 }
dc133480
C
54
55 return publishedVideos
2baea0c7 56 })
dc133480
C
57
58 for (const v of publishedVideos) {
59 Notifier.Instance.notifyOnNewVideo(v)
60 Notifier.Instance.notifyOnPendingVideoPublished(v)
61 }
2baea0c7
C
62 }
63
64 static get Instance () {
65 return this.instance || (this.instance = new this())
66 }
67}