Run the symfony2 command in the background and try out via Ajax

With the SF2 process component, you can run asynchronous background tasks from the controller.

Now I want to write javascript that periodically checks this command and prints the result of this task to inform the user about what is happening.

Is there a way to save the process and get information on multiple requests so that I can poll it using an ajax request?

+6
source share
1 answer

I am using JMSJobQueueBundle ( Github , docs ) to create background processes and store the queue (with metadata) in the database.

After installing this package, you simply save the task in your database using the command (and optional arguments and options):

$job = new Job('my-symfony2:command', array('some-args', 'or', '--options="foo"')); $em->persist($job); $em->flush($job); 

The package executes this command (after setting up supervisord or cron job), and it stores the output of your command in the database. If your job does not work, it even stores the stack trace in the database. Now you can get the job status in your application (you can use $job->getState() to find out if it is finished or not). You can find the Job object here to see what is stored in the database by default.

I used this method when sending many emails or creating large csv export shipments and it works great.

0
source

Source: https://habr.com/ru/post/988094/


All Articles