]>
Commit | Line | Data |
---|---|---|
6fdc553a | 1 | import { isTestInstance } from '../../helpers/core-utils' |
60650c77 | 2 | import { logger } from '../../helpers/logger' |
6b9c966f | 3 | import { ACTOR_FOLLOW_SCORE, SCHEDULER_INTERVALS_MS } from '../../initializers/constants' |
7d9ba5c0 | 4 | import { ActorFollowModel } from '../../models/actor/actor-follow' |
d74d29ad | 5 | import { ActorFollowScoreCache } from '../files-cache' |
7d9ba5c0 | 6 | import { AbstractScheduler } from './abstract-scheduler' |
60650c77 | 7 | |
2f5c6b2f | 8 | export 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 | } |