diff --git a/CHANGELOG.markdown b/CHANGELOG.markdown index 98de83f..de4182d 100644 --- a/CHANGELOG.markdown +++ b/CHANGELOG.markdown @@ -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) ## diff --git a/README.markdown b/README.markdown index 0f2289c..7a19252 100644 --- a/README.markdown +++ b/README.markdown @@ -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 ### diff --git a/lib/Resque/Job.php b/lib/Resque/Job.php index afcea17..2831bf4 100644 --- a/lib/Resque/Job.php +++ b/lib/Resque/Job.php @@ -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(); } } diff --git a/test/Resque/Tests/JobTest.php b/test/Resque/Tests/JobTest.php index 9e3b461..1d1db56 100644 --- a/test/Resque/Tests/JobTest.php +++ b/test/Resque/Tests/JobTest.php @@ -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); } } \ No newline at end of file diff --git a/test/Resque/Tests/bootstrap.php b/test/Resque/Tests/bootstrap.php index d2efc27..e21707c 100644 --- a/test/Resque/Tests/bootstrap.php +++ b/test/Resque/Tests/bootstrap.php @@ -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; } } \ No newline at end of file