about summary refs log tree commit diff
path: root/app
diff options
context:
space:
mode:
Diffstat (limited to 'app')
-rw-r--r--app/workers/maintenance/uncache_preview_worker.rb18
-rw-r--r--app/workers/scheduler/preview_cards_cleanup_scheduler.rb22
2 files changed, 40 insertions, 0 deletions
diff --git a/app/workers/maintenance/uncache_preview_worker.rb b/app/workers/maintenance/uncache_preview_worker.rb
new file mode 100644
index 000000000..810ffd8cc
--- /dev/null
+++ b/app/workers/maintenance/uncache_preview_worker.rb
@@ -0,0 +1,18 @@
+# frozen_string_literal: true
+
+class Maintenance::UncachePreviewWorker
+  include Sidekiq::Worker
+
+  sidekiq_options queue: 'pull'
+
+  def perform(preview_card_id)
+    preview_card = PreviewCard.find(preview_card_id)
+
+    return if preview_card.image.blank?
+
+    preview_card.image.destroy
+    preview_card.save
+  rescue ActiveRecord::RecordNotFound
+    true
+  end
+end
diff --git a/app/workers/scheduler/preview_cards_cleanup_scheduler.rb b/app/workers/scheduler/preview_cards_cleanup_scheduler.rb
new file mode 100644
index 000000000..2b38792f0
--- /dev/null
+++ b/app/workers/scheduler/preview_cards_cleanup_scheduler.rb
@@ -0,0 +1,22 @@
+# frozen_string_literal: true
+
+class Scheduler::PreviewCardsCleanupScheduler
+  include Sidekiq::Worker
+
+  sidekiq_options unique: :until_executed, retry: 0
+
+  def perform
+    Maintenance::UncachePreviewWorker.push_bulk(recent_link_preview_cards.pluck(:id))
+    Maintenance::UncachePreviewWorker.push_bulk(older_preview_cards.pluck(:id))
+  end
+
+  private
+
+  def recent_link_preview_cards
+    PreviewCard.where(type: :link).where('updated_at < ?', 1.month.ago)
+  end
+
+  def older_preview_cards
+    PreviewCard.where('updated_at < ?', 6.months.ago)
+  end
+end