]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/schedulers/actor-follow-scheduler.ts
/!\ Use a dedicated config file for development
[github/Chocobozzz/PeerTube.git] / server / lib / schedulers / actor-follow-scheduler.ts
CommitLineData
9452d4fd 1import { isTestOrDevInstance } from '../../helpers/core-utils'
60650c77 2import { logger } from '../../helpers/logger'
6b9c966f 3import { ACTOR_FOLLOW_SCORE, SCHEDULER_INTERVALS_MS } from '../../initializers/constants'
7d9ba5c0 4import { ActorFollowModel } from '../../models/actor/actor-follow'
9db437c8 5import { ActorFollowHealthCache } from '../actor-follow-health-cache'
7d9ba5c0 6import { AbstractScheduler } from './abstract-scheduler'
60650c77 7
2f5c6b2f 8export class ActorFollowScheduler extends AbstractScheduler {
60650c77
C
9
10 private static instance: AbstractScheduler
11
61953742 12 protected schedulerIntervalMs = SCHEDULER_INTERVALS_MS.ACTOR_FOLLOW_SCORES
2baea0c7 13
60650c77
C
14 private constructor () {
15 super()
16 }
17
2f5c6b2f
C
18 protected async internalExecute () {
19 await this.processPendingScores()
20
21 await this.removeBadActorFollows()
22 }
23
24 private async processPendingScores () {
9db437c8
C
25 const pendingScores = ActorFollowHealthCache.Instance.getPendingFollowsScore()
26 const badServerIds = ActorFollowHealthCache.Instance.getBadFollowingServerIds()
27 const goodServerIds = ActorFollowHealthCache.Instance.getGoodFollowingServerIds()
2f5c6b2f 28
9db437c8
C
29 ActorFollowHealthCache.Instance.clearPendingFollowsScore()
30 ActorFollowHealthCache.Instance.clearBadFollowingServerIds()
31 ActorFollowHealthCache.Instance.clearGoodFollowingServerIds()
2f5c6b2f
C
32
33 for (const inbox of Object.keys(pendingScores)) {
6b9c966f 34 await ActorFollowModel.updateScore(inbox, pendingScores[inbox])
2f5c6b2f 35 }
6b9c966f
C
36
37 await ActorFollowModel.updateScoreByFollowingServers(badServerIds, ACTOR_FOLLOW_SCORE.PENALTY)
38 await ActorFollowModel.updateScoreByFollowingServers(goodServerIds, ACTOR_FOLLOW_SCORE.BONUS)
2f5c6b2f
C
39 }
40
41 private async removeBadActorFollows () {
9452d4fd 42 if (!isTestOrDevInstance()) logger.info('Removing bad actor follows (scheduler).')
ea99d15f 43
60650c77
C
44 try {
45 await ActorFollowModel.removeBadActorFollows()
46 } catch (err) {
d5b7d911 47 logger.error('Error in bad actor follows scheduler.', { err })
60650c77
C
48 }
49 }
50
51 static get Instance () {
52 return this.instance || (this.instance = new this())
53 }
54}