I have some functions taking a lot of time and I want them to be executed in the background and to send an email when it's finished. These functions generate a PDF as Response and if possible, I want this PDF to be attached to email.
This is the standalone function that takes a lot of time :
$passages = $em->getRepository(PasserColle::class)->calculClassementAction($id, $group);
This function is included in my Controller in a function imprimerAction($id, $request) that returns :
return new Response($html2pdf->Output('Classement.pdf'), 200, array('Content-Type' => 'application/pdf'));
I tried to use the Process Component but I can't make it work since I don't understand what to type in the parenthesis :
$process = new Process('ls -lsa');
And how to get the output I want.
You can use RabbitMQ to do this and this bundle for Symfony.
The concept is simple. You'll have Producers who will send messages (with the format you want) and Consumers who will consume these messages. Messages are published in an exchange and will be routed into queue where Consumers are waiting for new messages.
In your example, you can produce a message which tell to Consumers to do a pdf generation and to send email. Publish a message in format JSON for example with, in your case, $id
and $group
and Consumers will do what they have to do.
Follow this link that explain how you can do this.
Otherwise, if you want to use Process Component, you can simple create a Command Console and then do this : $process = new Process('php bin/console yourcommand')
and $process->run()
I just hope it helps.
Best Regards.