]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/schedulers/update-videos-scheduler.ts
Add ability to disable webtorrent
[github/Chocobozzz/PeerTube.git] / server / lib / schedulers / update-videos-scheduler.ts
1 import { logger } from '../../helpers/logger'
2 import { AbstractScheduler } from './abstract-scheduler'
3 import { ScheduleVideoUpdateModel } from '../../models/video/schedule-video-update'
4 import { retryTransactionWrapper } from '../../helpers/database-utils'
5 import { federateVideoIfNeeded } from '../activitypub'
6 import { SCHEDULER_INTERVALS_MS } from '../../initializers/constants'
7 import { VideoPrivacy } from '../../../shared/models/videos'
8 import { Notifier } from '../notifier'
9 import { sequelizeTypescript } from '../../initializers/database'
10 import { MVideoFullLight } from '@server/typings/models'
11
12 export class UpdateVideosScheduler extends AbstractScheduler {
13
14 private static instance: AbstractScheduler
15
16 protected schedulerIntervalMs = SCHEDULER_INTERVALS_MS.updateVideos
17
18 private constructor () {
19 super()
20 }
21
22 protected async internalExecute () {
23 return retryTransactionWrapper(this.updateVideos.bind(this))
24 }
25
26 private async updateVideos () {
27 if (!await ScheduleVideoUpdateModel.areVideosToUpdate()) return undefined
28
29 const publishedVideos = await sequelizeTypescript.transaction(async t => {
30 const schedules = await ScheduleVideoUpdateModel.listVideosToUpdate(t)
31 const publishedVideos: MVideoFullLight[] = []
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
39 const isNewVideo = oldPrivacy === VideoPrivacy.PRIVATE
40
41 video.privacy = schedule.privacy
42 if (isNewVideo === true) video.publishedAt = new Date()
43
44 await video.save({ transaction: t })
45 await federateVideoIfNeeded(video, isNewVideo, t)
46
47 if (oldPrivacy === VideoPrivacy.UNLISTED || oldPrivacy === VideoPrivacy.PRIVATE) {
48 const videoToPublish: MVideoFullLight = Object.assign(video, { ScheduleVideoUpdate: schedule, UserVideoHistories: [] })
49 publishedVideos.push(videoToPublish)
50 }
51 }
52
53 await schedule.destroy({ transaction: t })
54 }
55
56 return publishedVideos
57 })
58
59 for (const v of publishedVideos) {
60 Notifier.Instance.notifyOnNewVideoIfNeeded(v)
61 Notifier.Instance.notifyOnVideoPublishedAfterScheduledUpdate(v)
62 }
63 }
64
65 static get Instance () {
66 return this.instance || (this.instance = new this())
67 }
68 }