]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/schedulers/actor-follow-scheduler.ts
Move transcoding files in their own directory
[github/Chocobozzz/PeerTube.git] / server / lib / schedulers / actor-follow-scheduler.ts
CommitLineData
6fdc553a 1import { isTestInstance } from '../../helpers/core-utils'
60650c77
C
2import { logger } from '../../helpers/logger'
3import { ActorFollowModel } from '../../models/activitypub/actor-follow'
4import { AbstractScheduler } from './abstract-scheduler'
6b9c966f 5import { ACTOR_FOLLOW_SCORE, SCHEDULER_INTERVALS_MS } from '../../initializers/constants'
d74d29ad 6import { ActorFollowScoreCache } from '../files-cache'
60650c77 7
2f5c6b2f 8export class ActorFollowScheduler extends AbstractScheduler {
60650c77
C
9
10 private static instance: AbstractScheduler
11
2f5c6b2f 12 protected schedulerIntervalMs = SCHEDULER_INTERVALS_MS.actorFollowScores
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 () {
6b9c966f
C
25 const pendingScores = ActorFollowScoreCache.Instance.getPendingFollowsScore()
26 const badServerIds = ActorFollowScoreCache.Instance.getBadFollowingServerIds()
27 const goodServerIds = ActorFollowScoreCache.Instance.getGoodFollowingServerIds()
2f5c6b2f
C
28
29 ActorFollowScoreCache.Instance.clearPendingFollowsScore()
6b9c966f
C
30 ActorFollowScoreCache.Instance.clearBadFollowingServerIds()
31 ActorFollowScoreCache.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 () {
6fdc553a 42 if (!isTestInstance()) 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}