module Eco module API class Session class Batch class JobsGroups < API::Common::Session::BaseSession DELAY_BETWEEN_GROUPS = 2 class << self def counter(seconds) puts "\n" while seconds + 1 > 0 do print " Waiting #{seconds}\r" $stdout.flush seconds -= 1 sleep 1 end print "#{" " * 40}" $stdout.flush puts "" end end def initialize(e) super(e) reset end def reset @order = [] @groups = {} @callbacks = {} end def [](name) @groups[name] end def exists?(name) @groups.key?(name) end def new(name, order: :last) fatal "Can't create job group named '#{name}' because it already exists." if exists?(name) Batch::Jobs.new(enviro, name: name).tap do |group| @groups[name] = group if order == :last @order.push(group) else @order.unshift(group) end @callbacks[group] = Proc.new if block_given? end end def pending? @groups.any? {|group| group.pending?} end def launch(simulate: false) groups_status = {} @order.each.with_index do |group, idx| if group.pending? groups_status[group] = group_status = group.launch(simulate: simulate) callback = @callbacks[group] callback.call(group, group_status) if callback self.class.counter(DELAY_BETWEEN_GROUPS) if !simulate && idx < @order.length - 1 end end return groups_status end def find_jobs(type:) @groups.each_with_object([]) do |(k, gr), jbs| jbs.concat(gr.find_jobs(type: type)) end end end end end end end