about summary refs log tree commit diff
path: root/app
diff options
context:
space:
mode:
authormultiple creatures <dev@multiple-creature.party>2019-08-18 19:50:10 -0500
committermultiple creatures <dev@multiple-creature.party>2019-08-18 19:50:10 -0500
commit88fa00c35a6dd91711035f6ad1835ac83382164b (patch)
tree7d996c5c9cd5e196e37c12c3d4f0014801f05a3e /app
parent1c5ff0ec10071ed93c66447cfab4d73a2c43e2ce (diff)
split db pruning off into its own scheduled job
Diffstat (limited to 'app')
-rw-r--r--app/workers/scheduler/janitor_scheduler.rb39
-rw-r--r--app/workers/scheduler/prune_database_scheduler.rb43
2 files changed, 43 insertions, 39 deletions
diff --git a/app/workers/scheduler/janitor_scheduler.rb b/app/workers/scheduler/janitor_scheduler.rb
index 06139a82c..6a386b30a 100644
--- a/app/workers/scheduler/janitor_scheduler.rb
+++ b/app/workers/scheduler/janitor_scheduler.rb
@@ -25,7 +25,6 @@ class Scheduler::JanitorScheduler
     import_blocklists!
     export_suspensions!
     export_activityrelay_config!
-    prune_database! unless redis.exists('janitor:pune_database')
   end
 
   private
@@ -34,44 +33,6 @@ class Scheduler::JanitorScheduler
     Account.local.where.not(suspended_at: nil).destroy_all
   end
 
-  def prune_database!
-    suspended_accounts = Account.where.not(suspended_at: nil).select(:id)
-    suspended_domains = DomainBlock.suspend.select(:domain)
-
-    # remove statuses from suspended accounts missed by SuspendStatusService
-    # if its sidekiq job crashed
-    Status.where(account_id: suspended_accounts).in_batches do |status|
-      BatchedRemoveStatusService.new.call(status)
-    end
-
-    # prune leaves of threads that lost their context after a suspension
-    # keeping these around eats a pretty good amount of storage
-    deleted_mentions = Mention.where(account_id: suspended_accounts).select(:status_id)
-    Status.remote.where(account_id: deleted_mentions).in_batches do |status|
-      BatchedRemoveStatusService.new.call(status)
-    end
-
-    # remove mention entries that have no status or account attached to them
-    Mention.where(account_id: nil).in_batches.destroy_all
-    Mention.where(status_id: nil).in_batches.destroy_all
-
-    # remove media attachments that don't belong to any status
-    MediaAttachment.where(status_id: nil).in_batches.destroy_all
-
-    # remove custom emoji from suspended domains missed by SuspendAccountService
-    CustomEmoji.where(domain: suspended_domains).in_batches.destroy_all
-
-    # prune empty tags
-    Tag.all.find_each { |tag| tag.destroy unless tag.statuses.exists? }
-
-    # remove audit log entries with missing context
-    # we already use LOG_USER to avoid that problem
-    Admin::ActionLog.where.not(target_id: Account.select(:id)).in_batches.destroy_all
-    Admin::ActionLog.where.not(account_id: Account.local.select(:id)).in_batches.destroy_all
-
-    redis.setex('janitor:prune_database', 1.day, 1)
-  end
-
   def suspend_abandoned_accounts!
     reason = "Appears to be abandoned. Freeing up the username for someone else."
     abandoned_accounts.find_each do |account|
diff --git a/app/workers/scheduler/prune_database_scheduler.rb b/app/workers/scheduler/prune_database_scheduler.rb
new file mode 100644
index 000000000..0e1cb6798
--- /dev/null
+++ b/app/workers/scheduler/prune_database_scheduler.rb
@@ -0,0 +1,43 @@
+# frozen_string_literal: true
+
+class Scheduler::PruneDatabaseScheduler
+  include Sidekiq::Worker
+
+  sidekiq_options unique: :until_executed
+
+  def perform
+    suspended_accounts = Account.where.not(suspended_at: nil).select(:id)
+    suspended_domains = DomainBlock.suspend.select(:domain)
+
+    # remove statuses from suspended accounts missed by SuspendStatusService
+    # if its sidekiq job crashed
+    Status.where(account_id: suspended_accounts).in_batches do |status|
+      BatchedRemoveStatusService.new.call(status)
+    end
+
+    # prune leaves of threads that lost their context after a suspension
+    # keeping these around eats a pretty good amount of storage
+    deleted_mentions = Mention.where(account_id: suspended_accounts).select(:status_id)
+    Status.remote.where(account_id: deleted_mentions).in_batches do |status|
+      BatchedRemoveStatusService.new.call(status)
+    end
+
+    # remove mention entries that have no status or account attached to them
+    Mention.where(account_id: nil).in_batches.destroy_all
+    Mention.where(status_id: nil).in_batches.destroy_all
+
+    # remove media attachments that don't belong to any status
+    MediaAttachment.where(status_id: nil).in_batches.destroy_all
+
+    # remove custom emoji from suspended domains missed by SuspendAccountService
+    CustomEmoji.where(domain: suspended_domains).in_batches.destroy_all
+
+    # prune empty tags
+    Tag.all.find_each { |tag| tag.destroy unless tag.statuses.exists? }
+
+    # remove mainline audit log entries with missing context
+    # monsterfork's audit log doesn't have this problem cause we use plaintext
+    Admin::ActionLog.where.not(target_id: Account.select(:id)).in_batches.destroy_all
+    Admin::ActionLog.where.not(account_id: Account.local.select(:id)).in_batches.destroy_all
+  end
+end