Yii framework async request

你离开我真会死。 提交于 2019-12-19 11:21:16

问题


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?


回答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.




回答2:


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 &")


来源:https://stackoverflow.com/questions/10005877/yii-framework-async-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!