mirror of
https://github.com/idanoo/php-resque.git
synced 2024-11-22 00:11:53 +00:00
Add setUp and tearDown callbacks for jobs
This commit is contained in:
parent
94fed1cfb4
commit
6e6d7ad859
@ -1,6 +1,8 @@
|
|||||||
## 1.1 (????-??-??) ##
|
## 1.1 (????-??-??) ##
|
||||||
* Change arguments for jobs to be an array as they're easier to work with in
|
* Change arguments for jobs to be an array as they're easier to work with in
|
||||||
PHP
|
PHP.
|
||||||
|
* Implement ability to have setUp and tearDown methods for jobs, called before
|
||||||
|
and after every single run.
|
||||||
|
|
||||||
## 1.0 (2010-04-18) ##
|
## 1.0 (2010-04-18) ##
|
||||||
|
|
||||||
|
@ -33,6 +33,8 @@ In addition, it also:
|
|||||||
* Has the ability to track the status of jobs
|
* Has the ability to track the status of jobs
|
||||||
* Will mark a job as failed, if a forked child running a job does
|
* Will mark a job as failed, if a forked child running a job does
|
||||||
not exit with a status code as 0
|
not exit with a status code as 0
|
||||||
|
* Has built in support for `setUp` and `tearDown` methods, called
|
||||||
|
pre and post jobs
|
||||||
|
|
||||||
## Jobs ##
|
## Jobs ##
|
||||||
|
|
||||||
@ -68,6 +70,36 @@ 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
|
careful here and make sure you handle the exceptions that shouldn't
|
||||||
result in a job failing.
|
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.
|
||||||
|
|
||||||
|
class My_Job
|
||||||
|
{
|
||||||
|
public static function setUp($args)
|
||||||
|
{
|
||||||
|
// ... Set up environment for this job
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function perform($args)
|
||||||
|
{
|
||||||
|
// .. Run job
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function tearDown($args)
|
||||||
|
{
|
||||||
|
// ... 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 ###
|
### Tracking Job Statuses ###
|
||||||
|
|
||||||
php-resque has the ability to perform basic status tracking of a queued
|
php-resque has the ability to perform basic status tracking of a queued
|
||||||
|
@ -1,8 +1,10 @@
|
|||||||
* Write tests for:
|
* Write tests for:
|
||||||
* `Resque_Failure`
|
* `Resque_Failure`
|
||||||
* `Resque_Failure_Redis`
|
* `Resque_Failure_Redis`
|
||||||
* Plugin/hook type system similar to Ruby version
|
* Plugin/hook type system similar to Ruby version (when done, implement the
|
||||||
|
setUp and tearDown methods as a plugin)
|
||||||
* Change to preforking worker model
|
* Change to preforking worker model
|
||||||
* Clean up /bin and /demo
|
* Clean up /bin and /demo
|
||||||
* Add a way to store arbitrary text in job statuses (for things like progress
|
* Add a way to store arbitrary text in job statuses (for things like progress
|
||||||
indicators)
|
indicators)
|
||||||
|
* Write plugin for Ruby resque that calls setUp and tearDown methods
|
@ -129,8 +129,16 @@ class Resque_Job
|
|||||||
'Job class ' . $this->payload['class'] . ' does not contain a perform method.'
|
'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']);
|
||||||
|
}
|
||||||
|
|
||||||
call_user_func(array($this->payload['class'], 'perform'), $this->payload['args']);
|
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']);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -136,4 +136,36 @@ class Resque_Tests_JobTest extends Resque_Tests_TestCase
|
|||||||
$job->worker = $this->worker;
|
$job->worker = $this->worker;
|
||||||
$job->perform();
|
$job->perform();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testJobWithSetUpCallbackFiresSetUp()
|
||||||
|
{
|
||||||
|
$payload = array(
|
||||||
|
'class' => 'Test_Job_With_SetUp',
|
||||||
|
'args' => array(
|
||||||
|
'somevar',
|
||||||
|
'somevar2',
|
||||||
|
),
|
||||||
|
);
|
||||||
|
$job = new Resque_Job('jobs', $payload);
|
||||||
|
$job->perform();
|
||||||
|
|
||||||
|
$this->assertTrue(Test_Job_With_SetUp::$called);
|
||||||
|
$this->assertEquals($payload['args'], Test_Job_With_SetUp::$data);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testJobWithTearDownCallbackFiresSetUp()
|
||||||
|
{
|
||||||
|
$payload = array(
|
||||||
|
'class' => 'Test_Job_With_TearDown',
|
||||||
|
'args' => array(
|
||||||
|
'somevar',
|
||||||
|
'somevar2',
|
||||||
|
),
|
||||||
|
);
|
||||||
|
$job = new Resque_Job('jobs', $payload);
|
||||||
|
$job->perform();
|
||||||
|
|
||||||
|
$this->assertTrue(Test_Job_With_TearDown::$called);
|
||||||
|
$this->assertEquals($payload['args'], Test_Job_With_TearDown::$data);
|
||||||
|
}
|
||||||
}
|
}
|
@ -113,4 +113,39 @@ class Failing_Job
|
|||||||
class Test_Job_Without_Perform_Method
|
class Test_Job_Without_Perform_Method
|
||||||
{
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
class Test_Job_With_SetUp
|
||||||
|
{
|
||||||
|
public static $called = false;
|
||||||
|
public static $data = false;
|
||||||
|
|
||||||
|
public function setUp($data)
|
||||||
|
{
|
||||||
|
self::$called = true;
|
||||||
|
self::$data = $data;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function perform($data)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class Test_Job_With_TearDown
|
||||||
|
{
|
||||||
|
public static $called = false;
|
||||||
|
public static $data = false;
|
||||||
|
|
||||||
|
public function perform($data)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function tearDown($data)
|
||||||
|
{
|
||||||
|
self::$called = true;
|
||||||
|
self::$data = $data;
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user