Class: GoodJob::Notifier
- Inherits:
-
Object
- Object
- GoodJob::Notifier
- Defined in:
- lib/good_job/notifier.rb
Overview
Notifiers hook into Postgres LISTEN/NOTIFY functionality to emit and listen for notifications across processes.
Notifiers can emit NOTIFY messages through Postgres. A notifier will LISTEN for messages by creating a background thread that runs in an instance of Concurrent::ThreadPoolExecutor. When a message is received, the notifier passes the message to each of its recipients.
Constant Summary collapse
- AdapterCannotListenError =
Raised if the Database adapter does not implement LISTEN.
Class.new(StandardError)
- CHANNEL =
Default Postgres channel for LISTEN/NOTIFY
'good_job'- EXECUTOR_OPTIONS =
Defaults for instance of Concurrent::ThreadPoolExecutor
{ name: name, min_threads: 0, max_threads: 1, auto_terminate: true, idletime: 60, max_queue: 1, fallback_policy: :discard, }.freeze
- RECONNECT_INTERVAL =
Seconds to wait if database cannot be connected to
5- WAIT_INTERVAL =
Seconds to block while LISTENing for a message
1
Class Attribute Summary collapse
-
.instances ⇒ Array<GoodJob::Notifier>?
readonly
List of all instantiated Notifiers in the current process.
Instance Attribute Summary collapse
-
#recipients ⇒ Array<#call, Array(Object, Symbol)>
readonly
List of recipients that will receive notifications.
Class Method Summary collapse
-
.notify(message) ⇒ Object
Send a message via Postgres NOTIFY.
Instance Method Summary collapse
-
#initialize(*recipients) ⇒ Notifier
constructor
A new instance of Notifier.
-
#listening? ⇒ true, ...
Tests whether the notifier is active and listening for new messages.
-
#restart(timeout: -1)) ⇒ void
Restart the notifier.
-
#running? ⇒ true, ...
Tests whether the notifier is running.
-
#shutdown(timeout: -1)) ⇒ void
Shut down the notifier.
-
#shutdown? ⇒ true, ...
Tests whether the scheduler is shutdown.
Constructor Details
#initialize(*recipients) ⇒ Notifier
Returns a new instance of Notifier.
53 54 55 56 57 58 59 60 61 |
# File 'lib/good_job/notifier.rb', line 53 def initialize(*recipients) @recipients = Concurrent::Array.new(recipients) @listening = Concurrent::AtomicBoolean.new(false) self.class.instances << self create_executor listen end |
Class Attribute Details
.instances ⇒ Array<GoodJob::Notifier>? (readonly)
List of all instantiated Notifiers in the current process.
37 |
# File 'lib/good_job/notifier.rb', line 37 cattr_reader :instances, default: [], instance_reader: false |
Instance Attribute Details
#recipients ⇒ Array<#call, Array(Object, Symbol)> (readonly)
List of recipients that will receive notifications.
50 51 52 |
# File 'lib/good_job/notifier.rb', line 50 def recipients @recipients end |
Class Method Details
.notify(message) ⇒ Object
Send a message via Postgres NOTIFY
41 42 43 44 45 46 |
# File 'lib/good_job/notifier.rb', line 41 def self.notify() connection = Job.connection connection.exec_query " NOTIFY \#{CHANNEL}, \#{connection.quote(message.to_json)}\n SQL\nend\n".squish |
Instance Method Details
#listening? ⇒ true, ...
Tests whether the notifier is active and listening for new messages.
65 66 67 |
# File 'lib/good_job/notifier.rb', line 65 def listening? @listening.true? end |
#restart(timeout: -1)) ⇒ void
This method returns an undefined value.
Restart the notifier. When shutdown, start; or shutdown and start.
103 104 105 106 107 |
# File 'lib/good_job/notifier.rb', line 103 def restart(timeout: -1) shutdown(timeout: timeout) if running? create_executor listen end |
#running? ⇒ true, ...
Tests whether the notifier is running.
72 |
# File 'lib/good_job/notifier.rb', line 72 delegate :running?, to: :executor, allow_nil: true |
#shutdown(timeout: -1)) ⇒ void
This method returns an undefined value.
Shut down the notifier. This stops the background LISTENing thread. Use #shutdown? to determine whether threads have stopped.
88 89 90 91 92 93 94 95 96 97 |
# File 'lib/good_job/notifier.rb', line 88 def shutdown(timeout: -1) return if executor.nil? || executor.shutdown? executor.shutdown if executor.running? if executor.shuttingdown? && timeout # rubocop:disable Style/GuardClause executor_wait = timeout.negative? ? nil : timeout executor.kill unless executor.wait_for_termination(executor_wait) end end |
#shutdown? ⇒ true, ...
Tests whether the scheduler is shutdown.
77 |
# File 'lib/good_job/notifier.rb', line 77 delegate :shutdown?, to: :executor, allow_nil: true |