Yii Framework Forum: Background job and webuser - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Background job and webuser Rate Topic: -----

#1 User is offline   fl007 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 27-July 11

Posted 30 July 2012 - 08:43 AM

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!

This post has been edited by fl007: 30 July 2012 - 09:22 AM

0

#2 User is offline   fl007 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 27-July 11

Posted 30 July 2012 - 09:20 AM

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.
0

#3 User is offline   Onman 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 175
  • Joined: 26-December 09
  • Location:The Netherlands

Posted 30 July 2012 - 09:27 AM

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.
0

#4 User is offline   fl007 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 27-July 11

Posted 30 July 2012 - 09:44 AM

View PostOnman, on 30 July 2012 - 09:27 AM, said:

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


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.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users