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
124 125 126 127 128 129 130 |
# File 'lib/aeternitas/pollable.rb', line 124 def add_source(raw_content) source = 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 77 78 79 |
# File 'lib/aeternitas/pollable.rb', line 51 def execute_poll _before_poll begin guard.with_lock { poll } rescue Aeternitas::Guard::GuardIsLocked # Do not transition to the 'errored' state for a guard lock. raise rescue => 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 => e begin log_poll_error(e) ensure raise e end end |
#guard ⇒ Object
100 101 102 103 104 105 |
# File 'lib/aeternitas/pollable.rb', line 100 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.
85 86 87 |
# File 'lib/aeternitas/pollable.rb', line 85 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
110 111 112 |
# File 'lib/aeternitas/pollable.rb', line 110 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.
93 94 95 96 97 98 |
# File 'lib/aeternitas/pollable.rb', line 93 def register_pollable self. ||= ( state: "waiting", pollable_class: self.class.name ) end |