actor-follow-scheduler.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import { isTestInstance } from '../../helpers/core-utils'
  2. import { logger } from '../../helpers/logger'
  3. import { ActorFollowModel } from '../../models/activitypub/actor-follow'
  4. import { AbstractScheduler } from './abstract-scheduler'
  5. import { SCHEDULER_INTERVALS_MS } from '../../initializers/constants'
  6. import { ActorFollowScoreCache } from '../files-cache'
  7. export class ActorFollowScheduler extends AbstractScheduler {
  8. private static instance: AbstractScheduler
  9. protected schedulerIntervalMs = SCHEDULER_INTERVALS_MS.actorFollowScores
  10. private constructor () {
  11. super()
  12. }
  13. protected async internalExecute () {
  14. await this.processPendingScores()
  15. await this.removeBadActorFollows()
  16. }
  17. private async processPendingScores () {
  18. const pendingScores = ActorFollowScoreCache.Instance.getPendingFollowsScoreCopy()
  19. ActorFollowScoreCache.Instance.clearPendingFollowsScore()
  20. for (const inbox of Object.keys(pendingScores)) {
  21. await ActorFollowModel.updateFollowScore(inbox, pendingScores[inbox])
  22. }
  23. }
  24. private async removeBadActorFollows () {
  25. if (!isTestInstance()) logger.info('Removing bad actor follows (scheduler).')
  26. try {
  27. await ActorFollowModel.removeBadActorFollows()
  28. } catch (err) {
  29. logger.error('Error in bad actor follows scheduler.', { err })
  30. }
  31. }
  32. static get Instance () {
  33. return this.instance || (this.instance = new this())
  34. }
  35. }