What i am trying to achieve: To run a background job on the server, using extension runactions, with the extra requirement to have access to the original user from within the background job. As explained here, this is not supported by the extension.
My idea is to forward the PHPSESSID with the request that ERunActions::runBackground() initiates for the background task, so i modified ERunActions::touchUrlExt() as follows:
public static function touchUrlExt($url,$postData=null,$contentType=null,$httpClientConfig=array())
{
// ...
// > INSERTED
$client->setCookie('PHPSESSID', $_COOKIE['PHPSESSID']);
// <
$client->request($method);
}
To test this, I put the following action inside a controller:
public function actionTest($id)
{
if (ERunActions::runBackground($useHttpClient = true))
{
$uid = Yii::app()->user->id;
Yii::log("Starting background for user $uid...");
usleep(10 * 1000000);
Yii::log("...finished.");
Yii::app()->end();
}
else
{
Yii::log("Foreground.");
Yii::app()->user->setFlash('notice', 'Foreground.');
$this->redirect(array('view','id'=>$id));
}
}
If I do not set the cokie (unmodified ERunActions::touchUrlExt()), I receive the page redirected to immediately, and the background does its job, but Yii::app()->user->id is undefined (as expected). With the modified Version (setting the PHPSESSID-Cookie) the background job gets the right user id, but the redirection only occurs after the background process finished (and flash message not appearing). The foreground log message is written immediately, though. The redirection appears to be blocked in some way.
I guess its not a runactions issue, but rather my approach is just too naive. If somebody could enlighten me…I appreciate!