lib/eco/api/session/batch/job.rb in eco-helpers-2.1.8 vs lib/eco/api/session/batch/job.rb in eco-helpers-2.1.9
- old
+ new
@@ -32,11 +32,14 @@
# @param e [Eco::API::Common::Session::Environment] requires a session environment, as any child of `Eco::API::Common::Session::BaseSession`.
# @param name [String] the name of this `batch job`
# @param type [Symbol] a valid batch operation.
# @param usecase [Eco::API::UseCases::UseCase, nil] when provided: `usecase` that generated this `batch job`.
# This is necessary to know the `options` used to run the usecase, which could modify the `Batch::Job` behviour.
- def initialize(e, name:, type:, sets:, usecase: nil)
+ # @param accept_update_with_no_id [Boolean] temporary contingency
+ # This parameter has been added due to a bug on server side.
+ # An external_id is still required.
+ def initialize(e, name:, type:, sets:, usecase: nil, accept_update_with_no_id: false)
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)
@@ -44,10 +47,11 @@
@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
def reset
@queue = []
@@ -242,16 +246,22 @@
# it means that it failed to be created (it doesn't exist on server-side).
# The entry won't be included.
# - The contingency above wouldn't be necessary if the server worked perfectly.
def processed_queue
pre_filtered = @queue.select do |entry|
+ by_pass_filter = false
if unexisting = entry.new? && !entry.id && type != :create
ref = Eco::API::Session::Batch::Feedback.person_ref(entry)
- msg = "Job ('#{name}':#{type}): excluded unexisting entry (failed creation): #{ref}"
+ msg = "Job ('#{name}':#{type}): "
+ if (by_pass_filter = entry.external_id && @accept_update_with_no_id)
+ msg << "entry errored on creation (failed creation) but will try with person code: #{ref}"
+ else
+ msg << "excluded unexisting entry (failed creation): #{ref}"
+ end
session.logger.warn(msg)
end
- !unexisting
+ !unexisting || by_pass_filter
end
pre_filtered.each {|e| @callbacks[e].call(e) if @callbacks.key?(e) }
apply_policies(api_included(pre_filtered)).select do |e|
!as_update(e).empty?
end.select do |e|
@@ -319,12 +329,28 @@
def post_launch(queue: [], simulate: false)
if !simulate && status
status.queue.map do |entry|
if status.success?(entry)
if type == :create && entry.respond_to?(:id=)
- entry.id = status[entry].body["id"]
+ entry.id = status[entry].body["id"].tap do |id|
+ if id.to_s.strip.empty?
+ ref = Eco::API::Session::Batch::Feedback.person_ref(entry)
+ msg = "Entry has been created but API did not return its 'id': #{ref}"
+ if response = status[entry]
+ msg << " (Response - status: #{response.status.code}; body: #{response.body.pretty_inspect})"
+ end
+ logger.error(msg)
+ end
+ end
end
- entry.consolidate! if entry.respond_to?(:consolidate!)
+ if entry.respond_to?(:consolidate!)
+ entry.consolidate!
+ if entry.respond_to?(:dirty?) && entry.dirty? && entry.respond_to?(:as_update)
+ msg = "After consolidate there's still a dirty model:\n"
+ msg << entry.as_update.pretty_inspect
+ logger.debug(msg)
+ end
+ end
#else # do not entry.reset! (keep track on changes still)
end
end
# launch error_handlers
handlers = session.config.error_handlers