You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
3.4 KiB

  1. # frozen_string_literal: true
  2. class Scheduler::AccountsStatusesCleanupScheduler
  3. include Sidekiq::Worker
  4. # This limit is mostly to be nice to the fediverse at large and not
  5. # generate too much traffic.
  6. # This also helps limiting the running time of the scheduler itself.
  7. MAX_BUDGET = 50
  8. # This is an attempt to spread the load across instances, as various
  9. # accounts are likely to have various followers.
  10. PER_ACCOUNT_BUDGET = 5
  11. # This is an attempt to limit the workload generated by status removal
  12. # jobs to something the particular instance can handle.
  13. PER_THREAD_BUDGET = 5
  14. # Those avoid loading an instance that is already under load
  15. MAX_DEFAULT_SIZE = 2
  16. MAX_DEFAULT_LATENCY = 5
  17. MAX_PUSH_SIZE = 5
  18. MAX_PUSH_LATENCY = 10
  19. # 'pull' queue has lower priority jobs, and it's unlikely that pushing
  20. # deletes would cause much issues with this queue if it didn't cause issues
  21. # with default and push. Yet, do not enqueue deletes if the instance is
  22. # lagging behind too much.
  23. MAX_PULL_SIZE = 500
  24. MAX_PULL_LATENCY = 300
  25. # This is less of an issue in general, but deleting old statuses is likely
  26. # to cause delivery errors, and thus increase the number of jobs to be retried.
  27. # This doesn't directly translate to load, but connection errors and a high
  28. # number of dead instances may lead to this spiraling out of control if
  29. # unchecked.
  30. MAX_RETRY_SIZE = 50_000
  31. sidekiq_options retry: 0, lock: :until_executed
  32. def perform
  33. return if under_load?
  34. budget = compute_budget
  35. first_policy_id = last_processed_id
  36. loop do
  37. num_processed_accounts = 0
  38. scope = AccountStatusesCleanupPolicy.where(enabled: true)
  39. scope.where(Account.arel_table[:id].gt(first_policy_id)) if first_policy_id.present?
  40. scope.find_each(order: :asc) do |policy|
  41. num_deleted = AccountStatusesCleanupService.new.call(policy, [budget, PER_ACCOUNT_BUDGET].min)
  42. num_processed_accounts += 1 unless num_deleted.zero?
  43. budget -= num_deleted
  44. if budget.zero?
  45. save_last_processed_id(policy.id)
  46. break
  47. end
  48. end
  49. # The idea here is to loop through all policies at least once until the budget is exhausted
  50. # and start back after the last processed account otherwise
  51. break if budget.zero? || (num_processed_accounts.zero? && first_policy_id.nil?)
  52. first_policy_id = nil
  53. end
  54. end
  55. def compute_budget
  56. threads = Sidekiq::ProcessSet.new.select { |x| x['queues'].include?('push') }.map { |x| x['concurrency'] }.sum
  57. [PER_THREAD_BUDGET * threads, MAX_BUDGET].min
  58. end
  59. def under_load?
  60. return true if Sidekiq::Stats.new.retry_size > MAX_RETRY_SIZE
  61. queue_under_load?('default', MAX_DEFAULT_SIZE, MAX_DEFAULT_LATENCY) || queue_under_load?('push', MAX_PUSH_SIZE, MAX_PUSH_LATENCY) || queue_under_load?('pull', MAX_PULL_SIZE, MAX_PULL_LATENCY)
  62. end
  63. private
  64. def queue_under_load?(name, max_size, max_latency)
  65. queue = Sidekiq::Queue.new(name)
  66. queue.size > max_size || queue.latency > max_latency
  67. end
  68. def last_processed_id
  69. Redis.current.get('account_statuses_cleanup_scheduler:last_account_id')
  70. end
  71. def save_last_processed_id(id)
  72. if id.nil?
  73. Redis.current.del('account_statuses_cleanup_scheduler:last_account_id')
  74. else
  75. Redis.current.set('account_statuses_cleanup_scheduler:last_account_id', id, ex: 1.hour.seconds)
  76. end
  77. end
  78. end