【问题标题】:Start a background symfony/process from symfony/console从 symfony/console 启动后台 symfony/进程
【发布时间】:2015-03-13 16:56:54
【问题描述】:

我正在尝试创建一个 cli (symfony/console) 来管理消耗消息队列的长时间运行的子进程 (symfony/process)。我有两个命令,Consume 和 Listen。 Consume 是 Listen 的包装器,因此可以在后台运行。 Listen 是一个长时间运行的 MQ 转储,它在添加到消息队列时显示其他消息。

问题:当尝试从 within Consume 调用 Listen 的 cli 命令时,它启动进程并给我一个 PID,但随后子进程立即死亡。我需要弄清楚如何让 Consume 分离出多个实际保持运行的 Listen 进程。

如果相关,它将运行的操作系统是 SLES 12 和使用 PHP 5.5 的 Ubuntu 14.04。

一些代码(相关的sn-ps)


听

// Runs as: php mycli.php mq:listen
// Keeps running until you ctrl+C
// On the commandline, I can run this as
// nohup php mycli.php mq:listen 2>&1 > /dev/null &
// and it works fine as a long running process.
protected function execute(InputInterface $input, OutputInterface $output)
{
    $mq = new Mq::connect();

    while (true) {
        // read the queue
        $mq->getMessage();
    }
}

消费

// Runs as: php mycli.php mq:consume --listen
// Goal: Run the mq:listen command in the background
protected function execute(InputInterface $input, OutputInterface $output)
{   
    if ($input->getOption('listen')) {

        $process = new Process('php mycli.php mq:listen');

        $process->start();

        $pid = $process->getPid();
        $output->writeln("Worker started with PID: $pid");

    }
}

【问题讨论】:

    标签: php symfony symfony-console symfony-process


    【解决方案1】:

    这样的任务通常会被委派给像主管这样的任务调度程序。像这样将进程作为孤儿离开是非常危险的。如果您的消息队列客户端失去连接但进程仍在运行,它们实际上会变成僵尸。

    您需要在每个子进程的持续时间内保持mq:consume 命令运行。如上三个示例所示,这是可以实现的:http://symfony.com/blog/new-in-symfony-2-2-process-component-enhancements

    【讨论】:

    • 如果在 Listen 中的 while() 中添加了连接检查,如果连接无效,我不能中断循环吗?
    • @Chad 这取决于$mq->getMessage() 是否是一个阻塞函数。如果是,您将没有机会进行检查。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-20
    • 2015-06-25
    • 2015-11-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-03
    相关资源
    最近更新 更多