The Process Component of Symfony allows to execute a command in a subprocess. This is helpful for example if you want the user to start a long lasting command that shouldn’t block the response without using some kind of message queue.
Nevertheless, when the response is send to the client, the process will automatically be terminated. In this case a SIGTERM signal is send which will cause the running process to stop. To avoid this and keep the process running even if the calling request was ended you simply have to override the “__destruct()” method of the Process class.
<?php
namespace App\Application\Component;
use Symfony\Component\Process\Process;
class AsyncProcess extends Process
{
/**
* Avoid stopping the running process when SIGTERM is received
*/
public function __destruct() {}
}
Now you can use the “AsyncProcess” class to start a process by using the start() method. For example:
$process = new AsyncProcess(['bin/console', 'cache:clear']);
$process->start();// you might want to do other stuff here
// or send a response to end the request
// the process will keep running until it is finished
Thanks to https://github.com/ttk for mentioning that solution here:
https://github.com/symfony/symfony/issues/7237#issuecomment-583181010