Yii framework async request

Go To StackoverFlow.com

1

I have ajax request that do 3 missions:

  • Save Model (DB)
  • Send Email
  • Give success or failed message.

Because this mission takes too time. User can wait up to 20 sec for response (success or failed message). And if the user close the browser its stop in one of the operation that current process for the user.

This is bad user experience.

I want user submit his data to my Controller and after it he will get the "success or failed message". And the process will be completely in the server side and its should support multi sessions.

How can I do that?

2012-04-04 06:20
by Dar
http://www.php.net/manual/en/function.ignore-user-abort.ph - hakre 2012-04-04 06:26
I am little bit confuse here. How can it stop on one of the process? Because once, the user request is sent, the 1st two steps will be done on server. The 3rd step will be on client side and it is just a notify. Basically all 3 steps will be run. Only difference is, user may not see the notification if he closes the browser. Or am i reading it wrong - itachi 2012-04-04 07:22
Basically, You need to change the order so the user get message whatever happens. Because rest of the mission will process and to get from other missions response takes longer then user need. User doesn't care if the mail send succeeded or not or even save to db - Dar 2012-04-04 07:54
I found problem in this function. Its send even the client close the client but if the client didn't close the browser its still processing and its take time until user get answer even if the answer before the processe - Dar 2012-04-04 10:48


1

@hakre What you gave not reduce the time user wait for respond.

I found the best solution for this: runactions extension for yii

This is extension let you run from controller background actions. There are several way to use it. The best one for my case is this

public function actionTimeConsumingProcess()
{
    if (ERunActions::runBackground())
    {
       //do all the stuff that should work in background
       mail->send()
    }
    else
    {
        //this code will be executed immediately
        //echo 'Time-consuming process has been started'
        //user->setFlash ...render ... redirect,
    }
  //User information
  echo "Submit Success!"
}

And its work but without ajax request, When I make ajax request its not working for some reason. So I used:

                         ERunActions::httpPOST($this->createAbsoluteUrl('Form/Submit'), array('to'=>'mail@domain.com', 'subject'=>'This is the subject'));

And its work great but its not the ideal solution for this.

2012-04-05 12:43
by Dar
How can I call mail->send() on ajax cal - Kailas 2014-03-25 12:11
This is not working after save the data in my database. Any solution - Developer 2017-11-27 11:58


1

The problem about runactions extension is that it works only with unauthenticated users, you may use the yii backjob extension, but this will require the use of some kind of non-blocking session storage, such as CHttpDbSession. I am still looking for the right way to do this... I found one of the best options is to run a server backgroud job:

/usr/bin/php -q longProcess.php > /dev/null 2>&1 &

However, I still need to know how to pass controller and action in cmd line and allow yii to actually use it, something like

/usr/bin/php -q yii-index.php controller action > /dev/null 2>&1 &

Update: I found out the best way is to use a yii console application and run it as a background job. the link below helped a lot:

Yii cron jobs

I use this code now and it is working perfectly:

exec("nohup /protected/yiic mycommand /dev/null 2>&1 &")
2014-07-29 17:48
by aszahran
Ads