aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib
diff options
context:
space:
mode:
Diffstat (limited to 'server/lib')
-rw-r--r--server/lib/schedulers/abstract-scheduler.ts8
-rw-r--r--server/lib/schedulers/bad-actor-follow-scheduler.ts3
-rw-r--r--server/lib/schedulers/remove-old-jobs-scheduler.ts3
-rw-r--r--server/lib/schedulers/update-videos-scheduler.ts62
4 files changed, 73 insertions, 3 deletions
diff --git a/server/lib/schedulers/abstract-scheduler.ts b/server/lib/schedulers/abstract-scheduler.ts
index 473544ddf..6ec5e3360 100644
--- a/server/lib/schedulers/abstract-scheduler.ts
+++ b/server/lib/schedulers/abstract-scheduler.ts
@@ -1,11 +1,13 @@
1import { SCHEDULER_INTERVAL } from '../../initializers'
2
3export abstract class AbstractScheduler { 1export abstract class AbstractScheduler {
4 2
3 protected abstract schedulerIntervalMs: number
4
5 private interval: NodeJS.Timer 5 private interval: NodeJS.Timer
6 6
7 enable () { 7 enable () {
8 this.interval = setInterval(() => this.execute(), SCHEDULER_INTERVAL) 8 if (!this.schedulerIntervalMs) throw new Error('Interval is not correctly set.')
9
10 this.interval = setInterval(() => this.execute(), this.schedulerIntervalMs)
9 } 11 }
10 12
11 disable () { 13 disable () {
diff --git a/server/lib/schedulers/bad-actor-follow-scheduler.ts b/server/lib/schedulers/bad-actor-follow-scheduler.ts
index 121f7145e..617149aaf 100644
--- a/server/lib/schedulers/bad-actor-follow-scheduler.ts
+++ b/server/lib/schedulers/bad-actor-follow-scheduler.ts
@@ -2,11 +2,14 @@ import { isTestInstance } from '../../helpers/core-utils'
2import { logger } from '../../helpers/logger' 2import { logger } from '../../helpers/logger'
3import { ActorFollowModel } from '../../models/activitypub/actor-follow' 3import { ActorFollowModel } from '../../models/activitypub/actor-follow'
4import { AbstractScheduler } from './abstract-scheduler' 4import { AbstractScheduler } from './abstract-scheduler'
5import { SCHEDULER_INTERVALS_MS } from '../../initializers'
5 6
6export class BadActorFollowScheduler extends AbstractScheduler { 7export class BadActorFollowScheduler extends AbstractScheduler {
7 8
8 private static instance: AbstractScheduler 9 private static instance: AbstractScheduler
9 10
11 protected schedulerIntervalMs = SCHEDULER_INTERVALS_MS.badActorFollow
12
10 private constructor () { 13 private constructor () {
11 super() 14 super()
12 } 15 }
diff --git a/server/lib/schedulers/remove-old-jobs-scheduler.ts b/server/lib/schedulers/remove-old-jobs-scheduler.ts
index 0e8ad1554..a29a6b800 100644
--- a/server/lib/schedulers/remove-old-jobs-scheduler.ts
+++ b/server/lib/schedulers/remove-old-jobs-scheduler.ts
@@ -2,11 +2,14 @@ import { isTestInstance } from '../../helpers/core-utils'
2import { logger } from '../../helpers/logger' 2import { logger } from '../../helpers/logger'
3import { JobQueue } from '../job-queue' 3import { JobQueue } from '../job-queue'
4import { AbstractScheduler } from './abstract-scheduler' 4import { AbstractScheduler } from './abstract-scheduler'
5import { SCHEDULER_INTERVALS_MS } from '../../initializers'
5 6
6export class RemoveOldJobsScheduler extends AbstractScheduler { 7export class RemoveOldJobsScheduler extends AbstractScheduler {
7 8
8 private static instance: AbstractScheduler 9 private static instance: AbstractScheduler
9 10
11 protected schedulerIntervalMs = SCHEDULER_INTERVALS_MS.removeOldJobs
12
10 private constructor () { 13 private constructor () {
11 super() 14 super()
12 } 15 }
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}