From 798bdb83a1511a86f071d792c81630b4c19f797d Mon Sep 17 00:00:00 2001 From: Georg Pfuetzenreuter Date: Sun, 1 Sep 2024 12:38:28 +0200 Subject: [PATCH] Only clean up pastes which exist If pastes would be deleted administratively before the scheduled expiry cleanup took place, the cleanup job would, once the schedule hit, return "Error performing PastesCleanupJob" along with a long traceback about the failure. Handle the situation gracefully and avoid the failure by validating whether the paste exists before attempting to delete it. To achieve this, the paste ID instead of the paste object must be passed to the job, as otherwise the object in the queue might no longer be pointing to any valid paste, not allowing for validation of it inside the function. Signed-off-by: Georg Pfuetzenreuter --- app/jobs/pastes_cleanup_job.rb | 4 ++-- app/models/paste.rb | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/jobs/pastes_cleanup_job.rb b/app/jobs/pastes_cleanup_job.rb index dd9780f..575dcfb 100644 --- a/app/jobs/pastes_cleanup_job.rb +++ b/app/jobs/pastes_cleanup_job.rb @@ -4,7 +4,7 @@ class PastesCleanupJob < ApplicationJob queue_as :default - def perform(paste) - paste.destroy + def perform(paste_id) + Paste.find_by(id: paste_id)&.destroy end end diff --git a/app/models/paste.rb b/app/models/paste.rb index 584d8e7..b0960e3 100644 --- a/app/models/paste.rb +++ b/app/models/paste.rb @@ -83,6 +83,6 @@ def create_permalink end def enqueue_removal - PastesCleanupJob.set(wait_until: remove_at).perform_later(self) + PastesCleanupJob.set(wait_until: remove_at).perform_later(self&.id) end end