Class: Aeternitas::Pollable::Dsl

Inherits:
Object
  • Object
show all
Defined in:
lib/aeternitas/pollable/dsl.rb

Overview

DSL wrapper to conveniently configure pollables

Instance Method Summary collapse

Constructor Details

#initialize(configuration, &block) ⇒ Dsl

Create a new DSL instance and configure the configuration with the given block

Parameters:



8
9
10
11
# File 'lib/aeternitas/pollable/dsl.rb', line 8

def initialize(configuration, &block)
  @configuration = configuration
  instance_eval(&block)
end

Instance Method Details

#after_polling(method) ⇒ Object

Configures a method that will be run after every successful poll

Examples:

method by reference

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

method by block

after_polling ->(pollable) {do_something}

Parameters:

  • method (Symbol, Block)

    method or method name



57
58
59
60
61
62
63
# 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) ⇒ Object

Configures a method that will be run before every poll

Examples:

method by reference

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

method by block

before_polling ->(pollable) {do_something}

Parameters:

  • method (Symbol, Block)

    method or method name



40
41
42
43
44
45
46
# 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) ⇒ Object

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

Parameters:

  • error_class (Object)

    error classes



68
69
70
# File 'lib/aeternitas/pollable/dsl.rb', line 68

def deactivate_on(*error_class)
  @configuration.deactivation_errors |= error_class
end

#guard_key(key) ⇒ Object

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

Examples:

using a fixed String

guard_key "MyLockKey"

using a method reference

guard_key :url

using a block

guard_key ->(pollable) {URI.parse(pollable.url).host}

Parameters:

  • key (String, Symbol, Proc)

    lock key



95
96
97
98
99
100
101
102
103
104
# 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) ⇒ Object

Configure the guard.

Parameters:

  • options (Hash)

    guard options

See Also:



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

def guard_options(options)
  @configuration.guard_options.merge!(options)
end

#ignore_error(*error_class) ⇒ Object

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

Parameters:

  • error_class (Object)

    error classes



76
77
78
# File 'lib/aeternitas/pollable/dsl.rb', line 76

def ignore_error(*error_class)
  @configuration.ignored_errors |= error_class
end

#polling_frequency(frequency) ⇒ Object

TODO:

allow custom methods via reference

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

Examples:

using a preset

polling_frequency :weekly

using a custom block

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

Parameters:

  • frequency (Symbol, Proc)

    Sets the polling frequency. representing the next polling time.



23
24
25
26
27
28
29
# 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) ⇒ Object

Configure the Sidekiq queue into which the instance’s poll jobs will be enqueued.

Parameters:

  • queue (String)

    name of the Sidekiq queue



82
83
84
# File 'lib/aeternitas/pollable/dsl.rb', line 82

def queue(queue)
  @configuration.queue = queue
end

#sleep_on_guard_locked(switch) ⇒ Object

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.

Parameters:

  • switch (Boolean)

    true|false



118
119
120
# File 'lib/aeternitas/pollable/dsl.rb', line 118

def sleep_on_guard_locked(switch)
  @configuration.sleep_on_guard_locked = switch
end