I have ajax request that do 3 missions:
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?
@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.
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:
I use this code now and it is working perfectly:
exec("nohup /protected/yiic mycommand /dev/null 2>&1 &")