1

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

schwaluck
  • 115
  • 9

1 Answers1

0

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.

Lucian Vasile
  • 482
  • 5
  • 17
  • Thanks, I expect I have to create a Command for inserting the entries and starting this command as a process in my controller. I will try this out! :) – schwaluck Oct 03 '22 at 10:46
  • You can also add your entity data to queue (e.q. RabbitMQ) and use Messenger Component to add your entities one by one. This is more complex and complicated solution but efficient. – Oliwer Lisek Oct 03 '22 at 10:58
  • @OliwerLisek Do yo maybe have an example post for an implementation of this? – schwaluck Oct 03 '22 at 11:40
  • 2
    Symfony has a nice documentation for this. https://symfony.com/doc/current/messenger.html Here you are simple tutorial also https://www.evertop.pl/en/how-to-use-rabbitmq-with-symfony-messenger/ – Oliwer Lisek Oct 03 '22 at 14:55
  • @OliwerLisek Thanks I have used the Symfony Messenger Component and it works like a charm. This tutorial series here was really helpfull btw.: https://www.youtube.com/watch?v=Xwn_BNfyjgk – schwaluck Oct 09 '22 at 12:38