Sha256: 6b8a2ae607dc8ab315db90e33e48c8363f5835abb420daea50e702ebdcc065fa

Contents?: true

Size: 1.85 KB

Versions: 35

Compression:

Stored size: 1.85 KB

Contents

require 'flydata/util/file_util'
require 'flydata/api_client'
require 'flydata/helper/base_action'

module Flydata
  module Helper
    module Action
      class CheckRemoteActions < BaseAction
        include FlydataCore::Logger

        MAX_ERROR_RETRY = 20

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

        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 => 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."
               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'] }
            # 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
      end

      class ActionPosition
        include Util::FileUtil

        def initialize(config)
          @path = config.helper_action_position_path
        end
        def save(position)
          write_line(@path, position.to_s)
        end
        def load
          read_line(@path).to_i
        end
      end
    end
  end
end

Version data entries

35 entries across 35 versions & 1 rubygems

Version Path
flydata-0.7.19 lib/flydata/helper/action/check_remote_actions.rb
flydata-0.7.18 lib/flydata/helper/action/check_remote_actions.rb
flydata-0.7.17 lib/flydata/helper/action/check_remote_actions.rb
flydata-0.7.16 lib/flydata/helper/action/check_remote_actions.rb
flydata-0.7.15 lib/flydata/helper/action/check_remote_actions.rb
flydata-0.7.14 lib/flydata/helper/action/check_remote_actions.rb
flydata-0.7.13 lib/flydata/helper/action/check_remote_actions.rb
flydata-0.7.12 lib/flydata/helper/action/check_remote_actions.rb
flydata-0.7.11 lib/flydata/helper/action/check_remote_actions.rb
flydata-0.7.10 lib/flydata/helper/action/check_remote_actions.rb
flydata-0.7.9 lib/flydata/helper/action/check_remote_actions.rb
flydata-0.7.8 lib/flydata/helper/action/check_remote_actions.rb
flydata-0.7.7 lib/flydata/helper/action/check_remote_actions.rb
flydata-0.7.6 lib/flydata/helper/action/check_remote_actions.rb
flydata-0.7.5 lib/flydata/helper/action/check_remote_actions.rb
flydata-0.7.4 lib/flydata/helper/action/check_remote_actions.rb
flydata-0.7.2.1 lib/flydata/helper/action/check_remote_actions.rb
flydata-0.7.2 lib/flydata/helper/action/check_remote_actions.rb
flydata-0.7.1 lib/flydata/helper/action/check_remote_actions.rb
flydata-0.7.0 lib/flydata/helper/action/check_remote_actions.rb