about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEugen Rochko <eugen@zeonfederated.com>2017-05-20 19:42:58 +0200
committerGitHub <noreply@github.com>2017-05-20 19:42:58 +0200
commitef900789bc99d3bb3cad3b664c7dd5b9bcd1a20c (patch)
tree5ad8886d984cf66c5c8f3bfa9c3bf2fea74a0f50
parentd78f5552547b5175770dbfbefeff73e426da980d (diff)
Replace mastodon:media:clear and mastodon:feeds:clear rake tasks with (#3180)
sidekiq-scheduler jobs

Resolves #2495
-rw-r--r--app/workers/scheduler/feed_cleanup_scheduler.rb26
-rw-r--r--app/workers/scheduler/media_cleanup_scheduler.rb17
-rw-r--r--config/sidekiq.yml6
-rw-r--r--lib/tasks/mastodon.rake8
4 files changed, 53 insertions, 4 deletions
diff --git a/app/workers/scheduler/feed_cleanup_scheduler.rb b/app/workers/scheduler/feed_cleanup_scheduler.rb
new file mode 100644
index 000000000..c7df363a4
--- /dev/null
+++ b/app/workers/scheduler/feed_cleanup_scheduler.rb
@@ -0,0 +1,26 @@
+# frozen_string_literal: true
+require 'sidekiq-scheduler'
+
+class Scheduler::FeedCleanupScheduler
+  include Sidekiq::Worker
+
+  def perform
+    logger.info 'Cleaning out home feeds of inactive users'
+
+    redis.pipelined do
+      inactive_users.pluck(:account_id).each do |account_id|
+        redis.del(FeedManager.instance.key(:home, account_id))
+      end
+    end
+  end
+
+  private
+
+  def inactive_users
+    User.confirmed.where('current_sign_in_at < ?', 14.days.ago)
+  end
+
+  def redis
+    Redis.current
+  end
+end
diff --git a/app/workers/scheduler/media_cleanup_scheduler.rb b/app/workers/scheduler/media_cleanup_scheduler.rb
new file mode 100644
index 000000000..885ee9afc
--- /dev/null
+++ b/app/workers/scheduler/media_cleanup_scheduler.rb
@@ -0,0 +1,17 @@
+# frozen_string_literal: true
+require 'sidekiq-scheduler'
+
+class Scheduler::MediaCleanupScheduler
+  include Sidekiq::Worker
+
+  def perform
+    logger.info 'Cleaning out unattached media attachments'
+    unattached_media.find_each(&:destroy)
+  end
+
+  private
+
+  def unattached_media
+    MediaAttachment.reorder(nil).where(status_id: nil).where('created_at < ?', 1.day.ago)
+  end
+end
diff --git a/config/sidekiq.yml b/config/sidekiq.yml
index 5c700cb27..6ed0aa4b5 100644
--- a/config/sidekiq.yml
+++ b/config/sidekiq.yml
@@ -9,3 +9,9 @@
   subscriptions_scheduler:
     cron: '0 5 * * *'
     class: Scheduler::SubscriptionsScheduler
+  media_cleanup_scheduler:
+    cron: '5 4 * * *'
+    class: Scheduler::MediaCleanupScheduler
+  feed_cleanup_scheduler:
+    cron: '0 0 * * *'
+    class: Scheduler::FeedCleanupScheduler
diff --git a/lib/tasks/mastodon.rake b/lib/tasks/mastodon.rake
index b71c578a6..07f0b2f48 100644
--- a/lib/tasks/mastodon.rake
+++ b/lib/tasks/mastodon.rake
@@ -45,7 +45,8 @@ namespace :mastodon do
   namespace :media do
     desc 'Removes media attachments that have not been assigned to any status for longer than a day'
     task clear: :environment do
-      MediaAttachment.where(status_id: nil).where('created_at < ?', 1.day.ago).find_each(&:destroy)
+      # No-op
+      # This task is now executed via sidekiq-scheduler
     end
 
     desc 'Remove media attachments attributed to silenced accounts'
@@ -89,9 +90,8 @@ namespace :mastodon do
   namespace :feeds do
     desc 'Clear timelines of inactive users'
     task clear: :environment do
-      User.confirmed.where('current_sign_in_at < ?', 14.days.ago).find_each do |user|
-        Redis.current.del(FeedManager.instance.key(:home, user.account_id))
-      end
+      # No-op
+      # This task is now executed via sidekiq-scheduler
     end
 
     desc 'Clears all timelines'