Method: Deliver::UploadMetadata#retry_if_nil

Defined in:
deliver/lib/deliver/upload_metadata.rb

#retry_if_nil(message) ⇒ Object

Retries a block of code if the return value is nil, with an exponential backoff.



455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
# File 'deliver/lib/deliver/upload_metadata.rb', line 455

def retry_if_nil(message)
  tries = options[:version_check_wait_retry_limit]
  wait_time = 10
  loop do
    tries -= 1

    value = yield
    return value if value

    # Calculate sleep time to be the lesser of the exponential backoff or 5 minutes.
    # This prevents problems with CI's console output timeouts (of usually 10 minutes), and also
    # speeds up the retry time for the user, as waiting longer than 5 minutes is a too long wait for a retry.
    sleep_time = [wait_time * 2, 5 * 60].min
    UI.message("#{message}... Retrying after #{sleep_time} seconds (remaining: #{tries})")
    Kernel.sleep(sleep_time)

    return nil if tries.zero?

    wait_time *= 2 # Double the wait time for the next iteration
  end
end