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.
33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/pgmq/client/topics.rb', line 33 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
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/pgmq/client/topics.rb', line 173 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.
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 |
# File 'lib/pgmq/client/topics.rb', line 126 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 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 > 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.
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/pgmq/client/topics.rb', line 86 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.
205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/pgmq/client/topics.rb', line 205 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
54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/pgmq/client/topics.rb', line 54 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).
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/pgmq/client/topics.rb', line 229 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).
256 257 258 259 260 261 262 263 264 265 |
# File 'lib/pgmq/client/topics.rb', line 256 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 |