Sha256: cd30fa9878bfb1b2cff715097eb3944ac1f03b66290eb084cb86ed243aa90c91

Contents?: true

Size: 1.88 KB

Versions: 18

Compression:

Stored size: 1.88 KB

Contents

module Eco
  module API
    class Session
      class Batch
        class Jobs < API::Common::Session::BaseSession
          attr_reader :name

          def initialize(e, name:)
            super(e)
            @name = name
            reset
          end

          def reset
            @jobs = {}
            @callbacks = {}
          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)
            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|
              @jobs[name] = job
              @callbacks[job] = Proc.new if block_given?
            end
          end

          def pending?
            @jobs.keys.any? {|key| @jobs[key].pending?}
          end

          def launch(simulate: false)
            group_status = {}
            @jobs.each do |name, job|
              if job.pending?
                group_status[job] = job_status = job.launch(simulate: simulate)
                callback = @callbacks[job]
                callback.call(job, job_status) if callback
              end
            end

            return group_status
          end

          def find_jobs(type:)
            @jobs.each_with_object([]) do |(k, jb), jbs|
              if jb.type == type
                jbs.push(jb)
              end
            end
          end

        end
      end
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
eco-helpers-1.1.2 lib/eco/api/session/batch/jobs.rb
eco-helpers-1.1.1 lib/eco/api/session/batch/jobs.rb
eco-helpers-1.0.14 lib/eco/api/session/batch/jobs.rb
eco-helpers-1.0.13 lib/eco/api/session/batch/jobs.rb
eco-helpers-1.0.12 lib/eco/api/session/batch/jobs.rb
eco-helpers-1.0.11 lib/eco/api/session/batch/jobs.rb
eco-helpers-1.0.10 lib/eco/api/session/batch/jobs.rb
eco-helpers-1.0.9 lib/eco/api/session/batch/jobs.rb
eco-helpers-1.0.8 lib/eco/api/session/batch/jobs.rb
eco-helpers-1.0.7 lib/eco/api/session/batch/jobs.rb
eco-helpers-1.0.6 lib/eco/api/session/batch/jobs.rb
eco-helpers-1.0.5 lib/eco/api/session/batch/jobs.rb
eco-helpers-1.0.4 lib/eco/api/session/batch/jobs.rb
eco-helpers-1.0.3 lib/eco/api/session/batch/jobs.rb
eco-helpers-0.9.5 lib/eco/api/session/batch/jobs.rb
eco-helpers-1.0.2 lib/eco/api/session/batch/jobs.rb
eco-helpers-0.9.4 lib/eco/api/session/batch/jobs.rb
eco-helpers-0.9.3 lib/eco/api/session/batch/jobs.rb