21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/ecf_dgii/polling.rb', line 21
def self.poll_until_complete(options = nil)
opts = options || PollingOptions.new
delay = opts.initial_delay
retries = 0
start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
loop do
result = yield
progress = nil
if result.respond_to?(:progress)
progress = result.progress
elsif result.is_a?(Hash)
progress = result[:progress] || result["progress"]
end
progress_value = progress.respond_to?(:value) ? progress.value : progress.to_s
return result if TERMINAL_PROGRESS.include?(progress_value)
elapsed = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time
if opts.timeout && opts.timeout > 0 && elapsed >= opts.timeout
raise PollingTimeoutError, "El polling excedió el tiempo límite de #{opts.timeout}s (último progreso: #{progress_value})"
end
retries += 1
if opts.max_retries && opts.max_retries > 0 && retries >= opts.max_retries
raise PollingMaxRetriesError, "El polling excedió el máximo de #{opts.max_retries} intentos (último progreso: #{progress_value})"
end
sleep(delay)
delay = [delay * opts.backoff_multiplier, opts.max_delay].min
end
end
|