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:
43
app/jobs/tournament_cleanup_job.rb
Normal file
43
app/jobs/tournament_cleanup_job.rb
Normal 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
|
||||
Reference in New Issue
Block a user