]>
Commit | Line | Data |
---|---|---|
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, sequelizeTypescript } from '../../initializers' | |
7 | import { VideoPrivacy } from '../../../shared/models/videos' | |
8 | ||
9 | export 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 | ||
34 | private async updateVideos () { | |
35 | if (!await ScheduleVideoUpdateModel.areVideosToUpdate()) return undefined | |
36 | ||
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 | const isNewVideo = oldPrivacy === VideoPrivacy.PRIVATE | |
47 | ||
48 | video.privacy = schedule.privacy | |
49 | if (isNewVideo === true) video.publishedAt = new Date() | |
50 | ||
51 | await video.save({ transaction: t }) | |
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 | } |