lib/flydata/helper/action/check_remote_actions.rb in flydata-0.7.19 vs lib/flydata/helper/action/check_remote_actions.rb in flydata-0.8.0

- old
+ new

@@ -1,8 +1,9 @@ require 'flydata/util/file_util' require 'flydata/api_client' require 'flydata/helper/base_action' +require 'rest_client' module Flydata module Helper module Action class CheckRemoteActions < BaseAction @@ -23,25 +24,31 @@ 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}'. Wait for the next turn." + 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 = { id: action_id, config: action['config'] } + 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 @@ -49,9 +56,23 @@ action_position.save(last_id) true # for debug else false # for debug end + end + + 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 end class ActionPosition include Util::FileUtil