Symfony2 - process launching a symfony2 command
Asked Answered
E

2

3

My goal is to launch some time consuming functions in the background to avoid user to wait before the server response is rendered.

I have a Symfony project where I am launching an asynchronous process from a controller. This process will launch a Symfony command that will call another controller.

The problem is when I am calling the process with start()

$process = new Process('/usr/bin/php '.$this->get('kernel')->getRootDir().'/console scopusftpupload ' . escapeshellcmd($params));
$process->start();

Then nothing happens with the process, but the redirect and some other code that I have later works.

However, if I do

$process->run();

Then the process works fine, but I need to wait until the proceess finished in order to render the view.

Any idea what I am doing wrong?

Equerry answered 21/10, 2014 at 13:50 Comment(0)
E
3

I found a workaround to this problem using exec instaed of creating a new Process object.

exec('/usr/bin/php '.$this->get('kernel')->getRootDir().'/console scopusftpupload ' . escapeshellcmd($params) . ' > /dev/null 2>&1 &');
Equerry answered 21/10, 2014 at 16:2 Comment(0)
O
0

We do our asynchronous processing with Gearman job server - http://gearman.org/

There are bundles that streamlines the use of Gearman in a Symfony project. We use mmoreram/GearmanBundle - https://github.com/mmoreram/GearmanBundle. Basically, you create your worker, which can be a service, and call it for time consuming tasks. We use it for data imports that take long time.

Ortiz answered 21/10, 2014 at 15:17 Comment(2)
Thank you @dmnptr, this may work but is a bit complicated for the usage I need. I found a workaround using exec, will post the answerEquerry
However, this deserves a +1 :)Equerry

© 2022 - 2024 — McMap. All rights reserved.