Module: Extensions::DatabaseEvent::ActiveRecord::Base::ClassMethods

Defined in:
lib/extensions/database_event/active_record/base.rb

Instance Method Summary collapse

Instance Method Details

#signal(identifier) ⇒ Object

Signals to possible waiting consumers on this record.


30
31
32
# File 'lib/extensions/database_event/active_record/base.rb', line 30

def signal(identifier)
  connection.execute("NOTIFY #{identifier};")
end

#wait(identifier, timeout: nil, while_callback: nil, &block) ⇒ Boolean, ...

Waits for the given +NOTIFY+ signal, optionally until a given time, or until a specific condition is met.

Parameters:

  • identifier (String)

    The +NOTIFY+ signal to wait for.

  • timeout (Integer|nil)

    The timeout to wait for a message. A +nil+ or zero timeout will wait indefinitely. The timeout applies to the total time waiting for a notification, even if the function waits multiple times.

  • while_callback (Proc|nil)

    The callback that will be called after the +LISTEN+ statement is sent, and is used to check to see if the library should continue waiting for a notification. If this returns +false+, the named of the last received notification is returned. If no notification was received yet (i.e. before the first wait), this returns +false+.

Returns:

  • (Boolean)

    If a +while+ callback was specified and it returned false before the first wait, this returns false.

  • (String)

    If a notification was received.

  • (nil)

    If the timeout elapsed.


20
21
22
23
24
25
26
27
# File 'lib/extensions/database_event/active_record/base.rb', line 20

def wait(identifier, timeout: nil, while_callback: nil, &block)
  deadline = timeout ? Time.zone.now + timeout : nil
  connection.execute("LISTEN #{identifier};")

  wait_for_identifier(identifier, deadline, while_callback, &block)
ensure
  connection.execute("UNLISTEN #{identifier};")
end