0

Would you know how to run a background task on Symfony 4, based on the setup of a form ? This would avoid that the user has to remain on the form until the task is finished. The idea would be that when the form is validated, it starts an independant background task. Then the user can continue its navigation and come back once the task is finished to get the results.

Thanks for your help,

2 Answers 2

1

You need to use pattern Message Bus. Symfony has own implementation of this pattern since version 4.1 introducing Messenger Component.

You can see documentation here: https://symfony.com/doc/current/components/messenger.html

To get it work you need some external program that will implement AMQP protocol. Most popular in PHP world IMHO RabbitMQ.

Sign up to request clarification or add additional context in comments.

3 Comments

Hi, I had a look at the different documentations (Rabbit, symfony messenger component), and I am not sure I see how everything is supposed to work together in my use case. That's why for now I am considering the solution proposed in the second comment, even if this would imply a cron job that would run 24h/7/7. May I ask you to describe a bit more how you would do it ?
You can just laucnh a consume message process and send messages to that process with Messenger. You don't really need RabbitMQ, just the php amqp-extension is enough with Symfony/Messenger.
Hi all, I've tried to set this up : I've created a Message and a MessageHandler to do the data processing I want, and called it from my controller ($bus->dispatch(new MyMessage(params)) : this works perfectly fine. Then I've set the amqp routing in messenger.yaml, so that MessageHandler is executed asynchronously from my form, and user can keep navigating while it is working : transports: amqp: '%env(MESSENGER_TRANSPORT_DSN)%'routing: '*': amqp I now get the following error when validating the form : "There is no active transaction." Any idea how I could solve this?
0

A very simple solution for this could be the following procedure:

  1. Form is valid.
  2. A temporary file is created.
  3. Cronjob gets executed every five minutes and starts a symfony command.
  4. The command checks if the file exists and if it's empty.
  5. If so, the command works of the background task. But before this, the command write it's process id in the file to prevent from beeing excuted a second time.
  6. Remove the file when the command has finished.
  7. As long as the file exists you can show a hint for the user that the task is running.

2 Comments

Hi Fabian, Thanks for your answer, this solution looks great ! One more question : how would you create a cronjob in Symfony ? I do not think it exists natively. If not, which component would you advise (I found that one : github.com/Cron/Symfony-Bundle). Thanks for your feedback,
Create a shell script which calls the command and this shell script you call via the crontab from Linux.

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.