【发布时间】:2019-02-28 01:19:25
【问题描述】:
我在 Docker 中有一个 LAMP 设置,用作我正在开发的 Cordova 项目的 API。
我只研究了 PHP 排队和 RabbitMQ,但卸载任务将有助于加快照片上传或电子邮件发送等任务的速度。
我有这段代码来收听和使用 RabbitMQ 消息以发送电子邮件,但是我不确定如何使用 PHP 将其作为一种守护进程启动。我还计划添加更多队列,这需要更多的侦听器,所以我的想法是有一个 Docker 任务容器专门用于侦听和使用任务。
use Enqueue\AmqpLib\AmqpConnectionFactory;
use Enqueue\AmqpLib\AmqpContext;
/**
* Inititate queue
*/
emailQueue();
function emailQueue(){
// Create consumer
$context = (new AmqpConnectionFactory(ENQUEUE_OPTIONS))->createContext();
$queue = $context->createQueue('send_email');
$context->declareQueue($queue);
$consumer = $context->createConsumer($queue);
while(true) {
// Get message
$message = $consumer->receive($timeout = 10);
if($message) {
// Extract args
$args = json_decode($message->getBody(), true);
extract($args);
// Send email
$mail = new Mailer();
$mail->setFrom($from, $from_name);
$mail->addAddress($email);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->send();
// Acknowledge
$consumer->acknowledge($message);
}
}
}
我如何创建一个 Docker 容器来启动我的 PHP 脚本来监听和使用 PHP 任务,以免阻塞我的主应用程序执行?
【问题讨论】:
-
为时已晚,但也许可以帮助其他人:您可以创建 Cron 作业运行群发脚本
-
@anmahmoudi Cron 在我的情况下不起作用,因为当它发生故障并且容器关闭时,supervisord 提供了一种更易于管理的监控方法
标签: php docker rabbitmq daemon