Class: Eco::API::Session::Batch::Jobs
Constant Summary
collapse
- DELAY_BETWEEN_JOBS =
0.3
Instance Attribute Summary collapse
#config, #environment, #session
#logger
Instance Method Summary
collapse
-
#[](name) ⇒ Object
-
#add(job) {|job, status| ... } ⇒ Eco::API::Session::Batch::Job
-
#each(&block) ⇒ Object
-
#empty? ⇒ Boolean
-
#errors? ⇒ Boolean
-
#exists?(name) ⇒ Boolean
-
#find_jobs(type:) ⇒ Object
-
#initialize(e, name:) ⇒ Jobs
constructor
-
#items ⇒ Object
-
#job(name, type: nil, sets: nil, usecase: nil, accept_update_with_no_id: false, &block) ⇒ Eco::API::Session::Batch::Job
It retrieves an existing job named name
or creates it if it doesn't exist.
-
#launch(simulate: false) ⇒ Hash<Eco::API::Session::Batch::Job, Eco::API::Session::Batch::Status>
Launches every Batch::Job
in the group.
-
#length ⇒ Object
-
#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
.
-
#pending? ⇒ Boolean
-
#reset ⇒ Object
-
#status(&block) ⇒ Object
-
#summary ⇒ Object
#api, #api?, #fatal, #file_manager, #logger, #mailer, #mailer?, #s3uploader, #s3uploader?, #sftp, #sftp?
#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
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
39
40
41
|
# File 'lib/eco/api/session/batch/jobs.rb', line 39
def [](name)
@jobs[name]
end
|
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
26
27
28
|
# File 'lib/eco/api/session/batch/jobs.rb', line 26
def empty?
count.zero?
end
|
#errors? ⇒ Boolean
120
121
122
|
# File 'lib/eco/api/session/batch/jobs.rb', line 120
def errors?
any?(&:errors?)
end
|
#exists?(name) ⇒ 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
|
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
|
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
|
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
.
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
83
84
85
|
# File 'lib/eco/api/session/batch/jobs.rb', line 83
def pending?
any?(&:pending?)
end
|
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 ||= {} end
end
|
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
|