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

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,
  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



36
37
38
39
40
41
42
43
44
# File 'lib/good_job/poller.rb', line 36

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?

  create_timer
  self.class.instances << self
end

Class Attribute Details

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

List of all instantiated Pollers in the current process.

Returns:



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

cattr_reader :instances, default: Concurrent::Array.new, 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)>)


32
33
34
# File 'lib/good_job/poller.rb', line 32

def recipients
  @recipients
end

Class Method Details

.from_configuration(configuration) ⇒ GoodJob::Poller

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

Parameters:

Returns:



26
27
28
# File 'lib/good_job/poller.rb', line 26

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.



79
80
81
82
# File 'lib/good_job/poller.rb', line 79

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

#running?true, ...

Tests whether the timer is running.

Returns:

  • (true, false, nil)


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

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.



64
65
66
67
68
69
70
71
72
73
# File 'lib/good_job/poller.rb', line 64

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)


52
53
54
# File 'lib/good_job/poller.rb', line 52

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