AMQPDaemon¶
This module provides generic AMQP daemon and builder of common connection
informations, which are defined as constants in edeposit.amqp.settings
.
Daemon is used by edeposit_amqp_alephdaemon
and
edeposit_amqp_calibredaemon
.
-
class
edeposit.amqp.amqpdaemon.
AMQPDaemon
(con_param, queue, out_exch, out_key, react_fn, glob)[source]¶ Bases:
edeposit.amqp.pikadaemon.PikaDaemon
Parameters: - con_param (ConnectionParameters) – see
getConParams()
for details - queue (str) – name of the queue
- out_exch (str) – name of the exchange for outgoing messages
- out_key (str) – what key will be used to send messages back
- react_fn (fn) – function, which can react to messages, see Note for details
- glob (dict) – result of
globals()
call - used in deserializer to automatically build classes, which are not available in this namespace of this package
Note
react_fn
parameter is expected to be function, which gets two parameters - message (some form of message, it can be also namedtuple), and UUID containing unique identificator of the message.Example of function used as react_fn parameter:
def reactToAMQPMessage(message, UUID): response = None if message == 1: return 2 elif message == "Hello": return "Hi" elif type(message) == dict: return {1: 2} raise UserWarning("Unrecognized message")
As you can see, protocol is pretty easy. You get message, to which you react somehow and return response. Thats all.
-
get_sendback
(uuid, key)[source]¶ Return function for sending progress messages back to original caller.
Parameters: Returns: Reference to function which takes only one data argument.
Return type: fn reference
-
onMessageReceived
(method_frame, properties, body)[source]¶ React to received message - deserialize it, add it to users reaction function stored in
self.react_fn
and send back result.If Exception is thrown during process, it is sent back instead of message.
Note
In case of Exception, response message doesn’t have useful body, but in headers is stored following (string) parameters:
exception
, where the Exception’s message is storedexception_type
, wheree.__class__
is storedexception_name
, wheree.__class__.__name__
is storedtraceback
where the full traceback is stored (contains line number)
This allows you to react to unexpected cases at the other end of the AMQP communication.
- con_param (ConnectionParameters) – see
-
edeposit.amqp.amqpdaemon.
getConParams
(virtualhost)[source]¶ Connection object builder.
Parameters: virtualhost (str) – selected virtualhost in rabbitmq Returns: object filled by constants from edeposit.amqp.settings
.Return type: pika.ConnectionParameters