Class: Legion::Extensions::Actors::Poll

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/legion/extensions/actors/poll.rb

Instance Method Summary collapse

Methods included from Base

#args, #enabled?, #function, #generate_task?, #manual, #runner, #use_runner?

Methods included from Helpers::Lex

#default_settings, included

Methods included from Helpers::Logger

#handle_exception, #log

Methods included from Helpers::Core

#find_setting, #settings

Methods included from Helpers::Base

#actor_class, #actor_const, #actor_name, #calling_class, #calling_class_array, #from_json, #full_path, #lex_class, #lex_const, #lex_name, #normalize, #runner_class, #runner_const, #runner_name, #to_dotted_hash

Constructor Details

#initializePoll

rubocop:disable Metrics/AbcSize



11
12
13
14
15
16
17
18
19
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
# File 'lib/legion/extensions/actors/poll.rb', line 11

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

Instance Method Details

#action(_payload = {}) ⇒ Object



75
76
77
# File 'lib/legion/extensions/actors/poll.rb', line 75

def action(_payload = {})
  Legion::Logging.warn 'An extension is using the default block from Legion::Extensions::Runners::Every'
end

#cache_nameObject



51
52
53
# File 'lib/legion/extensions/actors/poll.rb', line 51

def cache_name
  "#{lex_name}_#{runner_name}"
end

#cancelObject



79
80
81
82
83
84
# File 'lib/legion/extensions/actors/poll.rb', line 79

def cancel
  Legion::Logging.debug 'Cancelling Legion Poller'
  @timer.shutdown
rescue StandardError => e
  Legion::Logging.error e.message
end

#check_subtask?Boolean



67
68
69
# File 'lib/legion/extensions/actors/poll.rb', line 67

def check_subtask?
  true
end

#int_percentage_normalizeObject



55
56
57
# File 'lib/legion/extensions/actors/poll.rb', line 55

def int_percentage_normalize
  0.00
end

#run_now?Boolean



63
64
65
# File 'lib/legion/extensions/actors/poll.rb', line 63

def run_now?
  true
end

#timeObject



59
60
61
# File 'lib/legion/extensions/actors/poll.rb', line 59

def time
  9
end

#timeoutObject



71
72
73
# File 'lib/legion/extensions/actors/poll.rb', line 71

def timeout
  5
end