I have read many (great) articles on RabbitMQ integration into a Symfony application.
The RabbitMqBundle makes it very easy to ingrate it, and it provides the convenient rabbitmq:consumer
command to consume messages from a queue like this:
app/console rabbitmq:consumer -m 50 upload_picture
I have a question however. Should you recommend to add this command to crontab? Are there any best practices about it?
The more accepted method is to keep your consumer running. There are tools like Supervisor and Circus that can help you with that. See this discussion. But if you can get your consumer to exit when there are no more messages, you could also use cron. Although this might cause a delay in consuming the messages. You can't react on messages instantly. Users might have to wait a minute before any task is started / mail is received.
Please take the following into account when running your consumer (or any PHP code for that matter) for a long time:
Try to avoid memory usage accumulation. Don't keep appending to arrays without ever clearing them. This means for instance that you shouldn't use FingersCrossedHandler in Monolog since this keeps a buffer of log messages. Fine for a single request, not for a daysworth of debug messages.
Even when you are careful, PHP might leak memory. What can you say, PHP... (That could indeed be a catchphrase) In my situation I have a cronjob installed that restarts the workers every night, but in theory the consumers could run about a month before they run out of memory.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With