Package: varys-client
Varys - A python RabbitMQ client for CLIMB-TRETODO:Test SSL support with CA-signed certificatesInstallationPip or Conda installation will be simplest for most users, and can be achieved with the following commands:pip install varys-clientorconda install -c conda-forge varysAlternatively, varys can be installed directly from this repository by doing the following:git clone https://github.com/CLIMB-TRE/varys.git

cd varys

pip install .ConfigurationVarys uses a JSON format configuration file to provide credentials to connect to RabbitMQ, the path to this configuration file should either be provided with theVARYS_CFGenvironmental variable or usingconfig_pathargument when instantiating varys, this will override an environmental variable.An example of the configuration file formatis available here.Basic UsageFirst the varys object must be instantiated, like so:importvarysvarys_client=Varys(profile="test_user",logfile="/var/log/varys_test.log",log_level="DEBUG")Profile will control which set of credentials the client will read from the config file.By default Varys will automatically acknowledge received messages instantly however if this is not the desired behaviour then the argumentauto_acknowledgecan be set toFalse, it is important that messages are later acknowledged if this setting is turned off or you risk the RabbitMQ server deciding that you have timed out and closing the connection, see more in the "Message Acknowledgement" section below.Once the base object has been instantiated you are ready to send or receive messages from the RabbitMQ server:Thequeue_suffixargument must be provided the first time a message is sent or receeived to/from a queue after varys is instantiated so that the varys may create or bind the queue (exchange + . + queue_suffix) if it already exists.Sendingmessage={"foo":"bar"}varys_client.send(message=message,exchange="test_exchange",queue_suffix="test_suffix")Messages must be a python object that can be serialised into JSON format usingjson.dumps()which includes the following types: dict, list, tuple, str, int, float, True, False, None.Receiving one message at a timeimportjsonmessage=varys_client.receive(exchange="test_exchange",queue_suffix="test_suffix",block=True,)deserialised_message=json.loads(message.body)print(deserialised_message)This will block execution until a message is received unless theblockargument is set toFalse, in this case if there are no messages to be received the return value will beNoneobject.If a message is received it will be avarys_messageobject which has the following attributes:basic_deliver -> Apika.spec.Basic.DeliverShould be irrelevant for normal usage but documentation available at this link.properties -> Apika.BasicPropertiesobject, containing header information about the message if provided when sending. Should be irrelevant for normal usage.body -> The message body in serialised JSON format, generally a user will wish to convert this to a python object equivalent for ease of use withjson.loads()Message acknowledgementWhereauto_acknowledgeis set toFalsewhen instantiating varys, messages must be acknowledged after being received manually like so:message=varys_client.receive(exchange="test_exchange",queue_suffix="test_suffix",block=True)varys_client.acknowledge_message(message)If you wish to instead reject the message meaning that it will be pushed back onto the first position of the queue, you cannacka message:message=varys_client.receive(exchange="test_exchange",queue_suffix="test_suffix",block=True)varys_client.nack_message(message)Prefetch countIfauto_acknowledgeis not set, another useful argument is theprefetch_countthis allows the user to change the number of messages which will be made available to the user at any given time; when a message is acknowledged another message will be made available to maintain this number so long as there are messages available in the rabbitMQ queue. This setting is particularly useful where messages may be acted on in parallel; for example: if 5 messages can be proccessed at a time then the appropriateprefetch_countwould be5, if 10 messages may be processed in parallel then the appropriateprefetch_countwould be10.Receiving multiple messages at a timeimportjsonmessages=varys_client.receive_batch(exchange="test_exchange",queue_suffix="test_suffix",)formessageinmessages:deserialised_message=json.loads(message.body)print(deserialised_message)This will never block execution and will always return a python list object containing all available messages asvarys_messageobjects which should then be iterated through and treated as above. In the case of there being no messages available, this list will be empty and will evaluate toFalse.