Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manage RabbitMQ consumers

I am planning to write a log processing application using RabbitMQ, Symfony2 and the RabbitMqBundle. The tool I am working on has to be highly available and must process millions of entries per day, so it's important that the consumers are always up and running (short breaks are fine), otherwise my queue might overflow after a while.

Are there best practices on how to manage the consumers (written in PHP), start/restart them in case of an error etc?

Thanks

like image 247
Tibor Avatar asked Nov 05 '13 16:11

Tibor


People also ask

How many consumers can RabbitMQ handle?

Single active consumer allows to have only one consumer at a time consuming from a queue and to fail over to another registered consumer in case the active one is cancelled or dies. Consuming with only one consumer is useful when messages must be consumed and processed in the same order they arrive in the queue.

Can RabbitMQ have multiple consumers?

In RabbitMQ, we can have multiple consumers listening from one queue. This is good for fast procesing, but not so good for message ordering.

How do I delete a consumer from RabbitMQ?

You can kill connections to the RabbitMQ broker using the rabbitmqctl tool (see the man page) or by using the Web UI. You could also purge and delete the queue which belonged to the rogue consumer. However, you can't kill the consumer process itself using those tools.


1 Answers

I use this bash script to make sure that there are all required consumers running on imagepush.to:

#!/bin/bash

NB_TASKS=1
SYMFONY_ENV="prod"

TEXT[0]="app/console rabbitmq:consumer primary"
TEXT[1]="app/console rabbitmq:consumer secondary"

for text in "${TEXT[@]}"
do

NB_LAUNCHED=$(ps ax | grep "$text" | grep -v grep | wc -l)

TASK="/usr/bin/env php ${text} --env=${SYMFONY_ENV}"

for (( i=${NB_LAUNCHED}; i<${NB_TASKS}; i++ ))
do
  echo "$(date +%c) - Launching a new consumer"
  nohup $TASK &
done

done

If I remember correctly I took base from KnpBundles code.

like image 107
Anton Babenko Avatar answered Nov 06 '22 01:11

Anton Babenko