Class: ActiveRecord::PostgresPubSub::Listener
- Inherits:
-
Object
- Object
- ActiveRecord::PostgresPubSub::Listener
- Extended by:
- PrivateAttr
- Defined in:
- lib/activerecord/postgres_pub_sub/listener.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(channel, listen_timeout: nil, exclusive_lock: true, notify_only: true) ⇒ Listener
constructor
A new instance of Listener.
- #listen ⇒ Object
- #on_notify(&blk) ⇒ Object
- #on_start(&blk) ⇒ Object
- #on_timeout(&blk) ⇒ Object
Constructor Details
#initialize(channel, listen_timeout: nil, exclusive_lock: true, notify_only: true) ⇒ Listener
Returns a new instance of Listener.
23 24 25 26 27 28 |
# File 'lib/activerecord/postgres_pub_sub/listener.rb', line 23 def initialize(channel, listen_timeout: nil, exclusive_lock: true, notify_only: true) @channel = channel @listen_timeout = listen_timeout @exclusive_lock = exclusive_lock @notify_only = notify_only end |
Class Method Details
.listen(channel, listen_timeout: nil, exclusive_lock: true, notify_only: true) {|listener| ... } ⇒ Object
14 15 16 17 18 19 20 21 |
# File 'lib/activerecord/postgres_pub_sub/listener.rb', line 14 def self.listen(channel, listen_timeout: nil, exclusive_lock: true, notify_only: true) listener = new(channel, listen_timeout: listen_timeout, exclusive_lock: exclusive_lock, notify_only: notify_only) yield(listener) if block_given? listener.listen end |
Instance Method Details
#listen ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/activerecord/postgres_pub_sub/listener.rb', line 42 def listen with_connection do |connection| on_start_blk&.call loop do wait_for_notify(connection) do |payload| notify_only ? on_notify_blk.call : on_notify_blk.call(payload) end end end end |
#on_notify(&blk) ⇒ Object
30 31 32 |
# File 'lib/activerecord/postgres_pub_sub/listener.rb', line 30 def on_notify(&blk) @on_notify_blk = blk end |
#on_start(&blk) ⇒ Object
34 35 36 |
# File 'lib/activerecord/postgres_pub_sub/listener.rb', line 34 def on_start(&blk) @on_start_blk = blk end |
#on_timeout(&blk) ⇒ Object
38 39 40 |
# File 'lib/activerecord/postgres_pub_sub/listener.rb', line 38 def on_timeout(&blk) @on_timeout_blk = blk end |