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, #function_desc, #function_example, #function_options, #function_set, included, #runner_desc

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



10
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
# 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

Instance Method Details

#action(_payload = {}) ⇒ Object



74
75
76
# 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_nameObject



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

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

#cancelObject



78
79
80
81
82
83
# 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?Boolean

Returns:

  • (Boolean)


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

def check_subtask?
  true
end

#int_percentage_normalizeObject



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

def int_percentage_normalize
  0.00
end

#run_now?Boolean

Returns:

  • (Boolean)


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

def run_now?
  true
end

#timeObject



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

def time
  9
end

#timeoutObject



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

def timeout
  5
end