Class: HaveAPI::Resources::ActionState::Poll

Inherits:
Action
  • Object
show all
Includes:
Mixin
Defined in:
lib/haveapi/resources/action_state.rb

Constant Summary collapse

MAX_TIMEOUT =
30

Constants inherited from Action

Action::PATH_PARAM_PATTERN

Instance Attribute Summary

Attributes inherited from Action

#current_user, #errors, #flags, #message, #request, #version

Instance Method Summary collapse

Methods included from Mixin

#state_to_hash

Methods inherited from Action

add_pre_authorize_blocks, authorize, #authorized?, build_route, delayed_inherited, describe, example, from_context, inherit_attrs_from_resource, inherited, initialize, #initialize, input, #input, meta, #meta, model_adapter, output, #params, path_param_names, path_params, #pre_exec, #prepare, resolve_path_params, #safe_exec, #safe_output, #set_meta, #v?, #validate!, validate_build

Methods included from Hookable

included

Methods inherited from Common

check_build, has_attr, inherit_attrs

Constructor Details

This class inherits a constructor from HaveAPI::Action

Instance Method Details

#execObject



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/haveapi/resources/action_state.rb', line 104

def exec
  if input[:timeout] > MAX_TIMEOUT
    error!("timeout has to be maximally #{MAX_TIMEOUT}", {}, http_status: 400)
  end

  t = Time.now

  loop do
    state = @context.server.action_state.new(
      current_user,
      id: params[:action_state_id]
    )

    error!('action state not found') unless state.valid?

    if state.finished? || (Time.now - t) >= input[:timeout]
      return state_to_hash(state)

    elsif input[:update_in]
      new_state = state_to_hash(state)

      %i[status current total].each do |v|
        return new_state if input[v] != new_state[v]
      end
    end

    return state_to_hash(state.poll(input)) if state.respond_to?(:poll)

    sleep(1)
  end
end