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



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.zero?
end

#errors?Boolean

Returns:

  • (Boolean)


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

def errors?
  any?(&: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



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

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
103
# File 'lib/eco/api/session/batch/jobs.rb', line 91

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:



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?(&: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



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

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

#summaryObject



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

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