lib/fasten/task_manager.rb in fasten-0.7.6 vs lib/fasten/task_manager.rb in fasten-0.8.0

- old
+ new

@@ -1,10 +1,10 @@ module Fasten class TaskManager < Array attr_reader :done, :failed, :pending, :running, :targets - def initialize(targets: nil) + def initialize(targets: []) super() @map = {} @done = [] @failed = [] @@ -51,10 +51,11 @@ def waiting return @waiting if @waiting reset setup_dependencies + setup_targets setup_scores move_pending_to_waiting end def no_waiting? @@ -85,12 +86,10 @@ @done = by_state.delete(:DONE) || [] @failed = by_state.delete(:FAIL) || [] @running = by_state.delete(:RUNNING) || [] @waiting = [] - return unless @targets.nil? || @targets.empty? - @pending = by_state.values.flatten.each do |task| task.depends = [] task.dependants = [] task.state = nil end @@ -108,11 +107,40 @@ after_task.dependants << task end end end - def setup_scores + def setup_targets + return if @targets.empty? + + @targets.each do |target| + task = target.is_a?(Task) ? target : @map[target] + raise "Target task #{target} not found" unless task + + mark_needed(task) + end + + @pending.reject { |task| task.state == :NEED }.each do |task| + @pending.delete task + delete task + end + @pending.each do |task| + task.state = nil + end + end + + def mark_needed(task) + return unless task.state == :IDLE + + task.state = :NEED + task.depends.each do |depend_task| + mark_needed(depend_task) + end + end + + def setup_scores + each do |task| task.run_score = task.dependants.count end end def move_pending_to_waiting