Class: Flydata::Helper::Action::CheckRemoteActions

Inherits:
BaseAction
  • Object
show all
Includes:
FlydataCore::Logger
Defined in:
lib/flydata/helper/action/check_remote_actions.rb

Constant Summary collapse

MAX_ERROR_RETRY =
20

Instance Attribute Summary

Attributes inherited from BaseAction

#config

Instance Method Summary collapse

Methods inherited from BaseAction

#config_hash

Constructor Details

#initialize(config) ⇒ CheckRemoteActions

Returns a new instance of CheckRemoteActions.



14
15
16
17
18
# File 'lib/flydata/helper/action/check_remote_actions.rb', line 14

def initialize(config)
  super
  @api_client = ApiClient.instance
  @retry_count = 0
end

Instance Method Details

#build_action_info(id = nil, action_config = nil) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/flydata/helper/action/check_remote_actions.rb', line 63

def build_action_info(id = nil, action_config = nil)
  action_info = {}
  action_info[:id] = id if id
  action_info[:config] = action_config

  if action_config.kind_of?(Hash)
    action_info[:config_hash] = action_config
  elsif !action_config.to_s.strip.empty?
    action_info[:config_hash] = JSON.parse(action_config.to_s, symbolize_names: true) rescue nil
  end

  action_info
end

#execute(opts = {}) ⇒ Object



20
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
55
56
57
58
59
60
61
# File 'lib/flydata/helper/action/check_remote_actions.rb', line 20

def execute(opts = {})
  action_position = ActionPosition.new(config[:helper])

  # Get actions from flydata web server
  last_id = action_position.load
  actions = nil
  begin
    actions = @api_client.agent.actions(last_id)
    @retry_count = 0
  rescue RestClient::Unauthorized => e
    log_warn "Stopping agent and helper process due to authentication error. Application was deleted. error:#{e}"
    yield :stop_agent, nil
    yield :stop_helper, nil
    return false
  rescue => e
    if @retry_count >= MAX_ERROR_RETRY
      @retry_count = 0
      raise
    else
       @retry_count += 1
       log_warn "Failed to get actions with error '#{e.to_s}'(#{e.class}). Wait for the next turn."
       actions = {'actions' => []}
    end
  end

  actions['actions'].each do |action|
    action_name = action['name']
    action_id = action['id'].to_i
    action_info = build_action_info(action_id, action['config'])

    # Request action
    yield action_name.to_sym, action_info
    last_id = action_id if action_id > last_id
  end

  if last_id > 0
    action_position.save(last_id)
    true   # for debug
  else
    false  # for debug
  end
end