lib/ecoportal/api/v1/people.rb in ecoportal-api-0.10.3 vs lib/ecoportal/api/v1/people.rb in ecoportal-api-0.10.4
- old
+ new
@@ -4,15 +4,12 @@
# @attr_reader client [Common::Client] a `Common::Client` object that
# holds the configuration of the api connection.
class People
extend Common::BaseClass
include Common::DocHelpers
- include Common::Client::TimeOut
include Enumerable
- DELAY_STATUS_CHECK = 5
-
class_resolver :person_class, "Ecoportal::API::V1::Person"
attr_reader :client
# @param client [Common::Client] a `Common::Client` object that holds the configuration of the api connection.
@@ -141,11 +138,11 @@
# @yield [batch_op] adds multiple api requests for the current batch.
# @yieldparam batch_op [BatchOperation]
# @param job_mode [Boolean] whether or not it should use batch jobs
# @return [Ecoportal::API::Common::Response] the results of the batch
def batch(job_mode: true, &block)
- return job(&block) if job_mode
+ return job.batch(&block) if job_mode
operation = Common::BatchOperation.new(
"/people",
person_class,
logger: client.logger
@@ -157,100 +154,25 @@
client.post("/people/batch", data: operation.as_json).tap do |response|
operation.process_response(response)
end
end
- # @return [Ecoportal::API::Common::Response] the results of the batch job
- def job
- operation = Common::BatchOperation.new(
- "/people",
- person_class,
- logger: client.logger
- )
+ # @return [Ecoportal::API::V1::Job]
+ def job(preserve_stats: true)
+ return @last_job = Job.new(client, person_class: person_class) unless preserve_stats && @last_job
- yield operation
-
- total = operation.count
- timeout = timeout_for(total)
-
- job_id = create_job(operation)
- status = wait_for_job_completion(job_id, timeout: timeout, total: total)
-
- # @todo
- # if total == status.progress
- if status&.complete?(total)
- job_result(job_id, operation)
- else
- msg = "Job '#{job_id}' not complete (size: #{total}).\n"
- msg << " Probably timeout after #{timeout} seconds.\n"
- msg << " Current status: #{status}"
-
- raise API::Errors::TimeOut, msg
- end
+ @last_job = @last_job.new
end
# Creates a new `Person` object.
# @return [Person] new empty person object of the current version.
def new
person_class.new
end
private
- def job_status(job_id)
- response = client.get("/people/job/#{CGI.escape(job_id)}/status")
- body = response && body_data(response.body)
-
- msg = "Status error (#{response.status}) - "
- msg << "Errors: #{body}"
- raise msg unless response.success?
-
- JobStatus.new(*body.values_at(*%w[id complete errored progress]))
- end
-
- # @return [Ecoportal::API::Common::Response] the results of the batch job
- def job_result(job_id, operation)
- client.get("/people/job/#{CGI.escape(job_id)}").tap do |response|
- operation.process_response(response)
- end
- end
-
- def wait_for_job_completion(job_id, timeout:, total:)
- # timeout library is evil. So we make poor-man timeout.
- # https://jvns.ca/blog/2015/11/27/why-rubys-timeout-is-dangerous-and-thread-dot-raise-is-terrifying/
- before = Time.now
-
- loop do
- status = job_status(job_id)
- break status if status.complete?(total)
-
- left = (before + timeout) - Time.now
- break status unless left.positive?
- # break status if Time.now >= before + timeout
-
- msg = " ... Await job "
- msg << "('#{job_id}'; done: #{status.progress}): "
- msg << "#{left.ceil} sec. \r"
-
- print msg
- $stdout.flush
-
- sleep(DELAY_STATUS_CHECK)
- status
- end
- end
-
- # @return [String] the `id` of the created batch job
- def create_job(operation)
- job_id = nil
- client.post("/people/job", data: operation.as_json).tap do |response|
- job_id = body_data(response.body)["id"] if response.success?
- raise "Could not create job - Error (#{response.status}): #{body_data(response.body)}" unless job_id
- end
- job_id
- end
-
# Hook for other api versions to obtain the raw data of a response
# @note this was introduced to allow `v2` to reuse this class
def body_data(body)
body
end
@@ -260,5 +182,6 @@
end
require 'ecoportal/api/v1/schema_field_value'
require 'ecoportal/api/v1/person_details'
require 'ecoportal/api/v1/person'
+require 'ecoportal/api/v1/job'