self_destruct_scheduler.rb 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # frozen_string_literal: true
  2. class Scheduler::SelfDestructScheduler
  3. include Sidekiq::Worker
  4. include SelfDestructHelper
  5. MAX_ENQUEUED = 10_000
  6. MAX_REDIS_MEM_USAGE = 0.5
  7. MAX_ACCOUNT_DELETIONS_PER_JOB = 50
  8. sidekiq_options retry: 0, lock: :until_executed, lock_ttl: 1.day.to_i
  9. def perform
  10. return unless self_destruct?
  11. return if sidekiq_overwhelmed?
  12. delete_accounts!
  13. end
  14. private
  15. def sidekiq_overwhelmed?
  16. redis_mem_info = Sidekiq.redis_info
  17. Sidekiq::Stats.new.enqueued > MAX_ENQUEUED || redis_mem_info['used_memory'].to_f > redis_mem_info['total_system_memory'].to_f * MAX_REDIS_MEM_USAGE
  18. end
  19. def delete_accounts!
  20. # We currently do not distinguish between deleted accounts and suspended
  21. # accounts, and we do not want to remove the records in this scheduler, as
  22. # we still rely on it for account delivery and don't want to perform
  23. # needless work when the database can be outright dropped after the
  24. # self-destruct.
  25. # Deleted accounts are suspended accounts that do not have a pending
  26. # deletion request.
  27. # This targets accounts that have not been deleted nor marked for deletion yet
  28. Account.local.without_suspended.reorder(id: :asc).take(MAX_ACCOUNT_DELETIONS_PER_JOB).each do |account|
  29. delete_account!(account)
  30. end
  31. return if sidekiq_overwhelmed?
  32. # This targets accounts that have been marked for deletion but have not been
  33. # deleted yet
  34. Account.local.suspended.joins(:deletion_request).take(MAX_ACCOUNT_DELETIONS_PER_JOB).each do |account|
  35. delete_account!(account)
  36. account.deletion_request&.destroy
  37. end
  38. end
  39. def inboxes
  40. @inboxes ||= Account.inboxes
  41. end
  42. def delete_account!(account)
  43. payload = ActiveModelSerializers::SerializableResource.new(
  44. account,
  45. serializer: ActivityPub::DeleteActorSerializer,
  46. adapter: ActivityPub::Adapter
  47. ).as_json
  48. json = Oj.dump(ActivityPub::LinkedDataSignature.new(payload).sign!(account))
  49. ActivityPub::DeliveryWorker.push_bulk(inboxes, limit: 1_000) do |inbox_url|
  50. [json, account.id, inbox_url]
  51. end
  52. # Do not call `Account#suspend!` because we don't want to issue a deletion request
  53. account.update!(suspended_at: Time.now.utc, suspension_origin: :local)
  54. end
  55. end