Module: Aeternitas::Pollable

Extended by:
ActiveSupport::Concern
Defined in:
lib/aeternitas/pollable.rb,
lib/aeternitas/pollable/dsl.rb,
lib/aeternitas/pollable/configuration.rb

Overview

Note:

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.

Examples:

class MyWebsitePollable < ActiveRecord::Base
  includes Aeternitas::Pollable

  polling_options do
    polling_frequency :daily
    lock_key ->(obj) {obj.url}
  end

  def poll
    response = HTTParty.get(self.url)
    raise StandardError, "#{self.url} responded with #{response.status}" unless response.success?
    HttpSource.create!(content: response.parsed_response)
  end
end

Defined Under Namespace

Classes: Configuration, Dsl

Instance Method Summary collapse

Instance Method Details

#add_source(raw_content) ⇒ Aeternitas::Source

Creates a new source with the given content if it does not exist

Examples:

#...
def poll
  response = HTTParty.get("http://example.com")
  add_source(response.parsed_response)
end
#...

Parameters:

  • raw_content (String)

    the sources raw content

Returns:



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_pollObject

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

#guardObject



99
100
101
102
103
104
# File 'lib/aeternitas/pollable.rb', line 99

def guard
  guard_key = pollable_configuration.guard_options[:key].call(self)
  guard_timeout = pollable_configuration.guard_options[:timeout]
  guard_cooldown = pollable_configuration.guard_options[:cooldown]
  Aeternitas::Guard.new(guard_key, guard_cooldown, guard_timeout)
end

#pollObject

This method is abstract.

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.

Raises:

  • (NotImplementedError)


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_configurationAeternitas::Pollable::Configuration

Access the Pollables configuration

Returns:



109
110
111
# File 'lib/aeternitas/pollable.rb', line 109

def pollable_configuration
  self.class.pollable_configuration
end

#register_pollableObject

Note:

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