Class: Eco::API::Session::Batch::Jobs

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

Constant Summary collapse

DELAY_BETWEEN_JOBS =
0.3

Instance Attribute Summary collapse

Attributes inherited from Common::Session::BaseSession

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

Instance Method Summary collapse

Methods inherited from Common::Session::BaseSession

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

Constructor Details

#initialize(e, name:) ⇒ Jobs

Returns a new instance of Jobs.



11
12
13
14
15
# File 'lib/eco/api/session/batch/jobs.rb', line 11

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

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/eco/api/session/batch/jobs.rb', line 9

def name
  @name
end

Instance Method Details

#[](name) ⇒ Object



39
40
41
# File 'lib/eco/api/session/batch/jobs.rb', line 39

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

#add(job) {|job, status| ... } ⇒ Eco::API::Session::Batch::Job

Parameters:

Yields:

  • (job, status)

    callback after launching the batch job request against the server.

Yield Parameters:

Returns:



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

def add(job, &block)
  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] = block if block_given?
end

#each(&block) ⇒ Object



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

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

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
  count == 0
end

#errors?Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/eco/api/session/batch/jobs.rb', line 121

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

#exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/eco/api/session/batch/jobs.rb', line 43

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

#find_jobs(type:) ⇒ Object



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

def find_jobs(type:)
  each_with_object([]) do |job, jbs|
    jbs.push(job) if job.type == type
  end
end

#itemsObject



35
36
37
# File 'lib/eco/api/session/batch/jobs.rb', line 35

def items
  @jobs.values
end

#job(name, type: nil, sets: nil, usecase: nil, accept_update_with_no_id: false, &block) ⇒ Eco::API::Session::Batch::Job

Note:
  • the block should only be passed when creating the job

It retrieves an existing job named name or creates it if it doesn't exist.



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

def job(name, type: nil, sets: nil, usecase: nil, accept_update_with_no_id: false, &block)
  fatal "Can't give a job post-launch callback &block to an already existing job" if exists?(name)
  new(name, type: type, sets: sets, usecase: usecase, accept_update_with_no_id: accept_update_with_no_id, &block) unless exists?(name)
  self[name]
end

#launch(simulate: false) ⇒ Hash<Eco::API::Session::Batch::Job, Eco::API::Session::Batch::Status>

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

Launches every Batch::Job in the group.



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

def launch(simulate: false)
  each_with_index do |job, idx|
    if job.pending?
      status[job] = job_status = job.launch(simulate: simulate)
      callback    = @callbacks[job]
      callback.call(job, job_status) if callback
      Eco::API::Session::Batch::JobsGroups.counter(delay_between_jobs) if !simulate && idx < self.length - 1
    end
  end
  launch(simulate: simulate) if pending?
  return status
end

#lengthObject



22
23
24
# File 'lib/eco/api/session/batch/jobs.rb', line 22

def length
  count
end

#new(name, type:, sets:, usecase: nil, accept_update_with_no_id: false) {|job, status| ... } ⇒ Eco::API::Session::Batch::Job

Creates a new Batch::Job included as part of this Batch::Jobs.

Parameters:

  • (see Eco::API::Session::Job#new)

Yields:

  • (job, status)

    callback after launching the batch job request against the server.

Yield Parameters:

Returns:



64
65
66
67
68
69
70
# File 'lib/eco/api/session/batch/jobs.rb', line 64

def new(name, type:, sets:, usecase: nil, accept_update_with_no_id: false, &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, accept_update_with_no_id: accept_update_with_no_id).tap do |job|
    add(job, &block)
  end
end

#pending?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/eco/api/session/batch/jobs.rb', line 83

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

#resetObject



17
18
19
20
# File 'lib/eco/api/session/batch/jobs.rb', line 17

def reset
  @jobs = {}
  @callbacks = {}
end

#statusObject



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

def status
  if block_given?
    status.each do |job, job_status|
      yield(job, job_status)
    end
    self
  else
    @jobs_status ||= {}
  end
end

#summaryObject



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

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