user_cleanup_scheduler.rb 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. # frozen_string_literal: true
  2. class Scheduler::UserCleanupScheduler
  3. include Sidekiq::Worker
  4. UNCONFIRMED_ACCOUNTS_MAX_AGE_DAYS = 7
  5. DISCARDED_STATUSES_MAX_AGE_DAYS = 30
  6. sidekiq_options retry: 0, lock: :until_executed, lock_ttl: 1.day.to_i
  7. def perform
  8. clean_unconfirmed_accounts!
  9. clean_discarded_statuses!
  10. end
  11. private
  12. def clean_unconfirmed_accounts!
  13. User.unconfirmed.where(confirmation_sent_at: ..UNCONFIRMED_ACCOUNTS_MAX_AGE_DAYS.days.ago).find_in_batches do |batch|
  14. # We have to do it separately because of missing database constraints
  15. AccountModerationNote.where(target_account_id: batch.map(&:account_id)).delete_all
  16. Account.where(id: batch.map(&:account_id)).delete_all
  17. User.where(id: batch.map(&:id)).delete_all
  18. end
  19. end
  20. def clean_discarded_statuses!
  21. Status.unscoped.discarded.where(deleted_at: ..DISCARDED_STATUSES_MAX_AGE_DAYS.days.ago).find_in_batches do |statuses|
  22. RemovalWorker.push_bulk(statuses) do |status|
  23. [status.id, { 'immediate' => true, 'skip_streaming' => true }]
  24. end
  25. end
  26. end
  27. end