Class: Eco::API::Session::Batch::JobsGroups

Inherits:
Common::Session::BaseSession show all
Includes:
Enumerable
Defined in:
lib/eco/api/session/batch/jobs_groups.rb

Constant Summary collapse

DELAY_BETWEEN_GROUPS =
2

Instance Attribute Summary

Attributes inherited from Common::Session::BaseSession

#api, #config, #environment, #file_manager, #logger, #session

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Common::Session::BaseSession

#enviro=, #fatal, #fm, #mailer, #mailer?, #s3uploader, #s3uploader?, #sftp, #sftp?

Constructor Details

#initialize(e) ⇒ JobsGroups

Returns a new instance of JobsGroups.



25
26
27
28
# File 'lib/eco/api/session/batch/jobs_groups.rb', line 25

def initialize(e)
  super(e)
  reset
end

Class Method Details

.counter(seconds) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/eco/api/session/batch/jobs_groups.rb', line 9

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

Instance Method Details

#[](name) ⇒ Object



53
54
55
# File 'lib/eco/api/session/batch/jobs_groups.rb', line 53

def [](name)
  @groups[name]
end

#each(&block) ⇒ Object



44
45
46
47
# File 'lib/eco/api/session/batch/jobs_groups.rb', line 44

def each(&block)
  return to_enum(:each) unless block
  items.each(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/eco/api/session/batch/jobs_groups.rb', line 40

def empty?
  count == 0
end

#errors?Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/eco/api/session/batch/jobs_groups.rb', line 110

def errors?
  any? {|group| group.errors?}
end

#exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/eco/api/session/batch/jobs_groups.rb', line 57

def exists?(name)
  @groups.key?(name)
end

#find_jobs(type:) ⇒ Object



93
94
95
96
97
# File 'lib/eco/api/session/batch/jobs_groups.rb', line 93

def find_jobs(type:)
  each_with_object([]) do |group, jbs|
    jbs.concat(group.find_jobs(type: type))
  end
end

#itemsObject



49
50
51
# File 'lib/eco/api/session/batch/jobs_groups.rb', line 49

def items
  @groups.values
end

#launch(simulate: false) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/eco/api/session/batch/jobs_groups.rb', line 81

def launch(simulate: false)
  @order.each_with_index do |group, idx|
    if group.pending?
      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 status
end

#lengthObject



36
37
38
# File 'lib/eco/api/session/batch/jobs_groups.rb', line 36

def length
  count
end

#new(name, order: :last) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/eco/api/session/batch/jobs_groups.rb', line 61

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

#pending?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/eco/api/session/batch/jobs_groups.rb', line 77

def pending?
  any? {|group| group.pending?}
end

#resetObject



30
31
32
33
34
# File 'lib/eco/api/session/batch/jobs_groups.rb', line 30

def reset
  @order  = []
  @groups = {}
  @callbacks = {}
end

#statusObject



99
100
101
102
103
104
105
106
107
108
# File 'lib/eco/api/session/batch/jobs_groups.rb', line 99

def status
  if block_given?
    status.each do |group, group_status|
      yield(group, group_status)
    end
    self
  else
    @groups_status ||= {}
  end
end

#summaryObject



114
115
116
117
118
# File 'lib/eco/api/session/batch/jobs_groups.rb', line 114

def summary
  [].tap do |msg|
    map {|group| msg << group.summary}
  end.join("\n")
end