module Eco module API class Session class Batch class Jobs < API::Common::Session::BaseSession include Enumerable attr_reader :name def initialize(e, name:) super(e) @name = name reset end def reset @jobs = {} @callbacks = {} end def length count end def empty? count == 0 end def each(&block) return to_enum(:each) unless block items.each(&block) end def items @jobs.values end def [](name) @jobs[name] end def exists?(name) @jobs.key?(name) end def job(name, type: nil, sets: nil, usecase: nil, &block) new(name, type: type, sets: sets, usecase: usecase, &block) unless exists?(name) self[name].tap do |job| block.call(job) if block end end def new(name, type:, sets:, usecase: nil, &block) fatal "Can't create job named '#{name}' because it already exists." if exists?(name) Batch::Job.new(enviro, name: name, type: type, sets: sets, usecase: usecase).tap do |job| add(job, &block) end end def add(job) fatal "Expected Eco::API::Session::Batch::Job object. Given #{job.class}" unless job.is_a?(Eco::API::Session::Batch::Job) @jobs[job.name] = job @callbacks[job] = Proc.new if block_given? end def pending? any? {|job| job.pending?} end def launch(simulate: false) each do |job| if job.pending? status[job] = job_status = job.launch(simulate: simulate) callback = @callbacks[job] callback.call(job, job_status) if callback end end return status end def find_jobs(type:) each_with_object([]) do |job, jbs| jbs.push(job) if job.type == type end end def status if block_given? status.each do |job, job_status| yield(job, job_status) end self else @jobs_status ||= {} end end def errors? any? {|job| job.errors?} end def summary [].tap do |msg| map {|job| msg << job.summary} end.join("\n") end end end end end end