如何为 RabbitMQ PHP 任务创建一个 Docker 容器
How do I make a Docker container for RabbitMQ PHP task consuming
我在 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 任务,以免阻止我的主应用程序执行?
我最终遵循了这里给出的教程 https://docs.docker.com/config/containers/multi-service_container/
使用 supervisord,我能够安排几个 PHP 任务使用者脚本 auto-restarts 失败并记录到 Docker 日志文件以进行监控。
我在 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 任务,以免阻止我的主应用程序执行?
我最终遵循了这里给出的教程 https://docs.docker.com/config/containers/multi-service_container/
使用 supervisord,我能够安排几个 PHP 任务使用者脚本 auto-restarts 失败并记录到 Docker 日志文件以进行监控。