【问题标题】:How do I make a Docker container for RabbitMQ PHP task consuming如何为 RabbitMQ PHP 任务创建 Docker 容器
【发布时间】: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


【解决方案1】:

我最终遵循了这里给出的教程https://docs.docker.com/config/containers/multi-service_container/

使用 supervisord,我能够安排几个 PHP 任务使用者脚本,并在失败时自动重启并记录到 Docker 日志文件以进行监控。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多