Reputation: 714
I'm attempting to create a cli (symfony/console) that manages long-running child processes (symfony/process) that consume a message queue. I have two commands, Consume and Listen. Consume is a wrapper for Listen so it can be run in the background. Listen is a long running dump of the MQ that shows additional messages as they're added to the message queue.
Problem: When attempting to invoke the cli command for Listen from within Consume, it starts the process and gives me a PID, but then the child process immediately dies. I need to figure out how to get Consume to spin off multiple Listen processes that actually stay running.
In case it's relevant, the OS(es) that this will run on are SLES 12 and Ubuntu 14.04 using PHP 5.5.
Some Code (relevant snippets)
Listen
// 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();
}
}
Consume
// 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");
}
}
Upvotes: 3
Views: 3665
Reputation: 6946
A task like this would normally be delegated to a task scheduler like Supervisor. It is quite dangerous to leave processes as orphans like this. If your message queue client loses connection but the processes are still running, they effectively turn into zombies.
You need to keep the mq:consume
command running for the duration for each sub process. This is achievable as indicated in the last three examples at: http://symfony.com/blog/new-in-symfony-2-2-process-component-enhancements
Upvotes: 2