Background job and webuser

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!

Found it myself. Issue was related to my RBAC implementation.

Please consider this topic now an information rather than a question:

With the above modification of ERunActions::touchUrl() the user who initiated the controller action will be available in the second/background call of the action.

Although I don’t know this particular extension, here’s what I guess is happening:

The method ERunActions::runBackground($useHttpClient) will look for the current Http client session. If it cannot find one, it is run in the background as a stand-alone process. Therefore you’ll get a quick response as your session will continue without waiting for the disconnected background process.

When you do have a session id stored, the current session id will wait until the process that is run in the background has finished.

In other words: if you don’t store a session id the action is run asynchronously in the background, otherwise it is run synchronously in the background.

What you may want to achieve is to add some parameters to the background request.

As I don’t know this extension, here are some options to look at:

  • the background process is started by submitting a http request; in this case try to pass a parameter to the url of the http request.

  • the background process is started by reading a request record from a particular database table; add a parameter to this record.

That’s what it actually does. Passing on the PHPSESSID cookie in that request does work. The issue was related to filtering this particular request in RBAC.