I am currently working on a Symfony 6.1 project and have a database insert that needs to generate quite a lot of entries (30k+). I would like to run this insert process as a background task and once all entries have been created in the database, I would like to send an email.
Now I found the following article about Background Task in the Symfony documentation: https://symfony.com/doc/current/components/process.html#running-processes-asynchronously
However, I don't quite understand how to get the process to run in background.
My current code:
$process = new Process(); //not sure what to put in for the constructor
$process->start(function ($data) {
foreach($data as $key => $value) {
$value->setExampleValue("example");
$this->entityManager->persist($value);
$this->entityManager->flush();
}
... email code
});
if (!$process->isSuccessful()) {
throw new ProcessFailedException($process);
}
UPDATE:
As someone noticed I have had a look on the Commands from Symfony. Now I tried to start my custom Command as a Process. However each time I start the Command it fails and the Exit Code is: ().
This is the code:
$process = new Process(['php bin\console app:generate-test']);
//$process->setWorkingDirectory(substr(getcwd(), 0, strrpos(getcwd(), '/')));
$process->start();
if (!$process->isSuccessful()) {
throw new ProcessFailedException($process);
}
However, if I start the command via the console "php bin/console app:generate-test" it works and I get a simple textline printed in my console window.
Has anyone an idea what I am doing wrong here?
FINAL SOLUTION:
Instead of the Process Component from Symfony I switched to the Messenger Component as recommended in the comments.
This works now like a charm!
I found a great tutorial series for understanding and implementing the messenger: https://www.youtube.com/watch?v=Xwn_BNfyjgk
I think you should look at commands (https://symfony.com/doc/current/console.html)
You can then run the command and minimize the console window or use a process manager (like Supervisor) to run it in the background.