views:

187

answers:

1

In the admin panel, when the admin selects an action it will load data from an API and save it to the DB. It could take from 2 seconds to 3 minutes, depending on what he is importing.

How can I do in Symfony to create like a thread, so the admin is not waiting to finish. And when it's finished tell him to check the result?

Are the tasks the answer for this problem?

thanks!

+1  A: 

Not symfony-specific, but you'll want to kick off some background process.

I'm not familiar with symfony's tooling for command-line scripts, but I think there is stuff you can use.

Then in your controller, you want something roughly like (assuming you're on a unixy host):

public function executeYourBackgroundTask(){

    // first, you might want to create some kind of entry in a table to keep track of jobs.
    // Imagine you've got a table to keep track of this stuff
    $job = new Backgroundjob();
    $job->user_id = $this->getUser()->getId();
    $job->starttime = time();
    $job->someArgument = $someArgument; //anything the job script needs for input.
    $job->save();


    $jobId = $job->getId();

    //start a job in the background.
    exec('php /path/to/your/background/script.php ' . $jobId .' &');

    //your view should just tell the user "Your job is being processed, you'll be notified when it is done"
}

Your background process (in /path/to/your/background/script.php) should take the passed jobId, grab the job record, and use any stored inputs to run the job. When it's done grabbing data and stuffing it into the database, it should do set an endtime in the table (which marks the job as complete), and then do whatever you want to do to notify the user (send an email, or insert some kind of row into a messages table, etc)

timdev
thanks, I've seen that I can do that with pakeApp
fesja