Code

Declare a consumer on queue with a marshmallow schema

decorator bus_consumer

anyblok_bus.consumer.bus_consumer(queue_name=None, adapter=None, processes=0, **kwargs)

anyblok model plugin

class anyblok_bus.consumer.BusConsumerPlugin(registry)

Bases: anyblok.model.plugins.ModelPluginBase

anyblok.model.plugin to allow the build of the anyblok_bus.bus_consumer

apply_consumer(consumer, new_base, properties, transformation_properties)

Insert in a base the overload

Parameters:
  • new_base – the base to be put on front of all bases
  • properties – the properties declared in the model
  • transformation_properties – the properties of the model
initialisation_tranformation_properties(properties, transformation_properties)

Initialise the transform properties

Parameters:
  • properties – the properties declared in the model
  • new_type_properties – param to add in a new base if need
insert_in_bases(new_base, namespace, properties, transformation_properties)

Insert in a base the overload

Parameters:
  • new_base – the base to be put on front of all bases
  • namespace – the namespace of the model
  • properties – the properties declared in the model
  • transformation_properties – the properties of the model
transform_base_attribute(attr, method, namespace, base, transformation_properties, new_type_properties)

transform the attribute for the final Model

Parameters:
  • attr – attribute name
  • method – method pointer of the attribute
  • namespace – the namespace of the model
  • base – One of the base of the model
  • transformation_properties – the properties of the model
  • new_type_properties – param to add in a new base if need

Worker

class anyblok_bus.worker.Worker(registry, profile, consumers, withautocommit=True)

Bases: object

Define consumers to consume the queue défined in the AnyBlok registry by the bus_consumer decorator

worker = Worker(anyblokregistry, profilename)
worker.start()  # blocking loop
worker.is_ready()  # return True if all the consumer are started
worker.stop()  # stop the loop and close the connection with rabbitmq

This is an example consumer that will handle unexpected interactions with RabbitMQ such as channel and connection closures.

If RabbitMQ closes the connection, this class will stop and indicate that reconnection is necessary. You should look at the output, as there are limited reasons why the connection may be closed, which usually are tied to permission related issues or socket timeouts.

If the channel is closed, it will indicate a problem with one of the commands that were issued and that should surface in the output as well.

Parameters:
  • registry – anyblok registry instance
  • profile – the name of the profile which give the url of rabbitmq
  • consumers – list of the consumer to consum
  • withautocommit – default True, commit all the transaction
add_on_cancel_callback()

Add a callback that will be invoked if RabbitMQ cancels the consumer for some reason. If RabbitMQ does cancel the consumer, on_consumer_cancelled will be invoked by pika.

add_on_channel_close_callback()

This method tells pika to call the on_channel_closed method if RabbitMQ unexpectedly closes the channel.

close_channel()

Call to close the channel with RabbitMQ cleanly by issuing the Channel.Close RPC command.

close_connection()
connect()

This method connects to RabbitMQ, returning the connection handle. When the connection is established, the on_connection_open method will be invoked by pika.

Return type:pika.SelectConnection
declare_consumer(queue, model, method)
get_url()

Retrieve connection url

is_ready()
on_basic_qos_ok(_unused_frame)

Invoked by pika when the Basic.QoS method has completed. At this point we will start consuming messages by calling start_consuming which will invoke the needed RPC commands to start the process.

Parameters:_unused_frame (pika.frame.Method) – The Basic.QosOk response frame
on_bindok(_unused_frame, userdata)

Invoked by pika when the Queue.Bind method has completed. At this point we will set the prefetch count for the channel.

Parameters:
  • _unused_frame (pika.frame.Method) – The Queue.BindOk response frame
  • userdata (str|unicode) – Extra user data (queue name)
on_cancelok(_unused_frame, userdata)

This method is invoked by pika when RabbitMQ acknowledges the cancellation of a consumer. At this point we will close the channel. This will invoke the on_channel_closed method once the channel has been closed, which will in-turn close the connection.

Parameters:
  • _unused_frame (pika.frame.Method) – The Basic.CancelOk frame
  • userdata (str|unicode) – Extra user data (consumer tag)
on_channel_closed(channel, reason)

Invoked by pika when RabbitMQ unexpectedly closes the channel. Channels are usually closed if you attempt to do something that violates the protocol, such as re-declare an exchange or queue with different parameters. In this case, we’ll close the connection to shutdown the object.

Parameters:
  • pika.channel.Channel – The closed channel
  • reason (Exception) – why the channel was closed
on_channel_open(channel)

This method is invoked by pika when the channel has been opened. The channel object is passed in so we can make use of it.

Since the channel is now open, we’ll declare the exchange to use.

Parameters:channel (pika.channel.Channel) – The channel object
on_connection_closed(_unused_connection, reason)

This method is invoked by pika when the connection to RabbitMQ is closed unexpectedly. Since it is unexpected, we will reconnect to RabbitMQ if it disconnects.

Parameters:
  • connection (pika.connection.Connection) – The closed connection obj
  • reason (Exception) – exception representing reason for loss of connection.
on_connection_open(_unused_connection)

This method is called by pika once the connection to RabbitMQ has been established. It passes the handle to the connection object in case we need it, but in this case, we’ll just mark it unused.

Parameters:_unused_connection (pika.SelectConnection) – The connection
on_connection_open_error(_unused_connection, err)

This method is called by pika if the connection to RabbitMQ can’t be established.

Parameters:
  • _unused_connection (pika.SelectConnection) – The connection
  • err (Exception) – The error
on_consumer_cancelled(method_frame)

Invoked by pika when RabbitMQ sends a Basic.Cancel for a consumer receiving messages.

Parameters:method_frame (pika.frame.Method) – The Basic.Cancel frame
open_channel()

Open a new channel with RabbitMQ by issuing the Channel.Open RPC command. When RabbitMQ responds that the channel is open, the on_channel_open callback will be invoked by pika.

reconnect()

Will be invoked if the connection can’t be opened or is closed. Indicates that a reconnect is necessary then stops the ioloop.

set_qos()

This method sets up the consumer prefetch to only be delivered one message at a time. The consumer must acknowledge this message before RabbitMQ will deliver another one. You should experiment with different prefetch values to achieve desired performance.

start()

Run the example consumer by connecting to RabbitMQ and then starting the IOLoop to block and allow the SelectConnection to operate.

start_consuming()

This method sets up the consumer by first calling add_on_cancel_callback so that the object is notified if RabbitMQ cancels the consumer. It then issues the Basic.Consume RPC command which returns the consumer tag that is used to uniquely identify the consumer with RabbitMQ. We keep the value to use it when we want to cancel consuming. The on_message method is passed in as a callback pika will invoke when a message is fully received.

stop()

Cleanly shutdown the connection to RabbitMQ by stopping the consumer with RabbitMQ. When RabbitMQ confirms the cancellation, on_cancelok will be invoked by pika, which will then closing the channel and connection. The IOLoop is started again because this method is invoked when CTRL-C is pressed raising a KeyboardInterrupt exception. This exception stops the IOLoop which needs to be running for pika to communicate with RabbitMQ. All of the commands issued prior to starting the IOLoop will be buffered but not processed.

stop_consuming()

Tell RabbitMQ that you would like to stop consuming by sending the Basic.Cancel RPC command.