From d8962d2468c3d7a4685eb56b9bd5dfe94b6e0372 Mon Sep 17 00:00:00 2001 From: Sebastian Jambor Date: Fri, 13 Jan 2023 16:41:35 +0100 Subject: [PATCH] scheduler to delete old accounts --- .../old_account_cleanup_scheduler.rb | 40 +++++++++++++++++++ config/sidekiq.yml | 4 ++ 2 files changed, 44 insertions(+) create mode 100644 app/workers/scheduler/old_account_cleanup_scheduler.rb diff --git a/app/workers/scheduler/old_account_cleanup_scheduler.rb b/app/workers/scheduler/old_account_cleanup_scheduler.rb new file mode 100644 index 000000000..2a2027dd9 --- /dev/null +++ b/app/workers/scheduler/old_account_cleanup_scheduler.rb @@ -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 diff --git a/config/sidekiq.yml b/config/sidekiq.yml index b8739aab3..6d4bf04ff 100644 --- a/config/sidekiq.yml +++ b/config/sidekiq.yml @@ -58,3 +58,7 @@ interval: 1 minute class: Scheduler::SuspendedUserCleanupScheduler queue: scheduler + old_account_cleanup_scheduler: + interval: 1 minute + class: Scheduler::OldAccountCleanupScheduler + queue: scheduler