diff options
Diffstat (limited to 'server/lib/schedulers/update-videos-scheduler.ts')
-rw-r--r-- | server/lib/schedulers/update-videos-scheduler.ts | 62 |
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 @@ | |||
1 | import { isTestInstance } from '../../helpers/core-utils' | ||
2 | import { logger } from '../../helpers/logger' | ||
3 | import { JobQueue } from '../job-queue' | ||
4 | import { AbstractScheduler } from './abstract-scheduler' | ||
5 | import { ScheduleVideoUpdateModel } from '../../models/video/schedule-video-update' | ||
6 | import { retryTransactionWrapper } from '../../helpers/database-utils' | ||
7 | import { federateVideoIfNeeded } from '../activitypub' | ||
8 | import { SCHEDULER_INTERVALS_MS, sequelizeTypescript } from '../../initializers' | ||
9 | import { VideoPrivacy } from '../../../shared/models/videos' | ||
10 | |||
11 | export 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 | } | ||