Sha256: 3751a2524cd913192ff9f2be67a97fcfe57e4a8112ebaed1ba782397c829c23f

Contents?: true

Size: 1.55 KB

Versions: 9

Compression:

Stored size: 1.55 KB

Contents

# frozen_string_literal: true

require_relative "../wait_for_action_attempt"

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

9 entries across 9 versions & 1 rubygems

Version Path
seam-2.3.0 lib/seam/helpers/action_attempt.rb
seam-2.2.0 lib/seam/helpers/action_attempt.rb
seam-2.1.0 lib/seam/helpers/action_attempt.rb
seam-2.0.1 lib/seam/helpers/action_attempt.rb
seam-2.0.0 lib/seam/helpers/action_attempt.rb
seam-2.0.0rc0 lib/seam/helpers/action_attempt.rb
seam-2.0.0b5 lib/seam/helpers/action_attempt.rb
seam-2.0.0b4 lib/seam/helpers/action_attempt.rb
seam-2.0.0b3 lib/seam/helpers/action_attempt.rb