1
0
mirror of https://github.com/jcwimer/wrestlingApp synced 2026-04-04 21:53:47 +00:00

Added a daily recurring job to cleanup tournaments .

This commit is contained in:
2025-05-06 20:58:45 -04:00
parent 2856060b11
commit 6825da5547
6 changed files with 129 additions and 16 deletions

View File

@@ -0,0 +1,43 @@
class TournamentCleanupJob < ApplicationJob
queue_as :default
def perform
# Task 1: Remove tournaments that are 1 week old and have 0 matches finished
remove_empty_old_tournaments
# Task 2: Cleanup tournaments that are a week old with at least 1 finished match
cleanup_active_old_tournaments
end
private
def remove_empty_old_tournaments
# Find tournaments older than 1 week with no finished matches
Tournament.where('date < ?', 1.week.ago.to_date)
.joins('LEFT JOIN matches ON matches.tournament_id = tournaments.id AND matches.finished = 1')
.group('tournaments.id')
.having('COUNT(matches.id) = 0')
.destroy_all
end
def cleanup_active_old_tournaments
# Find tournaments older than 1 week with at least 1 finished match
old_active_tournaments = Tournament.where('date < ?', 1.week.ago.to_date)
.joins('INNER JOIN matches ON matches.tournament_id = tournaments.id AND matches.finished = 1')
.group('tournaments.id')
.having('COUNT(matches.id) > 0')
old_active_tournaments.each do |tournament|
# 1. Remove all school delegates
tournament.schools.each do |school|
school.delegates.destroy_all
end
# 2. Remove all tournament delegates
tournament.delegates.destroy_all
# 3. Set user_id to null
tournament.update(user_id: nil)
end
end
end