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

#config, #environment, #session

Attributes included from Language::AuxiliarLogger

#logger

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, 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



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

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:



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

def add(job, &block)
  msg = "Expected Eco::API::Session::Batch::Job object. Given #{job.class}"
  fatal msg 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
34
# File 'lib/eco/api/session/batch/jobs.rb', line 30

def each(&block)
  return to_enum(:each) unless block_given?

  items.each(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
  count.zero?
end

#errors?Boolean

Returns:

  • (Boolean)


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

def errors?
  any?(&:errors?)
end

#exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#find_jobs(type:) ⇒ Object



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

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

#itemsObject



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

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.



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

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.



95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/eco/api/session/batch/jobs.rb', line 95

def launch(simulate: false)
  each_with_index do |job, idx|
    next unless job.pending?

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

  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:



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

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)


87
88
89
# File 'lib/eco/api/session/batch/jobs.rb', line 87

def pending?
  any?(&:pending?)
end

#resetObject



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

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

#status(&block) ⇒ Object



115
116
117
118
119
120
121
122
# File 'lib/eco/api/session/batch/jobs.rb', line 115

def status(&block)
  if block_given?
    status.each(&block)
    self
  else
    @jobs_status ||= {} # rubocop:disable Naming/MemoizedInstanceVariableName
  end
end

#summaryObject



128
129
130
131
132
# File 'lib/eco/api/session/batch/jobs.rb', line 128

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