Class: Eco::API::Session::Batch::Job
- Inherits:
-
Common::Session::BaseSession
- Object
- Common::Session::BaseSession
- Eco::API::Session::Batch::Job
- Defined in:
- lib/eco/api/session/batch/job.rb
Class Attribute Summary collapse
-
.sets ⇒ Object
readonly
Returns the value of attribute sets.
-
.types ⇒ Object
readonly
Returns the value of attribute types.
Instance Attribute Summary collapse
-
#feedback ⇒ Eco::API::Session::Batch::Feedback
readonly
helper class for feedback and 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
#api, #config, #environment, #file_manager, #logger, #session
Class Method Summary collapse
Instance Method Summary collapse
-
#add(entry, unique: true) {|person| ... } ⇒ Void
Adds an entry(ies) to the job queue.
-
#dup(name = "ad-hoc:job-from:#{self.name}", usecase: nil) ⇒ Eco::API::Session::Batch::Job
Creates an empty
Batch::Jobwith same behaviour as the current one. -
#error_handler? ⇒ Boolean
trueif the current batch job is a result of an error_handler. -
#errors? ⇒ Boolean
trueif there were Server errors,falseotherwise. -
#initialize(e, name:, type:, sets:, usecase: nil) ⇒ 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
-
#summary ⇒ String
Provides a text summary of the current status.
-
#usecase? ⇒ Boolean
Was this
batch jobgenerated by ausecase? (Eco::API::UseCases::UseCase).
Methods inherited from Common::Session::BaseSession
#enviro=, #fatal, #fm, #mailer, #mailer?, #s3uploader, #s3uploader?, #sftp, #sftp?
Constructor Details
#initialize(e, name:, type:, sets:, usecase: nil) ⇒ Job
Returns a new instance of Job.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/eco/api/session/batch/job.rb', line 36 def initialize(e, name:, type:, sets:, usecase: nil) raise "A name is required to refer a job. Given: #{name}" if !name raise "Type should be one of #{self.class.types}. Given: #{type}" unless self.class.valid_type?(type) raise "Sets should be some of #{self.class.sets}. Given: #{sets}" unless self.class.valid_sets?(sets) raise "usecase must be a Eco::API::UseCases::UseCase object. Given: #{usecase.class}" if usecase && !usecase.is_a?(Eco::API::UseCases::UseCase) super(e) @name = name @type = type @sets = [sets].flatten.compact @usecase = usecase @feedback = Eco::API::Session::Batch::Feedback.new(job: self) reset end |
Class Attribute Details
.sets ⇒ Object (readonly)
Returns the value of attribute sets.
16 17 18 |
# File 'lib/eco/api/session/batch/job.rb', line 16 def sets @sets end |
.types ⇒ Object (readonly)
Returns the value of attribute types.
16 17 18 |
# File 'lib/eco/api/session/batch/job.rb', line 16 def types @types end |
Instance Attribute Details
#feedback ⇒ Eco::API::Session::Batch::Feedback (readonly)
helper class for feedback and decision making
11 12 13 |
# File 'lib/eco/api/session/batch/job.rb', line 11 def feedback @feedback end |
#name ⇒ String (readonly)
the name of this batch job
11 12 13 |
# File 'lib/eco/api/session/batch/job.rb', line 11 def name @name end |
#sets ⇒ Array<Symbol> (readonly)
the parts of the person model this batch is supposed to affect
11 12 13 |
# File 'lib/eco/api/session/batch/job.rb', line 11 def sets @sets end |
#status ⇒ Eco::API::Session::Batch::Status (readonly)
if launched: the status of the batch
11 12 13 |
# File 'lib/eco/api/session/batch/job.rb', line 11 def status @status end |
#type ⇒ Symbol (readonly)
a valid batch operation
11 12 13 |
# File 'lib/eco/api/session/batch/job.rb', line 11 def type @type end |
#usecase ⇒ Eco::API::UseCases::UseCase? (readonly)
when provided: usecase that generated this batch job
11 12 13 |
# File 'lib/eco/api/session/batch/job.rb', line 11 def usecase @usecase end |
Class Method Details
.valid_sets?(value) ⇒ Boolean
22 23 24 25 |
# File 'lib/eco/api/session/batch/job.rb', line 22 def valid_sets?(value) sts = [value].flatten sts.all? { |s| sets.include?(s) } end |
.valid_type?(value) ⇒ Boolean
18 19 20 |
# File 'lib/eco/api/session/batch/job.rb', line 18 def valid_type?(value) types.include?(value) end |
Instance Method Details
#add(entry, unique: true) {|person| ... } ⇒ Void
Adds an entry(ies) to the job queue.
89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/eco/api/session/batch/job.rb', line 89 def add(entry, unique: true, &block) case entry when Enumerable entry.each {|e| add(e, unique: unique, &block)} else unless !entry unless unique && @queue_hash.key?(entry) @queue_hash[entry] = true @queue.push(entry) @callbacks[entry] = Proc.new if block_given? end end end end |
#dup(name = "ad-hoc:job-from:#{self.name}", usecase: nil) ⇒ 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
64 65 66 |
# File 'lib/eco/api/session/batch/job.rb', line 64 def dup(name = "ad-hoc:job-from:#{self.name}", usecase: nil) self.class.new(enviro, name: name, type: type, sets: sets, usecase: usecase) end |
#error_handler? ⇒ Boolean
Returns true if the current batch job is a result of an error_handler.
69 70 71 |
# File 'lib/eco/api/session/batch/job.rb', line 69 def error_handler? usecase? && usecase.is_a?(Eco::API::Error::Handler) end |
#errors? ⇒ Boolean
Returns true if there were Server errors, false otherwise.
129 130 131 |
# File 'lib/eco/api/session/batch/job.rb', line 129 def errors? status && 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 (seeEco::API::Session::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 (seeEco::API::Session::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::RequestStats) out of the requests - if there is a batch policy declared for the current job
type, it checks compliance againststats(Eco::API::Session::Batch::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(seeEco::API::Session::Batch::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
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/eco/api/session/batch/job.rb', line 163 def launch(simulate: false) pqueue = processed_queue @requests = pqueue.map {|e| as_update(e)} pre_checks(requests, simulate: simulate) unless simulate if pqueue.length > 0 backup_update(requests) session.batch.launch(pqueue, method: type).tap do |job_status| @status = job_status status.root = self status.errors.print end end end unless requests.empty? logger.info("--- simulate mode (dry-run) -- job '#{name}' -- this would have launched #{type.to_s.upcase}") if simulate end post_launch(queue: pqueue, simulate: simulate) @pending = false return status end |
#options ⇒ Hash
Returns options the root usecase is run with.
79 80 81 |
# File 'lib/eco/api/session/batch/job.rb', line 79 def usecase?? usecase. : {} end |
#pending? ⇒ Boolean
Returns has been this batch job launched?.
110 111 112 |
# File 'lib/eco/api/session/batch/job.rb', line 110 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
136 137 138 |
# File 'lib/eco/api/session/batch/job.rb', line 136 def people(input = @queue) Eco::API::Organization::People.new(input) end |
#request_stats(requests = nil) ⇒ Object
123 124 125 |
# File 'lib/eco/api/session/batch/job.rb', line 123 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.
117 118 119 120 |
# File 'lib/eco/api/session/batch/job.rb', line 117 def requests raise "Method missuse. Firstly 'launch' should be invoked" unless instance_variable_defined?(:@requests) @requests end |
#reset ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/eco/api/session/batch/job.rb', line 51 def reset @queue = [] @queue_hash = {} @callbacks = {} @pending = true @status = nil end |
#summary ⇒ String
if launch was not invoked, it specifies so
Provides a text summary of the current status
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/eco/api/session/batch/job.rb', line 192 def summary [].tap do |msg| if pending? msg << "PENDING - Batch #{type.to_s.upcase} - job '#{name}' - length: #{@queue.length}" else 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. unless !status end end.join("\n") end |
#usecase? ⇒ Boolean
Returns was this batch job generated by a usecase? (Eco::API::UseCases::UseCase).
74 75 76 |
# File 'lib/eco/api/session/batch/job.rb', line 74 def usecase? !!usecase end |