module Eco module API class Session class JobGroups < API::Common::Session::BaseSession DELAY_BETWEEN_GROUPS = 2 def initialize(session:) raise("JobGroups requires a session object") unless session.is_a?(API::Session) super(session.enviro) @session = session 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) raise("Can't create job group named #{'name'} because it already exists.") if exists?(name) group = BatchJobs.new(name, session: @session) @groups[name] = group if order == :last @order.push(group) else @order.unshift(group) end @callbacks[group] = Proc.new if block_given? group end def pending? @groups.any? do |group| group.pending? end end def launch(simulate: false) groups_status = {} @order.each.with_index do |group, idx| groups_status[group] = group_status = group.launch(simulate: simulate) callback = @callbacks[group] callback.call(group, group_status) if callback JobGroups.counter(DELAY_BETWEEN_GROUPS) if !simulate && idx < @order.length - 1 end return groups_status end def self.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 class BatchJobs < API::Common::Session::BaseSession attr_reader :name, :session def initialize(name, session:) raise("BatchJobs requires a session object") unless session.is_a?(API::Session) super(session.enviro) @session = session @name = name reset end def reset @jobs = {} @callbacks = {} end def [](name) @jobs[name] end def exists?(name) @jobs.key?(name) end def new(name, type:, sets:) raise("Can't create job named '#{name}' because it already exists.") if exists?(name) job = BatchJob.new(name, type: type, sets: sets, root: self) @jobs[name] = job @callbacks[job] = Proc.new if block_given? job end def pending? @jobs.keys.any? do |key| @jobs[key].pending? end end def launch(simulate: false) group_status = {} @jobs.each do |name, job| group_status[job] = job_status = job.launch(simulate: simulate) callback = @callbacks[job] callback.call(job, job_status) if callback end return group_status end end end end end