Sha256: e4612b8dcefae662221e2efd0bc71a4265876b9bd22b09ea0483aed0248ed161

Contents?: true

Size: 1.79 KB

Versions: 3

Compression:

Stored size: 1.79 KB

Contents

module Eco
  module API
    class Session
      class JobGroups < API::Common::Session::BaseSession
        DELAY_BETWEEN_GROUPS = 2

        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)

          group = BatchJobs.new(enviro, name: name)
          @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
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
eco-helpers-0.6.17 lib/eco/api/session/job_groups.rb
eco-helpers-0.6.16 lib/eco/api/session/job_groups.rb
eco-helpers-0.6.15 lib/eco/api/session/job_groups.rb