class Aeternitas::Pollable::Dsl

DSL wrapper to conveniently configure pollables

Public Class Methods

new(configuration, &block) click to toggle source

Create a new DSL instance and configure the configuration with the given block @param [Aeternitas::Pollable::Configuration] configuration a pollables configuration @param [Proc] block configuration block

# File lib/aeternitas/pollable/dsl.rb, line 8
def initialize(configuration, &block)
  @configuration = configuration
  instance_eval(&block)
end

Public Instance Methods

after_polling(method) click to toggle source

Configures a method that will be run after every successful poll

@param [Symbol, Block] method method or method name @example method by reference

after:polling :my_method
...
def my_method(pollable) do_something end

@example method by block

after_polling ->(pollable) {do_something}
# File lib/aeternitas/pollable/dsl.rb, line 57
def after_polling(method)
  if method.is_a?(Symbol)
    @configuration.after_polling << ->(pollable) { pollable.send(method) }
  else
    @configuration.after_polling << method
  end
end
before_polling(method) click to toggle source

Configures a method that will be run before every poll

@param [Symbol, Block] method method or method name @example method by reference

before_polling :my_method
...
def my_method(pollable) do_something end

@example method by block

before_polling ->(pollable) {do_something}
# File lib/aeternitas/pollable/dsl.rb, line 40
def before_polling(method)
  if method.is_a?(Symbol)
    @configuration.before_polling << ->(pollable) { pollable.send(method) }
  else
    @configuration.before_polling << method
  end
end
deactivate_on(*error_class) click to toggle source

Configure errors that will cause the pollable instance to be deactivated immediately during poll.

@param [Object] error_class error classes

# File lib/aeternitas/pollable/dsl.rb, line 68
def deactivate_on(*error_class)
  @configuration.deactivation_errors |= error_class
end
guard_key(key) click to toggle source

Configure the guard key. This can be either a fixed String, a method reference or a block

@param [String, Symbol, Proc] key lock key @example using a fixed String

guard_key "MyLockKey"

@example using a method reference

guard_key :url

@example using a block

guard_key ->(pollable) {URI.parse(pollable.url).host}
# File lib/aeternitas/pollable/dsl.rb, line 95
def guard_key(key)
  @configuration.guard_options[:key] = case key
                          when Symbol
                            ->(obj) { return obj.send(key) }
                          when Proc
                            key
                          else
                            ->(obj) { return key.to_s }
                          end
end
guard_options(options) click to toggle source

Configure the guard. @see guard_key @see Aeternitas::Guard @param [Hash] options guard options

# File lib/aeternitas/pollable/dsl.rb, line 110
def guard_options(options)
  @configuration.guard_options.merge!(options)
end
ignore_error(*error_class) click to toggle source

Configure errors that will be wrapped in {Aeternitas::Error::Ignored}. Use this to group exceptions which should be ignored in your exception tracker.

@param [Object] error_class error classes

# File lib/aeternitas/pollable/dsl.rb, line 76
def ignore_error(*error_class)
  @configuration.ignored_errors |= error_class
end
polling_frequency(frequency) click to toggle source

Configures the polling frequency. This can be either the name of a {Aeternitas::PollingFrequency} or a lambda that receives a pollable instance and returns a DateTime

@param [Symbol, Proc] frequency Sets the polling frequency.

representing the next polling time.

@example using a preset

polling_frequency :weekly

@example using a custom block

polling_frequency ->(pollable) {Time.now + 1.month + Time.now - pollable.created_at.to_i / 3.month * 1.month}

@todo allow custom methods via reference

# File lib/aeternitas/pollable/dsl.rb, line 23
def polling_frequency(frequency)
  if frequency.is_a?(Symbol)
    @configuration.polling_frequency = Aeternitas::PollingFrequency.by_name(frequency)
  else
    @configuration.polling_frequency = frequency
  end
end
queue(queue) click to toggle source

Configure the Sidekiq queue into which the instance's poll jobs will be enqueued. @param [String] queue name of the Sidekiq queue

# File lib/aeternitas/pollable/dsl.rb, line 82
def queue(queue)
  @configuration.queue = queue
end
sleep_on_guard_locked(switch) click to toggle source

Configure the behaviour of poll jobs if a lock can't be acquired. When set to true poll jobs (and effectively the Sidekiq worker thread) will sleep until the lock is released if the lock could not be acquired. @param [Boolean] switch true|false

# File lib/aeternitas/pollable/dsl.rb, line 118
def sleep_on_guard_locked(switch)
  @configuration.sleep_on_guard_locked = switch
end