我試圖創建一個cli(symfony/console)來管理使用消息隊列的長時間運行的子進程(symfony/process)。我有兩個命令,消費和聽。 Consume是Listen的包裝,所以它可以在後臺運行。 Listen是MQ的一個長時間運行的轉儲,它在添加到消息隊列中時顯示額外的消息。從symfony/console開始後臺symfony /進程
問題:當試圖調用CLI命令聽從內消費,啓動過程,給了我一個PID,但隨後的子進程立即死亡。我需要弄清楚如何讓Consume消除實際上保持運行的多個Listen過程。
如果相關,它將運行的操作系統是使用PHP 5.5的SLES 12和Ubuntu 14.04。
一些代碼(相關片段)
聽
// 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");
}
}
如果連接檢查已添加到Listen中的while(),如果連接無效,我不能打斷循環嗎? – Chad 2015-03-13 17:43:25
@Chad這取決於'$ mq-> getMessage()'是否是一個阻塞函數。如果是這樣,你將不會有機會進行檢查。 – Flosculus 2015-03-16 09:37:03