2015-03-13 116 views
3

我試圖創建一個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"); 

    } 
} 

回答

2

任務裏通常這將委派給像Supervisor這樣的任務調度器。把流程作爲這樣的孤兒離開是非常危險的。如果你的消息隊列客戶端失去連接,但進程仍在運行,它們會變成殭屍。

您需要保持mq:consume命令在每個子進程的持續時間內運行。這是可以實現的,如在最後三個例子中所示:http://symfony.com/blog/new-in-symfony-2-2-process-component-enhancements

+0

如果連接檢查已添加到Listen中的while(),如果連接無效,我不能打斷循環嗎? – Chad 2015-03-13 17:43:25

+0

@Chad這取決於'$ mq-> getMessage()'是否是一個阻塞函數。如果是這樣,你將不會有機會進行檢查。 – Flosculus 2015-03-16 09:37:03