php-resque icon indicating copy to clipboard operation
php-resque copied to clipboard

static property in job does not work as expect

Open zhaosih opened this issue 9 years ago • 4 comments

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?

zhaosih avatar Jun 12 '15 08:06 zhaosih

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.

danhunsaker avatar Jun 13 '15 01:06 danhunsaker

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.

zhaosih avatar Jun 13 '15 04:06 zhaosih

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

danhunsaker avatar Jun 14 '15 01:06 danhunsaker

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

gcasweb avatar Jun 15 '15 20:06 gcasweb