Commit ddcd4b58 authored by Rémy Coutable's avatar Rémy Coutable

Address latest review

Signed-off-by: default avatarRémy Coutable <remy@rymai.me>
parent d4b10599
...@@ -19,7 +19,7 @@ class StaticAnalysis ...@@ -19,7 +19,7 @@ class StaticAnalysis
command.join(' ') command.join(' ')
end end
end end
NodeAssignment = Struct.new(:index, :tasks, :total_duration) do NodeAssignment = Struct.new(:index, :tasks) do
def total_duration def total_duration
return 0 if tasks.empty? return 0 if tasks.empty?
...@@ -53,7 +53,7 @@ class StaticAnalysis ...@@ -53,7 +53,7 @@ class StaticAnalysis
Task.new(%w[scripts/lint-rugged], 1), Task.new(%w[scripts/lint-rugged], 1),
Task.new(%w[scripts/gemfile_lock_changed.sh], 1), Task.new(%w[scripts/gemfile_lock_changed.sh], 1),
Task.new(%w[scripts/frontend/check_no_partial_karma_jest.sh], 1) Task.new(%w[scripts/frontend/check_no_partial_karma_jest.sh], 1)
].reject { |t| t.nil? }.freeze ].compact.freeze
def run_tasks!(options = {}) def run_tasks!(options = {})
node_assignment = tasks_to_run((ENV['CI_NODE_TOTAL'] || 1).to_i)[(ENV['CI_NODE_INDEX'] || 1).to_i - 1] node_assignment = tasks_to_run((ENV['CI_NODE_TOTAL'] || 1).to_i)[(ENV['CI_NODE_INDEX'] || 1).to_i - 1]
...@@ -129,17 +129,17 @@ class StaticAnalysis ...@@ -129,17 +129,17 @@ class StaticAnalysis
def tasks_to_run(node_total) def tasks_to_run(node_total)
total_time = TASKS_WITH_DURATIONS_SECONDS.sum(&:duration).to_f total_time = TASKS_WITH_DURATIONS_SECONDS.sum(&:duration).to_f
ideal_time_per_job = total_time / node_total ideal_time_per_node = total_time / node_total
tasks_by_duration_desc = TASKS_WITH_DURATIONS_SECONDS.sort_by { |a| -a.duration } tasks_by_duration_desc = TASKS_WITH_DURATIONS_SECONDS.sort_by { |a| -a.duration }
nodes = Array.new(node_total) { |i| NodeAssignment.new(i + 1, [], 0) } nodes = Array.new(node_total) { |i| NodeAssignment.new(i + 1, []) }
puts "Total expected time: #{total_time}; ideal time per job: #{ideal_time_per_job}.\n\n" puts "Total expected time: #{total_time}; ideal time per job: #{ideal_time_per_node}.\n\n"
puts "Tasks to distribute:" puts "Tasks to distribute:"
tasks_by_duration_desc.each { |task| puts "* #{task.cmd} (#{task.duration}s)" } tasks_by_duration_desc.each { |task| puts "* #{task.cmd} (#{task.duration}s)" }
# Distribute tasks optimally first # Distribute tasks optimally first
puts "\nAssigning tasks optimally." puts "\nAssigning tasks optimally."
distribute_tasks(tasks_by_duration_desc, nodes, ideal_time_per_job: ideal_time_per_job) distribute_tasks(tasks_by_duration_desc, nodes, ideal_time_per_node: ideal_time_per_node)
# Distribute remaining tasks, ordered by ascending duration # Distribute remaining tasks, ordered by ascending duration
leftover_tasks = tasks_by_duration_desc - nodes.flat_map(&:tasks) leftover_tasks = tasks_by_duration_desc - nodes.flat_map(&:tasks)
...@@ -157,17 +157,17 @@ class StaticAnalysis ...@@ -157,17 +157,17 @@ class StaticAnalysis
nodes nodes
end end
def distribute_tasks(tasks, nodes, ideal_time_per_job: nil) def distribute_tasks(tasks, nodes, ideal_time_per_node: nil)
condition = condition =
if ideal_time_per_job if ideal_time_per_node
->(task, node, ideal_time_per_job) { (task.duration + node.total_duration) <= ideal_time_per_job } ->(task, node, ideal_time_per_node) { (task.duration + node.total_duration) <= ideal_time_per_node }
else else
->(*) { true } ->(*) { true }
end end
tasks.each do |task| tasks.each do |task|
nodes.each do |node| nodes.each do |node|
if condition.call(task, node, ideal_time_per_job) if condition.call(task, node, ideal_time_per_node)
assign_task_to_node(tasks, node, task) assign_task_to_node(tasks, node, task)
break break
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment