Method: PG::Connection#wait_for_notify

Defined in:
ext/pg_connection.c

#wait_for_notify([ timeout ]) ⇒ String #wait_for_notify([ timeout ]) {|event, pid| ... } ⇒ Object #wait_for_notify([ timeout ]) ⇒ Object Also known as: notifies_wait

Blocks while waiting for notification(s), or until the optional timeout is reached, whichever comes first. timeout is measured in seconds and can be fractional.

Returns nil if timeout is reached, the name of the NOTIFY event otherwise. If used in block form, passes the name of the NOTIFY event and the generating pid into the block.

Under PostgreSQL 9.0 and later, if the notification is sent with the optional payload string, it will be given to the block as the third argument.

Overloads:

  • #wait_for_notify([ timeout ]) ⇒ String

    Returns:

    • (String)
  • #wait_for_notify([ timeout ]) {|event, pid| ... } ⇒ Object

    Yields:

    • (event, pid)


2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
# File 'ext/pg_connection.c', line 2407

static VALUE
pgconn_wait_for_notify(int argc, VALUE *argv, VALUE self)
{
	PGconn *conn = pg_get_pgconn( self );
	PGnotify *pnotification;
	struct timeval timeout;
	struct timeval *ptimeout = NULL;
	VALUE timeout_in = Qnil, relname = Qnil, be_pid = Qnil, extra = Qnil;
	double timeout_sec;

	rb_scan_args( argc, argv, "01", &timeout_in );

	if ( RTEST(timeout_in) ) {
		timeout_sec = NUM2DBL( timeout_in );
		timeout.tv_sec = (time_t)timeout_sec;
		timeout.tv_usec = (suseconds_t)( (timeout_sec - (long)timeout_sec) * 1e6 );
		ptimeout = &timeout;
	}

	pnotification = (PGnotify*) wait_socket_readable( conn, ptimeout, notify_readable);

	/* Return nil if the select timed out */
	if ( !pnotification ) return Qnil;

	relname = rb_tainted_str_new2( pnotification->relname );
#ifdef M17N_SUPPORTED
	ENCODING_SET( relname, rb_enc_to_index(pg_conn_enc_get( conn )) );
#endif
	be_pid = INT2NUM( pnotification->be_pid );
#ifdef HAVE_ST_NOTIFY_EXTRA
	if ( *pnotification->extra ) {
		extra = rb_tainted_str_new2( pnotification->extra );
#ifdef M17N_SUPPORTED
		ENCODING_SET( extra, rb_enc_to_index(pg_conn_enc_get( conn )) );
#endif
	}
#endif
	PQfreemem( pnotification );

	if ( rb_block_given_p() )
		rb_yield_values( 3, relname, be_pid, extra );

	return relname;
}