lib/ecoportal/api/v1/people.rb in ecoportal-api-0.10.1 vs lib/ecoportal/api/v1/people.rb in ecoportal-api-0.10.2

- old
+ new

@@ -4,13 +4,13 @@ # @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::TimeOut include Enumerable - JOB_TIMEOUT = 240 DELAY_STATUS_CHECK = 5 class_resolver :person_class, "Ecoportal::API::V1::Person" attr_reader :client @@ -167,19 +167,24 @@ logger: client.logger ) yield operation - job_id = create_job(operation) - status = wait_for_job_completion(job_id) + total = operation.count + timeout = timeout_for(total) - if status&.complete? + 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. " - msg << "Probably timeout after #{JOB_TIMEOUT} seconds. " - msg << "Current status: #{status}" + 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 end @@ -189,43 +194,46 @@ person_class.new end private - JobStatus = Struct.new(:id, :complete?, :errored?, :progress) - 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["id"], - body["complete"], - body["errored"], - body["progress"] - ) + 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) + 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? - break status if Time.now >= before + JOB_TIMEOUT + 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