scheduler to delete old accounts

This commit is contained in:
Sebastian Jambor 2023-01-13 16:41:35 +01:00
parent a3b8fa244a
commit d8962d2468
2 changed files with 44 additions and 0 deletions

View file

@ -0,0 +1,40 @@
# frozen_string_literal: true
class Scheduler::OldAccountCleanupScheduler
include Sidekiq::Worker
# Each processed deletion request may enqueue an enormous
# amount of jobs in the `pull` queue, so only enqueue when
# the queue is empty or close to being so.
MAX_PULL_SIZE = 50
# Since account deletion is very expensive, we want to avoid
# overloading the server by queing too much at once.
MAX_DELETIONS_PER_JOB = 5
sidekiq_options retry: 0
def perform
return if Sidekiq::Queue.new('pull').size > MAX_PULL_SIZE
clean_old_accounts!
end
private
def clean_old_accounts!
Account
# only fetch local accounts
.where("domain IS NULL")
# id -99 is the instance actor
.where("id <> -99")
# don't delete admin
.where("username <> 'admin'")
.where("created_at < ?", 1.day.ago)
.order(created_at: :asc)
.limit(MAX_DELETIONS_PER_JOB)
.each do |account|
AccountDeletionWorker.perform_async(account.id, { :reserve_username => false })
end
end
end

View file

@ -58,3 +58,7 @@
interval: 1 minute
class: Scheduler::SuspendedUserCleanupScheduler
queue: scheduler
old_account_cleanup_scheduler:
interval: 1 minute
class: Scheduler::OldAccountCleanupScheduler
queue: scheduler