class Legion::Extensions::Actors::Poll

Public Class Methods

new() click to toggle source
# File lib/legion/extensions/actors/poll.rb, line 10
def initialize # rubocop:disable Metrics/AbcSize
  log.debug "Starting timer for #{self.class} with #{{ execution_interval: time, timeout_interval: timeout, run_now: run_now?, check_subtask: check_subtask? }}"
  @timer = Concurrent::TimerTask.new(execution_interval: time, timeout_interval: timeout, run_now: run_now?) do
    t1 = Time.now
    log.debug "Running #{self.class}"
    old_result = Legion::Cache.get(cache_name)
    log.debug "Cached value for #{self.class}: #{old_result}"
    results = Legion::JSON.load(Legion::JSON.dump(manual))
    Legion::Cache.set(cache_name, results, time * 2)

    unless old_result.nil?
      results[:diff] = Hashdiff.diff(results, old_result, numeric_tolerance: 0.0, array_path: false) do |_path, obj1, obj2|
        if int_percentage_normalize.positive? && obj1.is_a?(Integer) && obj2.is_a?(Integer)
          obj1.between?(obj2 * (1 - int_percentage_normalize), obj2 * (1 + int_percentage_normalize))
        end
      end
      results[:changed] = results[:diff].count.positive?

      Legion::Logging.info results[:diff] if results[:changed]
      Legion::Transport::Messages::CheckSubtask.new(runner_class: runner_class.to_s,
                                                    function:     runner_function,
                                                    result:       results,
                                                    type:         'poll_result',
                                                    polling:      true).publish
    end

    sleep_time = 1 - (Time.now - t1)
    sleep(sleep_time) if sleep_time.positive?
    log.debug("#{self.class} result: #{results}")
    results
  rescue StandardError => e
    Legion::Logging.fatal e.message
    Legion::Logging.fatal e.backtrace
  end
  @timer.execute
rescue StandardError => e
  Legion::Logging.error e.message
  Legion::Logging.error e.backtrace
end

Public Instance Methods

action(_payload = {}) click to toggle source
# File lib/legion/extensions/actors/poll.rb, line 74
def action(_payload = {})
  Legion::Logging.warn 'An extension is using the default block from Legion::Extensions::Runners::Every'
end
cache_name() click to toggle source
# File lib/legion/extensions/actors/poll.rb, line 50
def cache_name
  "#{lex_name}_#{runner_name}"
end
cancel() click to toggle source
# File lib/legion/extensions/actors/poll.rb, line 78
def cancel
  Legion::Logging.debug 'Cancelling Legion Poller'
  @timer.shutdown
rescue StandardError => e
  Legion::Logging.error e.message
end
check_subtask?() click to toggle source
# File lib/legion/extensions/actors/poll.rb, line 66
def check_subtask?
  true
end
int_percentage_normalize() click to toggle source
# File lib/legion/extensions/actors/poll.rb, line 54
def int_percentage_normalize
  0.00
end
run_now?() click to toggle source
# File lib/legion/extensions/actors/poll.rb, line 62
def run_now?
  true
end
time() click to toggle source
# File lib/legion/extensions/actors/poll.rb, line 58
def time
  9
end
timeout() click to toggle source
# File lib/legion/extensions/actors/poll.rb, line 70
def timeout
  5
end