Class: GoodJob::Poller

Inherits:
Object
  • Object
show all
Defined in:
lib/good_job/poller.rb

Overview

Pollers regularly wake up execution threads to check for new work.

Constant Summary collapse

TIMEOUT_INTERVAL =
5
DEFAULT_TIMER_OPTIONS =

Defaults for instance of Concurrent::TimerTask. The timer controls how and when sleeping threads check for new work.

{
  execution_interval: Configuration::DEFAULT_POLL_INTERVAL,
  timeout_interval: TIMEOUT_INTERVAL,
  run_now: true,
}.freeze

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*recipients, poll_interval: nil) ⇒ Poller

Returns a new instance of Poller.

Parameters:

  • recipients (Array<Proc, #call, Array(Object, Symbol)>)
  • poll_interval (Integer, nil) (defaults to: nil)

    number of seconds between polls



38
39
40
41
42
43
44
45
46
47
# File 'lib/good_job/poller.rb', line 38

def initialize(*recipients, poll_interval: nil)
  @recipients = Concurrent::Array.new(recipients)

  @timer_options = DEFAULT_TIMER_OPTIONS.dup
  @timer_options[:execution_interval] = poll_interval if poll_interval.present?

  self.class.instances << self

  create_timer
end

Class Attribute Details

.instancesArray<GoodJob::Poller>? (readonly)

List of all instantiated Pollers in the current process.

Returns:



23
# File 'lib/good_job/poller.rb', line 23

cattr_reader :instances, default: [], instance_reader: false

Instance Attribute Details

#recipientsArray<#call, Array(Object, Symbol)> (readonly)

List of recipients that will receive notifications.

Returns:

  • (Array<#call, Array(Object, Symbol)>)


34
35
36
# File 'lib/good_job/poller.rb', line 34

def recipients
  @recipients
end

Class Method Details

.from_configuration(configuration) ⇒ GoodJob::Poller

Creates GoodJob::Poller from a GoodJob::Configuration instance.

Parameters:

Returns:



28
29
30
# File 'lib/good_job/poller.rb', line 28

def self.from_configuration(configuration)
  GoodJob::Poller.new(poll_interval: configuration.poll_interval)
end

Instance Method Details

#restart(timeout: -1)) ⇒ void

This method returns an undefined value.

Restart the poller. When shutdown, start; or shutdown and start.

Parameters:

  • timeout (Numeric, nil) (defaults to: -1))

    Seconds to wait; shares same values as #shutdown.



82
83
84
85
# File 'lib/good_job/poller.rb', line 82

def restart(timeout: -1)
  shutdown(timeout: timeout) if running?
  create_timer
end

#running?true, ...

Tests whether the timer is running.

Returns:

  • (true, false, nil)


51
# File 'lib/good_job/poller.rb', line 51

delegate :running?, to: :timer, allow_nil: true

#shutdown(timeout: -1)) ⇒ void

This method returns an undefined value.

Shut down the poller. Use #shutdown? to determine whether threads have stopped.

Parameters:

  • timeout (nil, Numeric) (defaults to: -1))

    Seconds to wait for active threads.

    • nil, the scheduler will trigger a shutdown but not wait for it to complete.

    • -1, the scheduler will wait until the shutdown is complete.

    • 0, the scheduler will immediately shutdown and stop any threads.

    • A positive number will wait that many seconds before stopping any remaining active threads.



67
68
69
70
71
72
73
74
75
76
# File 'lib/good_job/poller.rb', line 67

def shutdown(timeout: -1)
  return if timer.nil? || timer.shutdown?

  timer.shutdown if timer.running?

  if timer.shuttingdown? && timeout # rubocop:disable Style/GuardClause
    timer_wait = timeout.negative? ? nil : timeout
    timer.kill unless timer.wait_for_termination(timer_wait)
  end
end

#shutdown?true, ...

Tests whether the timer is shutdown.

Returns:

  • (true, false, nil)


55
56
57
# File 'lib/good_job/poller.rb', line 55

def shutdown?
  timer ? timer.shutdown? : true
end