Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I do background parsing of data with Symfony2?

I write a web application in PHP with Symfony2. The user can upload a CSV file with data that is saved to the database. The parsing of each row of the CSV file last about 0.2 seconds because I make some requests to the Google Maps API.

So when you upload a CSV file with 5000 rows, which is a realistic case in my app, it may take 16 minutes to parse the whole file.

I don't want that the user must wait 16 minutes until he can continue using my app. So my question is: how can I parse the CSV file in the background, so that the user can continue surfing?

like image 806
rotespferd Avatar asked Dec 15 '22 23:12

rotespferd


1 Answers

You can create a kernel.terminate event listener and do your parsing there. This event fires after response is sent to the browser. Sample implementation would be,

Service declaration,

//services.yml
csv_parse_listener:
    class: FQCN\Of\CsvParseListener
    tags:
        - { name: kernel.event_listener, event: kernel.terminate, method: onKernelTerminate }

Listener class,

namespace Your\namespace;
use Symfony\Component\HttpKernel\Event\PostResponseEvent;

class CsvParseListener
{
    public function onKernelTerminate(PostResponseEvent $event)
    {
        $request = $event->getRequest();
        if($request->get('_route') !== "Your_route"){
            return;
        }

        $csvFile = $request->files->get('file_field_name');

        //move file using $csvFile->move()
        //read and parse
    }
}
like image 138
Mun Mun Das Avatar answered Jan 13 '23 05:01

Mun Mun Das