Module: Aeternitas::Pollable
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/aeternitas/pollable.rb,
lib/aeternitas/pollable/dsl.rb,
lib/aeternitas/pollable/configuration.rb
Overview
Can only be used by classes inheriting from ActiveRecord::Base
Mixin that enables the frequent polling of the receiving class. Classes including this method must implement the .poll method. Polling behaviour can be configured via pollable_options.
Defined Under Namespace
Classes: Configuration, Dsl
Instance Method Summary collapse
-
#add_source(raw_content) ⇒ Aeternitas::Source
Creates a new source with the given content if it does not exist.
-
#execute_poll ⇒ Object
This method runs the polling workflow.
- #guard ⇒ Object
-
#poll ⇒ Object
abstract
This method implements the class specific polling behaviour.
-
#pollable_configuration ⇒ Aeternitas::Pollable::Configuration
Access the Pollables configuration.
-
#register_pollable ⇒ Object
Registers the instance as pollable.
Instance Method Details
#add_source(raw_content) ⇒ Aeternitas::Source
Creates a new source with the given content if it does not exist
123 124 125 126 127 128 129 |
# File 'lib/aeternitas/pollable.rb', line 123 def add_source(raw_content) source = self.sources.create(raw_content: raw_content) return nil unless source.persisted? Aeternitas::Metrics.log(:sources_created, self.class) source end |
#execute_poll ⇒ Object
This method runs the polling workflow
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/aeternitas/pollable.rb', line 51 def execute_poll _before_poll begin guard.with_lock { poll } rescue StandardError => e if pollable_configuration.deactivation_errors.include?(e.class) disable_polling(e) return false elsif pollable_configuration.ignored_errors.include?(e.class) .has_errored! raise Aeternitas::Errors::Ignored, e else .has_errored! raise e end end _after_poll rescue StandardError => e begin log_poll_error(e) ensure raise e end end |
#guard ⇒ Object
99 100 101 102 103 104 |
# File 'lib/aeternitas/pollable.rb', line 99 def guard guard_key = pollable_configuration.[:key].call(self) guard_timeout = pollable_configuration.[:timeout] guard_cooldown = pollable_configuration.[:cooldown] Aeternitas::Guard.new(guard_key, guard_cooldown, guard_timeout) end |
#poll ⇒ Object
This method must be implemented when Aeternitas::Pollable is included
This method implements the class specific polling behaviour. It is only called after the lock was acquired successfully.
84 85 86 |
# File 'lib/aeternitas/pollable.rb', line 84 def poll raise NotImplementedError, "#{self.class.name} does not implement #poll, required by Aeternitas::Pollable" end |
#pollable_configuration ⇒ Aeternitas::Pollable::Configuration
Access the Pollables configuration
109 110 111 |
# File 'lib/aeternitas/pollable.rb', line 109 def pollable_configuration self.class.pollable_configuration end |
#register_pollable ⇒ Object
Manual registration is only needed if the object was created before Aeternitas::Pollable was included. Otherwise it is done automatically after creation.
Registers the instance as pollable.
92 93 94 95 96 97 |
# File 'lib/aeternitas/pollable.rb', line 92 def register_pollable self. ||= ( state: 'waiting', pollable_class: self.class.name ) end |