aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/schedulers/update-videos-scheduler.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/lib/schedulers/update-videos-scheduler.ts')
-rw-r--r--server/lib/schedulers/update-videos-scheduler.ts62
1 files changed, 62 insertions, 0 deletions
diff --git a/server/lib/schedulers/update-videos-scheduler.ts b/server/lib/schedulers/update-videos-scheduler.ts
new file mode 100644
index 000000000..d123c3ceb
--- /dev/null
+++ b/server/lib/schedulers/update-videos-scheduler.ts
@@ -0,0 +1,62 @@
1import { isTestInstance } from '../../helpers/core-utils'
2import { logger } from '../../helpers/logger'
3import { JobQueue } from '../job-queue'
4import { AbstractScheduler } from './abstract-scheduler'
5import { ScheduleVideoUpdateModel } from '../../models/video/schedule-video-update'
6import { retryTransactionWrapper } from '../../helpers/database-utils'
7import { federateVideoIfNeeded } from '../activitypub'
8import { SCHEDULER_INTERVALS_MS, sequelizeTypescript } from '../../initializers'
9import { VideoPrivacy } from '../../../shared/models/videos'
10
11export class UpdateVideosScheduler extends AbstractScheduler {
12
13 private static instance: AbstractScheduler
14
15 protected schedulerIntervalMs = SCHEDULER_INTERVALS_MS.updateVideos
16
17 private isRunning = false
18
19 private constructor () {
20 super()
21 }
22
23 async execute () {
24 if (this.isRunning === true) return
25 this.isRunning = true
26
27 try {
28 await retryTransactionWrapper(this.updateVideos.bind(this))
29 } catch (err) {
30 logger.error('Cannot execute update videos scheduler.', { err })
31 } finally {
32 this.isRunning = false
33 }
34 }
35
36 private updateVideos () {
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
46
47 video.privacy = schedule.privacy
48 await video.save({ transaction: t })
49
50 const isNewVideo = oldPrivacy === VideoPrivacy.PRIVATE
51 await federateVideoIfNeeded(video, isNewVideo, t)
52 }
53
54 await schedule.destroy({ transaction: t })
55 }
56 })
57 }
58
59 static get Instance () {
60 return this.instance || (this.instance = new this())
61 }
62}