]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/schedulers/update-videos-scheduler.ts
Auto update youtube-dl
[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'
8
9export class UpdateVideosScheduler extends AbstractScheduler {
10
11 private static instance: AbstractScheduler
12
13 protected schedulerIntervalMs = SCHEDULER_INTERVALS_MS.updateVideos
14
15 private isRunning = false
16
17 private constructor () {
18 super()
19 }
20
21 async execute () {
22 if (this.isRunning === true) return
23 this.isRunning = true
24
25 try {
26 await retryTransactionWrapper(this.updateVideos.bind(this))
27 } catch (err) {
28 logger.error('Cannot execute update videos scheduler.', { err })
29 } finally {
30 this.isRunning = false
31 }
32 }
33
bbe0f064
C
34 private async updateVideos () {
35 if (!await ScheduleVideoUpdateModel.areVideosToUpdate()) return undefined
36
2baea0c7
C
37 return sequelizeTypescript.transaction(async t => {
38 const schedules = await ScheduleVideoUpdateModel.listVideosToUpdate(t)
39
40 for (const schedule of schedules) {
41 const video = schedule.Video
42 logger.info('Executing scheduled video update on %s.', video.uuid)
43
44 if (schedule.privacy) {
45 const oldPrivacy = video.privacy
77de223a 46 const isNewVideo = oldPrivacy === VideoPrivacy.PRIVATE
2baea0c7
C
47
48 video.privacy = schedule.privacy
77de223a 49 if (isNewVideo === true) video.publishedAt = new Date()
2baea0c7 50
77de223a 51 await video.save({ transaction: t })
2baea0c7
C
52 await federateVideoIfNeeded(video, isNewVideo, t)
53 }
54
55 await schedule.destroy({ transaction: t })
56 }
57 })
58 }
59
60 static get Instance () {
61 return this.instance || (this.instance = new this())
62 }
63}