chrisboulton / php-resque

PHP port of resque (Workers and Queueing)
MIT License
3.43k stars 759 forks source link

static property in job does not work as expect #257

Open zhaosih opened 9 years ago

zhaosih commented 9 years ago

class BaseJob { public static $xx; }

class Job1 extends BaseJob { public function perform() { static::$xx='be static' ; } } class job2 extends BaseJob { public function perform() { echo static::$xx; }
}

after execute Job1, the Job2 doesn't output ‘be static’; Then How can I set some common context for all jobs in a queue?

danhunsaker commented 9 years ago

Each job is executed in its own process, which is forked before the job is even instantiated. So you can't share contexts within PHP. You'd have to store and retrieve the contextual information externally (Redis, SQL, file system, etc) to get this behavior.

zhaosih commented 9 years ago

My context include some complicate objects which hard to serialize. I tried to init them in 'beforeFirstFork' event, but seems the first job start before the event returned.

danhunsaker commented 9 years ago

That event fires once per worker, before it processes any jobs. The jobs still won't be able to share changes, though, because each fork is entirely isolated from the parent process (each job is entirely isolated from the worker itself, in this case).

gcasweb commented 9 years ago

well...

define the variable value in the BaseJob, ex:

class BaseJob{ public static $xx = 'be static'; // if you wanna use this variable in this own class public function perform(){ self :: $xx; // do whatever operation you want } }

and at the others classes use,

class Job1 extends BaseJob{ public function getXX(){ parent :: $xx; // do whatever operation you want to } }

the same for the others classes

class Job2 extends BaseJob{ public function getXX(){ parent :: $xx; // do whatever operation you want to } }

a self variable is something from class, not an instance (like a constant). In PHP, for operations using static, - you don't use the pseudo variable '$this ->', just <self, parent> :: .

I hope helped you :D