Sha256: 62e27142ddef4b6f8ac0b2545e7cec7da09061d70ebac7ae358dcf4e02313bca

Contents?: true

Size: 1.51 KB

Versions: 3

Compression:

Stored size: 1.51 KB

Contents

# frozen_string_literal: true

module Seam
  module Helpers
    module ActionAttempt
      def self.decide_and_wait(action_attempt, client, wait_for_action_attempt)
        if wait_for_action_attempt == true
          return wait_until_finished(action_attempt, client)
        elsif wait_for_action_attempt.is_a?(Hash)
          return wait_until_finished(action_attempt, client, timeout: wait_for_action_attempt[:timeout],
            polling_interval: wait_for_action_attempt[:polling_interval])
        end

        action_attempt
      end

      def self.wait_until_finished(action_attempt, client, timeout: nil, polling_interval: nil)
        timeout = timeout.nil? ? 5.0 : timeout
        polling_interval = polling_interval.nil? ? 0.5 : polling_interval

        time_waiting = 0.0

        while action_attempt.status == "pending"
          sleep(polling_interval)
          time_waiting += polling_interval

          raise Seam::ActionAttemptTimeoutError.new(action_attempt, timeout) if time_waiting > timeout

          action_attempt = update_action_attempt(action_attempt, client)
        end

        raise Seam::ActionAttemptFailedError.new(action_attempt) if action_attempt.status == "error"

        action_attempt
      end

      def self.update_action_attempt(action_attempt, client)
        response = client.get("/action_attempts/get", {action_attempt_id: action_attempt.action_attempt_id})

        action_attempt.update_from_response(response.body["action_attempt"])
        action_attempt
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
seam-2.0.0b2 lib/seam/helpers/action_attempt.rb
seam-2.0.0b1 lib/seam/helpers/action_attempt.rb
seam-2.0.0b0 lib/seam/helpers/action_attempt.rb