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

Inherits:
Object
  • Object
show all
Extended by:
Dsl
Includes:
Base, Fingerprint
Defined in:
lib/legion/extensions/actors/poll.rb

Constant Summary

Constants included from Helpers::Base

Helpers::Base::NAMESPACE_BOUNDARIES

Instance Method Summary collapse

Methods included from Dsl

define_dsl_accessor

Methods included from Fingerprint

#compute_fingerprint, #fingerprint_source, #skip_if_unchanged?, #skip_or_run, #store_fingerprint!, #unchanged?

Methods included from Base

#args, #check_subtask?, #enabled?, #function, #generate_task?, included, #manual, #remote_invocable?, #runner, #use_runner?

Methods included from Helpers::Lex

#default_settings, included, #runner_desc

Methods included from Helpers::Base

#actor_class, #actor_const, #actor_name, #amqp_prefix, #calling_class, #calling_class_array, #from_json, #full_path, #lex_class, #lex_const, #lex_name, #lex_slug, #log_tag, #normalize, #runner_class, #runner_const, #runner_name, #segments, #settings_path, #table_prefix, #to_dotted_hash

Methods included from Helpers::Task

#generate_task_id, #generate_task_log, #task_update

Methods included from Helpers::Logger

#handle_runner_exception

Methods included from Helpers::Secret

reset_identity!, resolve_identity!, #secret

Methods included from Helpers::Core

#find_setting

Constructor Details

#initializePoll

Returns a new instance of Poll.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/legion/extensions/actors/poll.rb', line 21

def initialize
  log.debug "Starting timer for #{self.class} with #{{ execution_interval: time, run_now: run_now?,
check_subtask: check_subtask? }}"
  @executing = Concurrent::AtomicBoolean.new(false)
  @timer = Concurrent::TimerTask.new(execution_interval: time, run_now: run_now?) do
    if @executing.make_true
      begin
        skip_or_run { poll_cycle }
      rescue StandardError => e
        handle_exception(e, level: :fatal)
      ensure
        @executing.make_false
      end
    else
      Legion::Logging.debug "[Poll] skipped (previous still running): #{self.class}"
    end
  end
  @timer.execute
rescue StandardError => e
  handle_exception(e)
end

Instance Method Details

#action(_payload = {}) ⇒ Object



87
88
89
# File 'lib/legion/extensions/actors/poll.rb', line 87

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

#cache_nameObject



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

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

#cancelObject



91
92
93
94
95
96
# File 'lib/legion/extensions/actors/poll.rb', line 91

def cancel
  Legion::Logging.debug 'Cancelling Legion Poller'
  @timer.shutdown
rescue StandardError => e
  handle_exception(e)
end

#int_percentage_normalizeObject



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

def int_percentage_normalize
  0.00
end

#poll_cycleObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/legion/extensions/actors/poll.rb', line 43

def poll_cycle
  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].any?

    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
  handle_exception(e, level: :fatal)
end

#run_now?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/legion/extensions/actors/poll.rb', line 83

def run_now?
  run_now
end