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
command.join(' ')
end
end
NodeAssignment = Struct.new(:index, :tasks, :total_duration) do
NodeAssignment = Struct.new(:index, :tasks) do
def total_duration
return 0 if tasks.empty?
......@@ -53,7 +53,7 @@ class StaticAnalysis
Task.new(%w[scripts/lint-rugged], 1),
Task.new(%w[scripts/gemfile_lock_changed.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 = {})
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
def tasks_to_run(node_total)
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 }
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:"
tasks_by_duration_desc.each { |task| puts "* #{task.cmd} (#{task.duration}s)" }
# Distribute tasks optimally first
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
leftover_tasks = tasks_by_duration_desc - nodes.flat_map(&:tasks)
......@@ -157,17 +157,17 @@ class StaticAnalysis
nodes
end
def distribute_tasks(tasks, nodes, ideal_time_per_job: nil)
def distribute_tasks(tasks, nodes, ideal_time_per_node: nil)
condition =
if ideal_time_per_job
->(task, node, ideal_time_per_job) { (task.duration + node.total_duration) <= ideal_time_per_job }
if ideal_time_per_node
->(task, node, ideal_time_per_node) { (task.duration + node.total_duration) <= ideal_time_per_node }
else
->(*) { true }
end
tasks.each do |task|
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)
break
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