baseplate.queue_consumer¶
Create a long-running process to consume from a queue. For example:
from kombu import Connection, Exchange
from baseplate import queue_consumer
def process_links(context, msg_body, msg):
print('processing %s' % msg_body)
queue_consumer.consume(
baseplate=make_baseplate(cfg, app_config),
exchange=Exchange('reddit_exchange', 'direct'),
connection=Connection(
hostname='amqp://guest:guest@reddit.local:5672',
virtual_host='/',
),
queue_name='process_links_q',
routing_keys=[
'link_created',
'link_deleted',
'link_updated',
],
handler=process_links,
)
This will create a queue named 'process_links_q' and bind the routing keys
'link_created', 'link_deleted', and 'link_updated'. It will then
register a consumer for 'process_links_q' to read messages and feed them to
process_links.
Register and run a queue consumer¶
-
baseplate.queue_consumer.consume(baseplate, exchange, connection, queue_name, routing_keys, handler)¶ Create a long-running process to consume messages from a queue.
A queue with name
queue_nameis created and bound to therouting_keysso messages published to therouting_keysare routed to the queue.Next, the process registers a consumer that receives messages from the queue and feeds them to the
handler.The
handlerfunction must take 3 arguments:context: a baseplate contextmessage_body: the text body of the messagemessage:kombu.message.Message
The consumer will automatically
ackeach message after the handler method exits. If there is an error in processing and the message must be retried the handler should raise an exception to crash the process. This will prevent theackand the message will be re-queued at the head of the queue.Parameters: - baseplate (baseplate.core.Baseplate) – A baseplate instance for the service.
- exchange (kombu.Exchange) –
- connection (kombu.connection.Connection) –
- queue_name (str) – The name of the queue.
- routing_keys (list) – List of routing keys.
- handler – The handler method.
-
class
baseplate.queue_consumer.KombuConsumer(worker, worker_thread)¶ Consumer for use in baseplate.
The
get_message()andget_batch()methods will automatically record diagnostic information.-
get_message(server_span)¶ Return a single message.
Parameters: server_span (baseplate.core.ServerSpan) –
-
get_batch(server_span, max_items, timeout)¶ Return a batch of messages.
Parameters: - server_span (baseplate.core.ServerSpan) –
- max_items (int) – The maximum batch size.
- timeout (int) – The maximum time to wait in seconds, or
Nonefor no timeout.
-
If you require more direct control¶
-
class
baseplate.queue_consumer.BaseKombuConsumer(worker, worker_thread)¶ Base object for consuming messages from a queue.
A worker process accepts messages from the queue and puts them in a local work queue. The “real” consumer can then get messages with
get_message()orget_batch(). It is that consumer’s responsibility toackorrejectmessages.Can be used directly, outside of standard baseplate context.
-
classmethod
new(connection, queues)¶ Create and initialize a consumer.
Parameters: - exchange (kombu.Exchange) –
- queues (list) – List of
kombu.queue.Queueobjects.
-
get_message()¶ Return a single message.
-
classmethod