2

I have a symfony commad that needes few minutes to be completed. And i want to run it by a http request. The problem is that the server kills the command process just after sending response, he has a timeout.

i tried to use symfony process asynchronously But still not working ( i get always the timeout problem).

I dont want use the kernel.terminate event, since it is not a best practice. Here is the code in my controller :

            $commandProcess=new Process('php  bin/console app:doSomeThing');
            $commandProcess->setWorkingDirectory('./../');
            $commandProcess->start();
            $commandProcess->setTimeout(50000000);

Any response will be much appreciated.

6
  • Are you using php-fpm by any chance? Commented Apr 26, 2018 at 9:57
  • Have you tried to settimeout before starting the process? Commented Apr 26, 2018 at 9:57
  • Thanks for your responses. @Med, i tried before starting the process, but still not work. Commented Apr 26, 2018 at 10:06
  • 2
    "I dont want use the kernel.terminate event, since it is not a best practice" Can you tell us why exactly? Commented Apr 26, 2018 at 10:30
  • 1
    Why you think it is a timeout problem? Can you show an output? Did you tried to use /usr/local/bin/php /var/www/project/bin/console command instead php bin/console command? php executable can be found using PhpExecutableFinder component. Commented Apr 26, 2018 at 12:33

2 Answers 2

1

I had to desibale the process output and add '&' after the command Here the answer:

     new Process('php  bin/console app:dosomthing &');

    $commandProcess=new Process('php  bin/console app:dosomthing &');
    $commandProcess->setWorkingDirectory('./../');
    $commandProcess->disableOutput();
    $commandProcess->setTimeout(1800);
    $commandProcess->start();
Sign up to request clarification or add additional context in comments.

Comments

0

I write here the solution that has finally worked for me

First create a service:

use Symfony\Component\Process\Process;
use Symfony\Component\HttpKernel\KernelInterface;


class ProcessService {

public function __construct(
    private KernelInterface $appKernel
){ }

public function executeProcessAsync($processToExecute) {
    $rootDir = $this->appKernel->getProjectDir();
    $process = Process::fromShellCommandline($processToExecute . ' &');
    $process->setWorkingDirectory($rootDir);
    $process->disableOutput();
    $process->setTimeout(0);
    $process->start();
}

}

Then we use the service from a controller:

class MyController {

public function __construct(
    private ProcessService $procesService
){
public function __invoke(Request $request)
{
// (other stuff ...)

    $this->procesService->executeProcessAsync('php bin/console mycommands:command ');

    // (other stuff...)

}
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.