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

#config, #environment, #session

Attributes included from Language::AuxiliarLogger

#logger

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Common::Session::BaseSession

#api, #api?, #fatal, #file_manager, #logger, #mailer, #mailer?, #s3uploader, #s3uploader?, #sftp, #sftp?

Methods included from Language::AuxiliarLogger

#log

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)


120
121
122
# File 'lib/eco/api/session/batch/jobs_groups.rb', line 120

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



103
104
105
106
107
# File 'lib/eco/api/session/batch/jobs_groups.rb', line 103

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) ⇒ Hash<Eco::API::Session::Batch::Jobs,Hash<Eco::API::Session::Batch::Job, Eco::API::Session::Batch::Status>>

Note:
  • if there was post-launch callback for a Batch::Jobs groups, it calls it.

Launches every Batch::Jobs group in the current Batch::JobGroups



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/eco/api/session/batch/jobs_groups.rb', line 90

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
  launch(simulate: simulate) if pending?
  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) {|group, group_status| ... } ⇒ Eco::API::Session::Batch::Jobs

Creates a new group of Batch::Jobs named name.

Yields:

  • (group, group_status)

    callback after launching the batch job request against the server.

Yield Parameters:

Returns:



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/eco/api/session/batch/jobs_groups.rb', line 66

def new(name, order: :last, &block)
  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] = block if block_given?
  end
end

#pending?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/eco/api/session/batch/jobs_groups.rb', line 82

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



109
110
111
112
113
114
115
116
117
118
# File 'lib/eco/api/session/batch/jobs_groups.rb', line 109

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

#summaryObject



124
125
126
127
128
# File 'lib/eco/api/session/batch/jobs_groups.rb', line 124

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