diff --git a/app/models/school.rb b/app/models/school.rb index 6b931c2..c72ddfb 100644 --- a/app/models/school.rb +++ b/app/models/school.rb @@ -31,12 +31,18 @@ class School < ActiveRecord::Base end def calculate_score - newScore = total_points_scored_by_wrestlers - total_points_deducted + if Rails.env.production? + self.delay(:job_owner_id => self.tournament.id, :job_owner_type => "Calculate team score for #{self.name}").calculate_score_raw + else + calculate_score_raw + end + + end + + def calculate_score_raw + newScore = total_points_scored_by_wrestlers - total_points_deducted self.score = newScore self.save - end - if Rails.env.production? - handle_asynchronously :calculate_score end def total_points_scored_by_wrestlers diff --git a/app/models/tournament.rb b/app/models/tournament.rb index 397e84a..8efe60f 100644 --- a/app/models/tournament.rb +++ b/app/models/tournament.rb @@ -12,6 +12,10 @@ class Tournament < ActiveRecord::Base attr_accessor :import_text + def deferred_jobs + Delayed::Job.where(job_owner_id: self.id) + end + def self.search(search) where("date LIKE ? or name LIKE ?", "%#{search}%", "%#{search}%") end diff --git a/app/services/bracket_advancement/advance_wrestler.rb b/app/services/bracket_advancement/advance_wrestler.rb index 8c20a08..ab82bcf 100644 --- a/app/services/bracket_advancement/advance_wrestler.rb +++ b/app/services/bracket_advancement/advance_wrestler.rb @@ -5,17 +5,24 @@ class AdvanceWrestler end def advance - pool_to_bracket_advancement if @tournament.tournament_type == "Pool to bracket" + if Rails.env.production? + self.delay(:job_owner_id => @tournament.id, :job_owner_type => "Advance wrestler #{@wrestler.name} in the bracket").advance_raw + else + advance_raw + end end - if Rails.env.production? - handle_asynchronously :advance + + def advance_raw + pool_to_bracket_advancement if @tournament.tournament_type == "Pool to bracket" end def pool_to_bracket_advancement if @wrestler.weight.all_pool_matches_finished(@wrestler.pool) and (@wrestler.finished_bracket_matches.size == 0 or @wrestler.weight.pools == 1) PoolOrder.new(@wrestler.weight.wrestlers_in_pool(@wrestler.pool)).getPoolOrder end - PoolAdvance.new(@wrestler,@wrestler.last_match).advanceWrestler + if @wrestler.weight.all_pool_matches_finished(@wrestler.pool) + PoolAdvance.new(@wrestler,@wrestler.last_match).advanceWrestler + end end end \ No newline at end of file diff --git a/app/services/tournament_services/generate_tournament_matches.rb b/app/services/tournament_services/generate_tournament_matches.rb index 7431e89..6bebef1 100644 --- a/app/services/tournament_services/generate_tournament_matches.rb +++ b/app/services/tournament_services/generate_tournament_matches.rb @@ -4,28 +4,38 @@ class GenerateTournamentMatches end def generateWeight(weight) - WipeTournamentMatches.new(@tournament).wipeWeightMatches(weight) - @tournament.curently_generating_matches = 1 - @tournament.save - unAssignBouts - unAssignMats - PoolToBracketMatchGeneration.new(@tournament).generatePoolToBracketMatchesWeight(weight) if @tournament.tournament_type == "Pool to bracket" - postMatchCreationActions - PoolToBracketGenerateLoserNames.new(@tournament).assignLoserNamesWeight(weight) if @tournament.tournament_type == "Pool to bracket" + if Rails.env.production? + self.delay(:job_owner_id => @tournament.id, :job_owner_type => "Generate matches for weights class #{weight.max}").generate_weight_raw(weight) + else + self.generate_weight_raw(weight) + end end - if Rails.env.production? - handle_asynchronously :generateWeight + + def generate_weight_raw(weight) + WipeTournamentMatches.new(@tournament).wipeWeightMatches(weight) + @tournament.curently_generating_matches = 1 + @tournament.save + unAssignBouts + unAssignMats + PoolToBracketMatchGeneration.new(@tournament).generatePoolToBracketMatchesWeight(weight) if @tournament.tournament_type == "Pool to bracket" + postMatchCreationActions + PoolToBracketGenerateLoserNames.new(@tournament).assignLoserNamesWeight(weight) if @tournament.tournament_type == "Pool to bracket" end def generate + if Rails.env.production? + self.delay(:job_owner_id => @tournament.id, :job_owner_type => "Generate matches for all weights").generate_raw + else + self.generate_raw + end + end + + def generate_raw standardStartingActions PoolToBracketMatchGeneration.new(@tournament).generatePoolToBracketMatches if @tournament.tournament_type == "Pool to bracket" postMatchCreationActions PoolToBracketMatchGeneration.new(@tournament).assignLoserNames if @tournament.tournament_type == "Pool to bracket" end - if Rails.env.production? - handle_asynchronously :generate - end def standardStartingActions @tournament.curently_generating_matches = 1 diff --git a/app/services/tournament_services/wrestlingdev_importer.rb b/app/services/tournament_services/wrestlingdev_importer.rb index cd08567..9f01c39 100644 --- a/app/services/tournament_services/wrestlingdev_importer.rb +++ b/app/services/tournament_services/wrestlingdev_importer.rb @@ -7,6 +7,14 @@ class WrestlingdevImporter end def import + if Rails.env.production? + self.delay(:job_owner_id => @tournament.id, :job_owner_type => "Importing a backup").import_raw + else + import_raw + end + end + + def import_raw @tournament.curently_generating_matches = 1 @tournament.save destroy_all @@ -14,9 +22,6 @@ class WrestlingdevImporter @tournament.curently_generating_matches = nil @tournament.save end - if Rails.env.production? - handle_asynchronously :import - end def destroy_all @tournament.mats.reload.each do | mat | diff --git a/app/views/tournaments/show.html.erb b/app/views/tournaments/show.html.erb index a0ff7b1..a0c4874 100644 --- a/app/views/tournaments/show.html.erb +++ b/app/views/tournaments/show.html.erb @@ -1,30 +1,23 @@

Info

-

Address: <%= @tournament.address %>

- - -

Director: <%= @tournament.director %>

-

Director email: <%= @tournament.director_email %>

-

Tournament Type: <%= @tournament.tournament_type %>


-

Schools

(Click to see lineups, score breakdowns, and stats)


@@ -35,8 +28,6 @@ <% if can? :manage, @tournament %><%= link_to " New School" , "/schools/new?tournament=#{@tournament.id}", :class=>"fas fa-plus" %><% end %> - - <% @schools.each do |school| %> @@ -55,9 +46,6 @@
-
- -

Weight Class Seeds


@@ -84,7 +72,6 @@ <% end %>
- <% if can? :manage, @tournament %>

@@ -96,7 +83,6 @@ <%= link_to " New Mat" , "/mats/new?tournament=#{@tournament.id}", :class=>"fas fa-plus" %> - <% @mats.each do |mat| %> @@ -111,7 +97,35 @@ <% end %> - +
+

Background Jobs

+

This is a list of queued or running background jobs. Match generation, bracket advancement, team score calculation, etc.

+ + + + + + + + + + <% @tournament.deferred_jobs.each do |job| %> + + + + + <% end %> + +
Name of JobJob Status
<%= job.job_owner_type %> + <% if job.locked_at %> + Running + <% elsif job.last_error %> + Error + <% elsif job.attempts == 0 and !job.locked_at %> + Pending + <% end %> +
+

<%= render 'import_form' %> diff --git a/db/migrate/20191126174257_add_owner_to_jobs.rb b/db/migrate/20191126174257_add_owner_to_jobs.rb new file mode 100644 index 0000000..c4747a0 --- /dev/null +++ b/db/migrate/20191126174257_add_owner_to_jobs.rb @@ -0,0 +1,6 @@ +class AddOwnerToJobs < ActiveRecord::Migration[6.0] + def change + add_column :delayed_jobs, :job_owner_id, :integer + add_column :delayed_jobs, :job_owner_type, :string + end +end