Class: Eco::API::Session::Batch::Job
- Inherits:
-
Common::Session::BaseSession
- Object
- Common::Session::BaseSession
- Eco::API::Session::Batch::Job
- Includes:
- Sets, Type, Language::Methods::DslAble
- Defined in:
- lib/eco/api/session/batch/job.rb,
lib/eco/api/session/batch/job/sets.rb,
lib/eco/api/session/batch/job/type.rb
Defined Under Namespace
Constant Summary
Constants included from Sets
Constants included from Type
Instance Attribute Summary collapse
-
#feedback ⇒ Eco::API::Session::Batch::Feedback
readonly
helper class for feedback and end-user decision making.
-
#name ⇒ String
readonly
the name of this
batch job. -
#sets ⇒ Array<Symbol>
readonly
the parts of the person model this batch is supposed to affect.
-
#status ⇒ Eco::API::Session::Batch::Status
readonly
if launched: the
statusof thebatch. -
#type ⇒ Symbol
readonly
a valid batch operation.
-
#usecase ⇒ Eco::API::UseCases::UseCase?
readonly
when provided:
usecasethat generated thisbatch job.
Attributes inherited from Common::Session::BaseSession
#config, #environment, #session
Attributes included from Language::AuxiliarLogger
Instance Method Summary collapse
-
#add(entry, unique: true) {|person| ... } ⇒ Eco::API::Session::Batch::Job
Adds an entry(ies) to the job queue.
-
#dup(name = "ad-hoc:job-from:#{self.name}", usecase: self.usecase) ⇒ Eco::API::Session::Batch::Job
Creates an empty
Batch::Jobwith same behaviour as the current one. - #ensure_requests! ⇒ Object
-
#error_handler? ⇒ Boolean
trueif the current batch job is a result of an error_handler. -
#errors? ⇒ Boolean
trueif there were Server errors,falseotherwise. -
#initialize(ev, name:, type:, sets:, usecase: nil, accept_update_with_no_id: false) ⇒ Job
constructor
A new instance of Job.
-
#launch(simulate: false) ⇒ Eco::API::Session::Batch::Status
Processes the
queueand, unlesssimulateistrue, launches against the server: 1. -
#options ⇒ Hash
Options the root
usecaseis run with. -
#pending? ⇒ Boolean
Has been this
batch joblaunched?. -
#people(input = @queue) ⇒ Eco::API::Organization::People
Helper/shortcut to obtain a people object out of
input. - #request_stats(requests = nil) ⇒ Object
-
#requests ⇒ Enumbrable<Hash>
The last requests that the queue will generate.
- #reset ⇒ Object
-
#subjobs ⇒ Eco::API::Session::Batch::Jobs
Group of subjobs of this
Batch::Job. -
#summary ⇒ String
Provides a text summary of the current status including: 1.
-
#usecase? ⇒ Boolean
Was this
batch jobgenerated by ausecase? (Eco::API::UseCases::UseCase).
Methods included from Language::Methods::DslAble
#evaluate, #method_missing, #respond_to_missing?
Methods inherited from Common::Session::BaseSession
#api, #api?, #fatal, #file_manager, #logger, #mailer, #mailer?, #s3uploader, #s3uploader?, #sftp, #sftp?
Methods included from Language::AuxiliarLogger
Constructor Details
#initialize(ev, name:, type:, sets:, usecase: nil, accept_update_with_no_id: false) ⇒ Job
Returns a new instance of Job.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/eco/api/session/batch/job.rb', line 39 def initialize(ev, name:, type:, sets:, usecase: nil, accept_update_with_no_id: false) msg = "A name is required to refer a job. Given: '#{name}'" raise msg unless name msg = "Type should be one of #{self.class.types}. Given: #{type}" raise msg unless self.class.valid_type?(type) msg = "Sets should be some of #{self.class.sets}. Given: #{sets}" raise msg unless self.class.valid_sets?(sets) msg = "usecase must be a Eco::API::UseCases::UseCase object. Given: #{usecase.class}" raise msg if usecase && !usecase.is_a?(Eco::API::UseCases::UseCase) super(ev) @name = name @type = type @sets = [sets].flatten.compact @usecase = usecase @feedback = Eco::API::Session::Batch::Feedback.new(job: self) @accept_update_with_no_id = accept_update_with_no_id reset end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Eco::Language::Methods::DslAble
Instance Attribute Details
#feedback ⇒ Eco::API::Session::Batch::Feedback (readonly)
helper class for feedback and end-user decision making
16 17 18 |
# File 'lib/eco/api/session/batch/job.rb', line 16 def feedback @feedback end |
#name ⇒ String (readonly)
the name of this batch job
16 17 18 |
# File 'lib/eco/api/session/batch/job.rb', line 16 def name @name end |
#sets ⇒ Array<Symbol> (readonly)
the parts of the person model this batch is supposed to affect
16 17 18 |
# File 'lib/eco/api/session/batch/job.rb', line 16 def sets @sets end |
#status ⇒ Eco::API::Session::Batch::Status (readonly)
if launched:
the status of the batch
16 17 18 |
# File 'lib/eco/api/session/batch/job.rb', line 16 def status @status end |
#type ⇒ Symbol (readonly)
a valid batch operation
16 17 18 |
# File 'lib/eco/api/session/batch/job.rb', line 16 def type @type end |
#usecase ⇒ Eco::API::UseCases::UseCase? (readonly)
when provided:
usecase that generated this batch job
16 17 18 |
# File 'lib/eco/api/session/batch/job.rb', line 16 def usecase @usecase end |
Instance Method Details
#add(entry, unique: true) {|person| ... } ⇒ Eco::API::Session::Batch::Job
Adds an entry(ies) to the job queue.
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/eco/api/session/batch/job.rb', line 118 def add(entry, unique: true, &block) case entry when Enumerable entry.each {|el| add(el, unique: unique, &block)} else return self unless entry return self if unique && @queue_hash.key?(entry) @queue_hash[entry] = true @queue.push(entry) @callbacks[entry] = block if block_given? end self end |
#dup(name = "ad-hoc:job-from:#{self.name}", usecase: self.usecase) ⇒ Eco::API::Session::Batch::Job
- this job will not be linked to the
Batch::Jobsmodel of the current session - mostly used for error_handlers
Creates an empty Batch::Job with same behaviour as the current one
76 77 78 79 80 81 82 83 84 |
# File 'lib/eco/api/session/batch/job.rb', line 76 def dup(name = "ad-hoc:job-from:#{self.name}", usecase: self.usecase) self.class.new( enviro, name: name, type: type, sets: sets, usecase: usecase ) end |
#ensure_requests! ⇒ Object
some times we need a summary of what actually
is going to be run without invoking launch
(i.e. when there's a uncompliance with the batch policy)
142 143 144 145 146 147 |
# File 'lib/eco/api/session/batch/job.rb', line 142 def ensure_requests! return if instance_variable_defined?(:@requests) pqueue = processed_queue @requests = as_update(pqueue) end |
#error_handler? ⇒ Boolean
Returns true if the current batch job is a result of an error_handler.
95 96 97 |
# File 'lib/eco/api/session/batch/job.rb', line 95 def error_handler? usecase? && usecase.is_a?(Eco::API::Error::Handler) end |
#errors? ⇒ Boolean
Returns true if there were Server errors, false otherwise.
166 167 168 |
# File 'lib/eco/api/session/batch/job.rb', line 166 def errors? status&.errors? end |
#launch(simulate: false) ⇒ Eco::API::Session::Batch::Status
Processes the queue and, unless simulate is true, launches against the server:
- pre_processes the queue obtaining the
requests:- if the entries of
queuegot pending callbacks (delayed changes), it processes them - unless type ==
:create: if there's a definedapi_excludedcallback it calls it (see Config::People#api_excluded) - transforms the result to a
Eco::API::Organization::Peopleobject - if there are
api policiesdefined, it passes the entries through them in order (see Config#policies)- this step is skipped if the option
-skip-api-policieswas used in the command line
- this step is skipped if the option
- at this point all the transformations have taken place...
- only include the entries that, after all above, still hold pending changes
(
!as_update.empty?) to be launched as update
- if the entries of
- pre launch checks against the
requests:- it generates
stats(Eco::API::Session::Batch::Feedback::RequestStats) out of the requests - if there is a batch policy declared for the current job
type, it checks compliance againststats(see Policies),- a non-compliant batch will stop the current session by raising an
Exception - this setp is skipped if the option
-skip-batch-policywas used in the command line
- a non-compliant batch will stop the current session by raising an
- it generates
- if we are not in
dry-run(orsimulate), it:- backs up the raw queries (
requests) launched to the Server, if we are not indry-run(orsimulate) - launches the batch request against the Server (see Eco::API::Session::Batch#launch)
- links the resulting batch
statusto thisBatch::Job(see Status) - prints any
errorsreplied by the Server
- backs up the raw queries (
- the post launch kicks in, and:
- for success requests, it consolidates the associated entries
(see
Ecoportal::API::V1::Person#consolidate!) - launches specific error handlers, if there were errors from the Server
as a result of the
batch.launch, and there areError::Handlersdefined
- for success requests, it consolidates the associated entries
(see
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/eco/api/session/batch/job.rb', line 207 def launch(simulate: false) # rubocop:disable Metrics/AbcSize pqueue = processed_queue @requests = as_update(pqueue) pre_checks(requests, simulate: simulate) if simulate if .dig(:requests, :backup) req_backup = as_update(pqueue, add_feedback: false) backup_update(req_backup, simulate: simulate) end elsif !pqueue.empty? req_backup = as_update(pqueue, add_feedback: false) backup_update(req_backup) log(:debug) { "Job ('#{name}':#{type}): going to launch batch against #{pqueue.count} entries" } session.batch.launch(pqueue, method: type).tap do |job_status| @status = job_status status.root = self status.errors.print end end unless requests.empty? || !simulate msg = "--- simulate mode (dry-run) -- job '#{name}' " msg << "-- this would have launched #{type.to_s.upcase}" log(:info) { msg } end post_launch(queue: pqueue, simulate: simulate) @pending = false status end |
#options ⇒ Hash
Returns options the root usecase is run with.
105 106 107 |
# File 'lib/eco/api/session/batch/job.rb', line 105 def usecase?? usecase. : {} end |
#pending? ⇒ Boolean
Returns has been this batch job launched?.
135 136 137 |
# File 'lib/eco/api/session/batch/job.rb', line 135 def pending? @pending end |
#people(input = @queue) ⇒ Eco::API::Organization::People
if input is not provided, it will use queue
Helper/shortcut to obtain a people object out of input
173 174 175 |
# File 'lib/eco/api/session/batch/job.rb', line 173 def people(input = @queue) Eco::API::Organization::People.new(input) end |
#request_stats(requests = nil) ⇒ Object
160 161 162 |
# File 'lib/eco/api/session/batch/job.rb', line 160 def request_stats(requests = nil) feedback.request_stats(requests || self.requests) end |
#requests ⇒ Enumbrable<Hash>
it requires launch to be firstly invoked
Returns the last requests that the queue will generate.
152 153 154 155 156 157 |
# File 'lib/eco/api/session/batch/job.rb', line 152 def requests msg = "Method missuse. Firstly 'launch' should be invoked" raise msg unless instance_variable_defined?(:@requests) @requests end |
#reset ⇒ Object
63 64 65 66 67 68 69 |
# File 'lib/eco/api/session/batch/job.rb', line 63 def reset @queue = [] @queue_hash = {} @callbacks = {} @pending = true @status = nil end |
#subjobs ⇒ Eco::API::Session::Batch::Jobs
Returns group of subjobs of this Batch::Job.
87 88 89 90 91 92 |
# File 'lib/eco/api/session/batch/job.rb', line 87 def subjobs @subjobs ||= Eco::API::Session::Batch::Jobs.new( enviro, name: "childs-of:#{name}" ) end |
#summary ⇒ String
if launch was not invoked, it specifies so
Provides a text summary of the current status including:
- stats of the changes introduced by the job in the different parts of the person model
- if the job is compliant with the batch policy
- error messages in case they were errors from the server
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
# File 'lib/eco/api/session/batch/job.rb', line 250 def summary [].tap do |msg| msg << "PENDING Job -------->\n" if pending? ensure_requests! msg << feedback.generate(requests, only_stats: true) if batch_policy && !batch_policy.compliant?(request_stats) msg << 'Batch Policy Uncompliance:' msg << batch_policy.uncompliance(request_stats) end msg << status.errors. if status msg << subjobs_summary end.join("\n") end |
#usecase? ⇒ Boolean
Returns was this batch job generated by a usecase? (Eco::API::UseCases::UseCase).
100 101 102 |
# File 'lib/eco/api/session/batch/job.rb', line 100 def usecase? !!usecase end |