From 646db76e095be60e1cd20bf5aa8318854c057337 Mon Sep 17 00:00:00 2001 From: Oleg Valter Date: Mon, 16 Dec 2024 17:08:00 +0300 Subject: [PATCH] added CleanupVotes daily job for removing orphaned votes --- app/jobs/cleanup_votes_job.rb | 17 +++++++++++++++++ config/schedule.rb | 4 ++++ scripts/cleanup_votes.rb | 1 + 3 files changed, 22 insertions(+) create mode 100644 app/jobs/cleanup_votes_job.rb create mode 100644 scripts/cleanup_votes.rb diff --git a/app/jobs/cleanup_votes_job.rb b/app/jobs/cleanup_votes_job.rb new file mode 100644 index 000000000..11ef53808 --- /dev/null +++ b/app/jobs/cleanup_votes_job.rb @@ -0,0 +1,17 @@ +class CleanupVotesJob < ApplicationJob + queue_as :default + + def perform + Community.all.each do |c| + RequestContext.community = c + orphan_votes = Vote.all.reject { |v| v.post.present? } + puts "[#{c.name}] destroying #{orphan_votes.length} #{'orphan vote'.pluralize(orphan_votes.length)}" + failed = orphan_votes.reject(&:destroy) + + failed.each do |v| + puts "[#{c.name}] failed to destroy vote \"#{v.id}\"" + v.errors.each { |e| puts e.full_message } + end + end + end +end diff --git a/config/schedule.rb b/config/schedule.rb index 39d5eee0b..8b0ea73db 100644 --- a/config/schedule.rb +++ b/config/schedule.rb @@ -18,6 +18,10 @@ runner 'scripts/cleanup_drafts.rb' end +every 1.day, ay: '02.25' do + runner 'scripts/cleanup_votes.rb' +end + every 6.hours do runner 'scripts/recalc_abilities.rb' end diff --git a/scripts/cleanup_votes.rb b/scripts/cleanup_votes.rb new file mode 100644 index 000000000..67707824e --- /dev/null +++ b/scripts/cleanup_votes.rb @@ -0,0 +1 @@ +CleanupVotesJob.perform_later \ No newline at end of file