Martkos IT Ltd

View Original

Pika

RabbitMQ is a message queuing system. A python tool called Pika is able to communicate with RabbitMQ and can prove useful for retrieving messages from error queues.

PRE-REQUISITE

You will need to install Pika. One of the easiest ways to do with is via pip.

INSTALLATION

To install pika you will need a python environment installed with pip. With pip installed you can simply run the following command :-

pip install pika

USAGE

In this example we are going to provide the code to extract all messages from an ERROR_QUEUE and compress them inside a gzip container. To execute it copy the code below and save into a file called pull_messages.py.

python pull_messages.py

————-——--CODE—————-—--

See this content in the original post

————-—---CODE———————--

Once you see all of the required messages have been consumed from the RabbitMQ ERROR_QUEUE you can then press CTRL + C to exit the python script. A file called ERROR_QUEUE.gz will now exist in the current directory. You can simply extract the file or read it with a tool like zcat and less.

zcat ERROR_QUEUE.gz | less

SCRIPT EXPLANATION

  1. Import both gzip and pika.

  2. Build variables containing the credentials and host details.

  3. Create the connection and channel.

  4. Define the call (in this case we want the message and all of the properties of the message) and destination file.

  5. Create a variable containing the queue details.

  6. Consume from the queue only stopping when there is a keyboard interupt.

FURTHER READING

Pika official site

PIP official site