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,
  timeout_interval: 1,
  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<#call, Array(Object, Symbol)>)
  • poll_interval (Hash) (defaults to: nil)

    number of seconds between polls



32
33
34
35
36
37
38
39
40
41
# File 'lib/good_job/poller.rb', line 32

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_pool
end

Class Attribute Details

.instancesarray<GoodJob:Poller> (readonly)

List of all instantiated Pollers in the current process.

Returns:

  • (array<GoodJob:Poller>)


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

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


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

def recipients
  @recipients
end

Class Method Details

.from_configuration(configuration) ⇒ Object



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

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

Instance Method Details

#restart(wait: true) ⇒ void

This method returns an undefined value.

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

Parameters:

  • wait (Boolean) (defaults to: true)

    Wait for background thread to finish



66
67
68
69
# File 'lib/good_job/poller.rb', line 66

def restart(wait: true)
  shutdown(wait: wait)
  create_pool
end

#shutdown(wait: true) ⇒ void

This method returns an undefined value.

Shut down the poller. If wait is true, the poller will wait for background thread to shutdown. If wait is false, this method will return immediately even though threads may still be running. Use #shutdown? to determine whether threads have stopped.

Parameters:

  • wait (Boolean) (defaults to: true)

    Wait for actively executing threads to finish



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

def shutdown(wait: true)
  return unless @timer&.running?

  @timer.shutdown
  @timer.wait_for_termination if wait
end

#shutdown?true, ...

Tests whether the poller is shutdown.

Returns:

  • (true, false, nil)


58
59
60
# File 'lib/good_job/poller.rb', line 58

def shutdown?
  !@timer&.running?
end