suspended_user_cleanup_scheduler.rb 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. # frozen_string_literal: true
  2. class Scheduler::SuspendedUserCleanupScheduler
  3. include Sidekiq::Worker
  4. # Each processed deletion request may enqueue an enormous
  5. # amount of jobs in the `pull` queue, so only enqueue when
  6. # the queue is empty or close to being so.
  7. MAX_PULL_SIZE = 50
  8. # Since account deletion is very expensive, we want to avoid
  9. # overloading the server by queuing too much at once.
  10. # This job runs approximately once per 2 minutes, so with a
  11. # value of `MAX_DELETIONS_PER_JOB` of 10, a server can
  12. # handle the deletion of 7200 accounts per day, provided it
  13. # has the capacity for it.
  14. MAX_DELETIONS_PER_JOB = 10
  15. sidekiq_options retry: 0, lock: :until_executed, lock_ttl: 1.day.to_i
  16. def perform
  17. return if Sidekiq::Queue.new('pull').size > MAX_PULL_SIZE
  18. process_deletion_requests!
  19. end
  20. private
  21. def process_deletion_requests!
  22. # This should be fine because we only process a small amount of deletion requests at once and
  23. # `id` and `created_at` should follow the same order.
  24. AccountDeletionRequest.reorder(id: :asc).take(MAX_DELETIONS_PER_JOB).each do |deletion_request|
  25. next unless deletion_request.created_at < AccountDeletionRequest::DELAY_TO_DELETION.ago
  26. Admin::AccountDeletionWorker.perform_async(deletion_request.account_id)
  27. end
  28. end
  29. end