Class: PGMQ::Client
- Inherits:
-
Object
- Object
- PGMQ::Client
- Includes:
- Autovacuum, Consumer, Maintenance, MessageLifecycle, Metrics, MultiQueue, Producer, QueueManagement, Topics, Transaction
- Defined in:
- lib/pgmq/client.rb,
lib/pgmq/client/topics.rb,
lib/pgmq/client/metrics.rb,
lib/pgmq/client/consumer.rb,
lib/pgmq/client/producer.rb,
lib/pgmq/client/autovacuum.rb,
lib/pgmq/client/maintenance.rb,
lib/pgmq/client/multi_queue.rb,
lib/pgmq/client/queue_management.rb,
lib/pgmq/client/message_lifecycle.rb
Overview
Low-level client for interacting with PGMQ (Postgres Message Queue)
This is a thin wrapper around PGMQ SQL functions. For higher-level abstractions (job processing, retries, Rails integration), use pgmq-framework.
Defined Under Namespace
Modules: Autovacuum, Consumer, Maintenance, MessageLifecycle, Metrics, MultiQueue, Producer, QueueManagement, Topics
Constant Summary collapse
- DEFAULT_VT =
Default visibility timeout in seconds
30
Constants included from Autovacuum
Autovacuum::DEFAULT_ARCHIVE_SETTINGS, Autovacuum::DEFAULT_QUEUE_SETTINGS
Instance Attribute Summary collapse
-
#connection ⇒ PGMQ::Connection
readonly
The connection manager.
Instance Method Summary collapse
-
#close ⇒ void
Closes all connections in the pool.
-
#initialize(conn_params = nil, pool_size: Connection::DEFAULT_POOL_SIZE, pool_timeout: Connection::DEFAULT_POOL_TIMEOUT, auto_reconnect: true) ⇒ Client
constructor
Creates a new PGMQ client.
-
#stats ⇒ Hash
Returns connection pool statistics.
-
#with_connection {|PG::Connection| ... } ⇒ Object
Checks out a pooled connection and yields the raw
PG::Connectionfor arbitrary SQL.
Methods included from Autovacuum
Methods included from Topics
#bind_topic, #list_topic_bindings, #produce_batch_topic, #produce_topic, #test_routing, #unbind_topic, #validate_routing_key, #validate_topic_pattern
Methods included from Metrics
Methods included from Maintenance
#convert_archive_partitioned, #disable_notify_insert, #enable_notify_insert, #list_notify_insert_throttles, #purge_queue, #update_notify_insert, #wait_for_notify
Methods included from MessageLifecycle
#archive, #archive_batch, #archive_multi, #delete, #delete_batch, #delete_multi, #pop, #pop_batch, #set_vt, #set_vt_batch, #set_vt_multi
Methods included from MultiQueue
#pop_multi, #read_multi, #read_multi_with_poll
Methods included from Consumer
#read, #read_batch, #read_grouped, #read_grouped_head, #read_grouped_rr, #read_grouped_rr_with_poll, #read_grouped_with_poll, #read_with_poll
Methods included from Producer
Methods included from QueueManagement
#create, #create_fifo_index, #create_fifo_indexes_all, #create_partitioned, #create_unlogged, #drop_queue, #list_queues
Methods included from Transaction
Constructor Details
#initialize(conn_params = nil, pool_size: Connection::DEFAULT_POOL_SIZE, pool_timeout: Connection::DEFAULT_POOL_TIMEOUT, auto_reconnect: true) ⇒ Client
Creates a new PGMQ client
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/pgmq/client.rb', line 63 def initialize( conn_params = nil, pool_size: Connection::DEFAULT_POOL_SIZE, pool_timeout: Connection::DEFAULT_POOL_TIMEOUT, auto_reconnect: true ) @connection = if conn_params.is_a?(Connection) conn_params else Connection.new( conn_params, pool_size: pool_size, pool_timeout: pool_timeout, auto_reconnect: auto_reconnect ) end end |
Instance Attribute Details
#connection ⇒ PGMQ::Connection (readonly)
Returns the connection manager.
43 44 45 |
# File 'lib/pgmq/client.rb', line 43 def connection @connection end |
Instance Method Details
#close ⇒ void
This method returns an undefined value.
Closes all connections in the pool
83 84 85 |
# File 'lib/pgmq/client.rb', line 83 def close @connection.close end |
#stats ⇒ Hash
Returns connection pool statistics
93 94 95 |
# File 'lib/pgmq/client.rb', line 93 def stats @connection.stats end |
#with_connection {|PG::Connection| ... } ⇒ Object
You receive the raw PG::Connection. PGMQ performs no type mapping (results come back as strings) and does not wrap your statement in a transaction. Use Transaction#transaction when you need atomicity.
Do not retain or use the yielded connection outside the block. Once the block returns, the connection goes back to the pool and another thread may check it out; PG::Connection is not thread-safe, so using it after the block can corrupt libpq state (nil results, wrong data, segfaults).
The connection is returned to the pool without resetting session state. Anything session-scoped you create - LISTEN, SET, a session-level advisory lock (pg_advisory_lock), a prepared statement, a temp table - survives check-in and leaks to the next pool user. Undo it before the block exits (UNLISTEN, RESET, pg_advisory_unlock, etc.). For LISTEN/NOTIFY consumption, prefer PGMQ::Client::Maintenance#wait_for_notify, which manages LISTEN/UNLISTEN for you.
Checks out a pooled connection and yields the raw PG::Connection for arbitrary SQL.
All PGMQ operations run through this method internally, so it carries the same guarantees: a connection is taken from the pool, health-checked when auto_reconnect is enabled, and a single retry on a fresh connection is attempted if the checked-out connection turns out to be dead before any SQL is sent (see PGMQ::Connection#with_connection). The connection is returned to the pool when the block exits.
Exposed so callers can issue PostgreSQL statements PGMQ does not wrap without standing up a second pool: ad-hoc NOTIFY, LISTEN, advisory locks, custom monitoring queries, or DDL that lives alongside your queue tables. Reusing the PGMQ pool keeps connection accounting in one place.
134 135 136 |
# File 'lib/pgmq/client.rb', line 134 def with_connection(&) @connection.with_connection(&) end |