PERF: correct clean up inactive so it does not clog scheduler

PERF: correct clean up inactive so it does not clog scheduler

also add a hard limit of 1000 users per job run so we do not clog the scheduler

destroyer.destroy has a transaction and this can have some serious complications with the open record set find_each has going

diff --git a/app/jobs/scheduled/clean_up_inactive_users.rb b/app/jobs/scheduled/clean_up_inactive_users.rb
index 99579db..d79562f 100644
--- a/app/jobs/scheduled/clean_up_inactive_users.rb
+++ b/app/jobs/scheduled/clean_up_inactive_users.rb
@@ -14,9 +14,10 @@ module Jobs
           "posts.user_id IS NULL AND users.last_seen_at < ?",
           SiteSetting.clean_up_inactive_users_after_days.days.ago
         )
-        .find_each do |user|
-
+        .limit(1000)
+        .pluck(:id).each do |id|
         begin
+          user = User.find(id)
           destroyer.destroy(user, context: I18n.t("user.destroy_reasons.inactive_user"))
         rescue => e
           Discourse.handle_job_exception(e,

GitHub sha: ec1c3559

1 Like