Skip to content

Commit

Permalink
Only clean up pastes which exist
Browse files Browse the repository at this point in the history
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 <mail@georg-pfuetzenreuter.net>
  • Loading branch information
tacerus committed Sep 1, 2024
1 parent 08b2cb8 commit 798bdb8
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
4 changes: 2 additions & 2 deletions app/jobs/pastes_cleanup_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion app/models/paste.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 798bdb8

Please sign in to comment.