我已经阅读了很多关于RabbitMQ集成到Symfony应用程序的文章。
RabbitMqBundle使得它很容易被提升,它提供了方便的rabbitmq:consumer
命令来使用来自队列的消息,如下所示:
app/console rabbitmq:consumer -m 50 upload_picture
但我有一个问题。您是否应该建议将此命令添加到crontab?有没有关于它的最佳实践?
答案 0 :(得分:6)
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.