]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/schedulers/update-videos-scheduler.ts
Bumped to version v5.2.1
[github/Chocobozzz/PeerTube.git] / server / lib / schedulers / update-videos-scheduler.ts
CommitLineData
c6406f47 1import { VideoModel } from '@server/models/video/video'
3545e72c
C
2import { MScheduleVideoUpdate } from '@server/types/models'
3import { VideoPrivacy, VideoState } from '@shared/models'
2baea0c7 4import { logger } from '../../helpers/logger'
c6406f47
C
5import { SCHEDULER_INTERVALS_MS } from '../../initializers/constants'
6import { sequelizeTypescript } from '../../initializers/database'
2baea0c7 7import { ScheduleVideoUpdateModel } from '../../models/video/schedule-video-update'
cef534ed 8import { Notifier } from '../notifier'
3545e72c
C
9import { addVideoJobsAfterUpdate } from '../video'
10import { VideoPathManager } from '../video-path-manager'
11import { setVideoPrivacy } from '../video-privacy'
c6406f47 12import { AbstractScheduler } from './abstract-scheduler'
2baea0c7
C
13
14export class UpdateVideosScheduler extends AbstractScheduler {
15
16 private static instance: AbstractScheduler
17
61953742 18 protected schedulerIntervalMs = SCHEDULER_INTERVALS_MS.UPDATE_VIDEOS
2baea0c7 19
2baea0c7
C
20 private constructor () {
21 super()
22 }
23
2f5c6b2f 24 protected async internalExecute () {
c6406f47 25 return this.updateVideos()
2baea0c7
C
26 }
27
bbe0f064
C
28 private async updateVideos () {
29 if (!await ScheduleVideoUpdateModel.areVideosToUpdate()) return undefined
30
fd6a74a8 31 const schedules = await ScheduleVideoUpdateModel.listVideosToUpdate()
fd6a74a8
C
32
33 for (const schedule of schedules) {
3545e72c
C
34 const videoOnly = await VideoModel.load(schedule.videoId)
35 const mutexReleaser = await VideoPathManager.Instance.lockFiles(videoOnly.uuid)
2baea0c7 36
3545e72c
C
37 try {
38 const { video, published } = await this.updateAVideo(schedule)
2baea0c7 39
3545e72c
C
40 if (published) Notifier.Instance.notifyOnVideoPublishedAfterScheduledUpdate(video)
41 } catch (err) {
42 logger.error('Cannot update video', { err })
43 }
2baea0c7 44
3545e72c
C
45 mutexReleaser()
46 }
47 }
48
49 private async updateAVideo (schedule: MScheduleVideoUpdate) {
50 let oldPrivacy: VideoPrivacy
51 let isNewVideo: boolean
52 let published = false
53
54 const video = await sequelizeTypescript.transaction(async t => {
55 const video = await VideoModel.loadFull(schedule.videoId, t)
eb8da03d 56 if (video.state === VideoState.TO_TRANSCODE) return null
3545e72c
C
57
58 logger.info('Executing scheduled video update on %s.', video.uuid)
59
60 if (schedule.privacy) {
61 isNewVideo = video.isNewVideo(schedule.privacy)
62 oldPrivacy = video.privacy
cef534ed 63
3545e72c
C
64 setVideoPrivacy(video, schedule.privacy)
65 await video.save({ transaction: t })
66
67 if (oldPrivacy === VideoPrivacy.PRIVATE) {
68 published = true
2baea0c7 69 }
3545e72c 70 }
2baea0c7 71
3545e72c 72 await schedule.destroy({ transaction: t })
dc133480 73
3545e72c
C
74 return video
75 })
76
eb8da03d
C
77 if (!video) {
78 return { video, published: false }
79 }
80
3545e72c
C
81 await addVideoJobsAfterUpdate({ video, oldPrivacy, isNewVideo, nameChanged: false })
82
83 return { video, published }
2baea0c7
C
84 }
85
86 static get Instance () {
87 return this.instance || (this.instance = new this())
88 }
89}