mirror of
https://github.com/idanoo/php-resque.git
synced 2024-11-25 01:35:14 +00:00
Change job classes to be instantiated rather than calling methods statically. This obviously makes it easier for state information to be destroyed after a job runs. This change is NOT backwards compatible and requests job classes be rewritten. Jobs also no longer receive arguments in the perform/setUp/tearDown methods but instead are passed as a $args variable to the instantiated job
This commit is contained in:
parent
5dc24ebbe4
commit
5f64653149
@ -6,6 +6,12 @@ and after every single run.
|
||||
* Ability to specify a cluster/multiple redis servers and consistent hash
|
||||
between them (Thanks dceballos)
|
||||
* Fix `APP_INCLUDE` environment variable not loading correctly.
|
||||
* Jobs are no longer defined as static methods, and classes are instantiated
|
||||
first. This change is NOT backwards compatible and requires job classes are
|
||||
updated.
|
||||
* Job arguments are passed to the job class when it is instantiated, and
|
||||
are accessible by $this->args. This change will break existing job classes
|
||||
that rely on arguments that have not been updated.
|
||||
|
||||
## 1.0 (2010-04-18) ##
|
||||
|
||||
|
@ -55,50 +55,45 @@ Jobs are queued as follows:
|
||||
### Defining Jobs ###
|
||||
|
||||
Each job should be in it's own class, and include a `perform` method.
|
||||
It's important to note that classes are called statically.
|
||||
|
||||
class My_Job
|
||||
{
|
||||
public static function perform($args)
|
||||
public function perform()
|
||||
{
|
||||
// Work work work
|
||||
echo $args['name'];
|
||||
echo $this->args['name'];
|
||||
}
|
||||
}
|
||||
|
||||
When the job is run, the class will be instantiated and any arguments
|
||||
will be set as an array on the instantiated object, and are accessible
|
||||
via `$this->args`.
|
||||
|
||||
Any exception thrown by a job will result in the job failing - be
|
||||
careful here and make sure you handle the exceptions that shouldn't
|
||||
result in a job failing.
|
||||
|
||||
Jobs can also have `setUp` and `tearDown` methods. If a `setUp` method
|
||||
is defined, it will be called along with `$args` before the `perform`
|
||||
method is run. The `tearDown` method if defined, will be called with
|
||||
`$args` also, after the job finishes.
|
||||
is defined, it will be called before the `perform` method is run.
|
||||
The `tearDown` method if defined, will be called after the job finishes.
|
||||
|
||||
class My_Job
|
||||
{
|
||||
public static function setUp($args)
|
||||
public function setUp()
|
||||
{
|
||||
// ... Set up environment for this job
|
||||
}
|
||||
|
||||
public static function perform($args)
|
||||
public function perform()
|
||||
{
|
||||
// .. Run job
|
||||
}
|
||||
|
||||
public static function tearDown($args)
|
||||
public function tearDown()
|
||||
{
|
||||
// ... Remove environment for this job
|
||||
}
|
||||
}
|
||||
|
||||
It is **IMPORTANT** to note, that on operating systems where Resque
|
||||
cannot fork to run a job (Mac OS X, or other platforms where the PHP
|
||||
process control functions are unavailable), that because job classes
|
||||
are static, their state will be retained between job calls. **ALWAYS**
|
||||
reset the environment back to how you got it if you're using a `setUp`
|
||||
method, by resetting changes in a `tearDown` method.
|
||||
|
||||
### Tracking Job Statuses ###
|
||||
|
||||
|
@ -129,15 +129,18 @@ class Resque_Job
|
||||
'Job class ' . $this->payload['class'] . ' does not contain a perform method.'
|
||||
);
|
||||
}
|
||||
|
||||
if(method_exists($this->payload['class'], 'setUp')) {
|
||||
call_user_func(array($this->payload['class'], 'setUp'), $this->payload['args']);
|
||||
|
||||
$instance = new $this->payload['class'];
|
||||
$isntance->args = $this->payload['args'];
|
||||
|
||||
if(method_exists($instance, 'setUp')) {
|
||||
$instance->setUp();
|
||||
}
|
||||
|
||||
call_user_func(array($this->payload['class'], 'perform'), $this->payload['args']);
|
||||
|
||||
if(method_exists($this->payload['class'], 'tearDown')) {
|
||||
call_user_func(array($this->payload['class'], 'tearDown'), $this->payload['args']);
|
||||
$instance->perform();
|
||||
|
||||
if(method_exists($instance, 'tearDown')) {
|
||||
$instance->tearDown();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -150,7 +150,6 @@ class Resque_Tests_JobTest extends Resque_Tests_TestCase
|
||||
$job->perform();
|
||||
|
||||
$this->assertTrue(Test_Job_With_SetUp::$called);
|
||||
$this->assertEquals($payload['args'], Test_Job_With_SetUp::$data);
|
||||
}
|
||||
|
||||
public function testJobWithTearDownCallbackFiresSetUp()
|
||||
@ -166,6 +165,5 @@ class Resque_Tests_JobTest extends Resque_Tests_TestCase
|
||||
$job->perform();
|
||||
|
||||
$this->assertTrue(Test_Job_With_TearDown::$called);
|
||||
$this->assertEquals($payload['args'], Test_Job_With_TearDown::$data);
|
||||
}
|
||||
}
|
@ -118,15 +118,14 @@ class Test_Job_Without_Perform_Method
|
||||
class Test_Job_With_SetUp
|
||||
{
|
||||
public static $called = false;
|
||||
public static $data = false;
|
||||
public $args = false;
|
||||
|
||||
public function setUp($data)
|
||||
public function setUp()
|
||||
{
|
||||
self::$called = true;
|
||||
self::$data = $data;
|
||||
}
|
||||
|
||||
public function perform($data)
|
||||
public function perform()
|
||||
{
|
||||
|
||||
}
|
||||
@ -136,16 +135,15 @@ class Test_Job_With_SetUp
|
||||
class Test_Job_With_TearDown
|
||||
{
|
||||
public static $called = false;
|
||||
public static $data = false;
|
||||
public $args = false;
|
||||
|
||||
public function perform($data)
|
||||
public function perform()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public function tearDown($data)
|
||||
public function tearDown()
|
||||
{
|
||||
self::$called = true;
|
||||
self::$data = $data;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user