Module: PGMQ::Client::Topics
- Included in:
- PGMQ::Client
- Defined in:
- lib/pgmq/client/topics.rb
Overview
Requires PGMQ v1.11.0+
Topic routing operations (AMQP-like patterns)
This module provides AMQP-style topic routing for PGMQ, allowing messages to be routed to multiple queues based on pattern matching.
Topic patterns support wildcards:
-
‘*` matches exactly one word between dots (e.g., `orders.*` matches `orders.new`)
-
‘#` matches zero or more words (e.g., `orders.#` matches `orders.new.urgent`)
Instance Method Summary collapse
-
#bind_topic(pattern, queue_name) ⇒ void
Binds a topic pattern to a queue.
-
#list_topic_bindings(queue_name: nil) ⇒ Array<Hash>
Lists all topic bindings.
-
#produce_batch_topic(routing_key, messages, headers: nil, delay: 0) ⇒ Array<Hash>
Sends multiple messages via topic routing.
-
#produce_topic(routing_key, message, headers: nil, delay: 0) ⇒ Integer
Sends a message via topic routing.
-
#test_routing(routing_key) ⇒ Array<Hash>
Tests which queues a routing key would match.
-
#unbind_topic(pattern, queue_name) ⇒ Boolean
Unbinds a topic pattern from a queue.
-
#validate_routing_key(routing_key) ⇒ Boolean
Validates a routing key.
-
#validate_topic_pattern(pattern) ⇒ Boolean
Validates a topic pattern.
Instance Method Details
#bind_topic(pattern, queue_name) ⇒ void
This method returns an undefined value.
Binds a topic pattern to a queue
Messages sent with routing keys matching this pattern will be delivered to the specified queue.
32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/pgmq/client/topics.rb', line 32 def bind_topic(pattern, queue_name) validate_queue_name!(queue_name) with_connection do |conn| conn.exec_params( "SELECT pgmq.bind_topic($1::text, $2::text)", [pattern, queue_name] ) end nil end |
#list_topic_bindings(queue_name: nil) ⇒ Array<Hash>
Lists all topic bindings
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/pgmq/client/topics.rb', line 183 def list_topic_bindings(queue_name: nil) result = with_connection do |conn| if queue_name validate_queue_name!(queue_name) conn.exec_params( "SELECT pattern, queue_name, bound_at FROM pgmq.list_topic_bindings($1::text)", [queue_name] ) else conn.exec("SELECT pattern, queue_name, bound_at FROM pgmq.list_topic_bindings()") end end result.map do |row| { pattern: row["pattern"], queue_name: row["queue_name"], bound_at: row["bound_at"] } end end |
#produce_batch_topic(routing_key, messages, headers: nil, delay: 0) ⇒ Array<Hash>
Sends multiple messages via topic routing
All messages will be delivered to all queues whose bound patterns match the routing key.
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/pgmq/client/topics.rb', line 125 def produce_batch_topic(routing_key, , headers: nil, delay: 0) return [] if .empty? if headers && headers.length != .length raise ArgumentError, "headers array length (#{headers.length}) must match messages array length (#{.length})" end result = with_connection do |conn| encoder = PG::TextEncoder::Array.new = encoder.encode() if headers && !delay.is_a?(Numeric) encoded_headers = encoder.encode(headers) conn.exec_params( "SELECT * FROM pgmq.send_batch_topic($1::text, $2::jsonb[], $3::jsonb[], $4::timestamptz)", [routing_key, , encoded_headers, delay.to_time.utc.iso8601(6)] ) elsif headers encoded_headers = encoder.encode(headers) conn.exec_params( "SELECT * FROM pgmq.send_batch_topic($1::text, $2::jsonb[], $3::jsonb[], $4::integer)", [routing_key, , encoded_headers, delay] ) elsif !delay.is_a?(Numeric) conn.exec_params( "SELECT * FROM pgmq.send_batch_topic($1::text, $2::jsonb[], $3::timestamptz)", [routing_key, , delay.to_time.utc.iso8601(6)] ) elsif delay > 0 conn.exec_params( "SELECT * FROM pgmq.send_batch_topic($1::text, $2::jsonb[], $3::integer)", [routing_key, , delay] ) else conn.exec_params( "SELECT * FROM pgmq.send_batch_topic($1::text, $2::jsonb[])", [routing_key, ] ) end end result.map do |row| { queue_name: row["queue_name"], msg_id: row["msg_id"] } end end |
#produce_topic(routing_key, message, headers: nil, delay: 0) ⇒ Integer
Sends a message via topic routing
The message will be delivered to all queues whose bound patterns match the routing key.
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/pgmq/client/topics.rb', line 84 def produce_topic(routing_key, , headers: nil, delay: 0) result = with_connection do |conn| if headers conn.exec_params( "SELECT pgmq.send_topic($1::text, $2::jsonb, $3::jsonb, $4::integer)", [routing_key, , headers, delay] ) elsif delay > 0 conn.exec_params( "SELECT pgmq.send_topic($1::text, $2::jsonb, $3::integer)", [routing_key, , delay] ) else conn.exec_params( "SELECT pgmq.send_topic($1::text, $2::jsonb)", [routing_key, ] ) end end result[0]["send_topic"].to_i end |
#test_routing(routing_key) ⇒ Array<Hash>
Tests which queues a routing key would match
Useful for debugging topic routing configurations.
215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/pgmq/client/topics.rb', line 215 def test_routing(routing_key) result = with_connection do |conn| conn.exec_params( "SELECT pattern, queue_name FROM pgmq.test_routing($1::text)", [routing_key] ) end result.map do |row| { pattern: row["pattern"], queue_name: row["queue_name"] } end end |
#unbind_topic(pattern, queue_name) ⇒ Boolean
Unbinds a topic pattern from a queue
53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/pgmq/client/topics.rb', line 53 def unbind_topic(pattern, queue_name) validate_queue_name!(queue_name) result = with_connection do |conn| conn.exec_params( "SELECT pgmq.unbind_topic($1::text, $2::text)", [pattern, queue_name] ) end result[0]["unbind_topic"] == "t" end |
#validate_routing_key(routing_key) ⇒ Boolean
Validates a routing key
Routing keys are dot-separated words (no wildcards allowed). Returns false for invalid routing keys (PGMQ raises an error for invalid keys).
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/pgmq/client/topics.rb', line 239 def validate_routing_key(routing_key) result = with_connection do |conn| conn.exec_params( "SELECT pgmq.validate_routing_key($1::text)", [routing_key] ) end result[0]["validate_routing_key"] == "t" rescue PGMQ::Errors::ConnectionError => e # PGMQ raises an error for invalid routing keys return false if e..include?("invalid characters") raise end |
#validate_topic_pattern(pattern) ⇒ Boolean
Validates a topic pattern
Topic patterns can include wildcards: * (single word) or # (zero or more words).
266 267 268 269 270 271 272 273 274 275 |
# File 'lib/pgmq/client/topics.rb', line 266 def validate_topic_pattern(pattern) result = with_connection do |conn| conn.exec_params( "SELECT pgmq.validate_topic_pattern($1::text)", [pattern] ) end result[0]["validate_topic_pattern"] == "t" end |