diff --git a/CHANGELOG.markdown b/CHANGELOG.markdown index 9944cd8..59b9d10 100644 --- a/CHANGELOG.markdown +++ b/CHANGELOG.markdown @@ -1,3 +1,7 @@ +## 1.1 (????-??-??) ## +* Change arguments for jobs to be an array as they're easier to work with in +PHP + ## 1.0 (2010-04-18) ## * Initial release \ No newline at end of file diff --git a/README.markdown b/README.markdown index 838102b..b8aa207 100644 --- a/README.markdown +++ b/README.markdown @@ -45,8 +45,9 @@ Jobs are queued as follows: // Required if redis is located elsewhere Resque::setBackend('localhost', 6379); - $args = new stdClass; - $args->name = 'Chris'; + $args = array( + 'name' => 'Chris' + ); Resque::enqueue('default', 'My_Job', $args); ### Defining Jobs ### @@ -59,7 +60,7 @@ It's important to note that classes are called statically. public static function perform($args) { // Work work work - echo $args->name; + echo $args['name']; } } @@ -77,7 +78,7 @@ To track the status of a job, pass `true` as the fourth argument to `Resque::enqueue`. A token used for tracking the job status will be returned: - $token = Resque::enqueue('default', 'My_Job', $args); + $token = Resque::enqueue('default', 'My_Job', $args, true); echo $token; To fetch the status of a job: diff --git a/demo/queue.php b/demo/queue.php index 29ac3ad..6a94e44 100644 --- a/demo/queue.php +++ b/demo/queue.php @@ -7,12 +7,12 @@ require '../lib/Resque.php'; date_default_timezone_set('GMT'); Resque::setBackend('127.0.0.1:6379'); -$class = new stdClass; -$class->test = 'test'; - -$args = new stdClass; -$args->time = time(); -$args->class = $class; +$args = array( + 'time' => time(), + 'array' => array( + 'test' => 'test', + ), +); $jobId = Resque::enqueue('default', $argv[1], $args, true); echo "Queued job ".$jobId."\n\n"; diff --git a/lib/Resque.php b/lib/Resque.php index ebb1c8a..705f1eb 100644 --- a/lib/Resque.php +++ b/lib/Resque.php @@ -73,7 +73,7 @@ class Resque return; } - return json_decode($item); + return json_decode($item, true); } /** diff --git a/lib/Resque/Job.php b/lib/Resque/Job.php index 9553e57..f165f2f 100644 --- a/lib/Resque/Job.php +++ b/lib/Resque/Job.php @@ -43,14 +43,14 @@ class Resque_Job * * @param string $queue The name of the queue to place the job in. * @param string $class The name of the class that contains the code to execute the job. - * @param object $args Any optional arguments that should be passed when the job is executed. Pass as a class. + * @param array $args Any optional arguments that should be passed when the job is executed. * @param boolean $monitor Set to true to be able to monitor the status of a job. */ public static function create($queue, $class, $args = null, $monitor = false) { - if($args !== null && !is_object($args)) { + if($args !== null && !is_array($args)) { throw new InvalidArgumentException( - 'Supplied $args must be an object and an instance of stdClass.' + 'Supplied $args must be an array.' ); } $id = md5(uniqid('', true)); @@ -95,7 +95,7 @@ class Resque_Job return; } - $statusInstance = new Resque_Job_Status($this->payload->id); + $statusInstance = new Resque_Job_Status($this->payload['id']); $statusInstance->update($status); } @@ -106,7 +106,7 @@ class Resque_Job */ public function getStatus() { - $status = new Resque_Job_Status($this->payload->id); + $status = new Resque_Job_Status($this->payload['id']); return $status->get(); } @@ -118,19 +118,19 @@ class Resque_Job */ public function perform() { - if(!class_exists($this->payload->class)) { + if(!class_exists($this->payload['class'])) { throw new Resque_Exception( - 'Could not find job class ' . $this->payload->class . '.' + 'Could not find job class ' . $this->payload['class'] . '.' ); } - if(!method_exists($this->payload->class, 'perform')) { + if(!method_exists($this->payload['class'], 'perform')) { throw new Resque_Exception( - 'Job class ' . $this->payload->class . ' does not contain a perform method.' + 'Job class ' . $this->payload['class'] . ' does not contain a perform method.' ); } - call_user_func(array($this->payload->class, 'perform'), $this->payload->args); + call_user_func(array($this->payload['class'], 'perform'), $this->payload['args']); } /** @@ -155,13 +155,13 @@ class Resque_Job */ public function recreate() { - $status = new Resque_Job_Status($this->payload->id); + $status = new Resque_Job_Status($this->payload['id']); $monitor = false; if($status->isTracking()) { $monitor = true; } - return self::create($this->queue, $this->payload->class, $this->payload->args, $monitor); + return self::create($this->queue, $this->payload['class'], $this->payload['args'], $monitor); } /** @@ -171,24 +171,16 @@ class Resque_Job */ public function __toString() { - $args = array(); - if(isset($this->payload->args)) { - $args = $this->payload->args; - foreach($args as $k => $v) { - if(is_object($v)) { - $args[$k] = '{' . get_class($v) . ' - '.implode(',', get_object_vars($v)) . '}'; - } - } - } - $name = array( 'Job{' . $this->queue .'}' ); - if(!empty($this->payload->id)) { - $name[] = 'ID: ' . $this->payload->id; + if(!empty($this->payload['id'])) { + $name[] = 'ID: ' . $this->payload['id']; + } + $name[] = $this->payload['class']; + if(!empty($this->payload['args'])) { + $name[] = json_encode($this->payload['args']); } - $name[] = $this->payload->class; - $name[] = implode(',', $args); return '(' . implode(' | ', $name) . ')'; } } diff --git a/lib/Resque/Job/Status.php b/lib/Resque/Job/Status.php index 8b45e98..e1554b0 100644 --- a/lib/Resque/Job/Status.php +++ b/lib/Resque/Job/Status.php @@ -115,12 +115,12 @@ class Resque_Job_Status return false; } - $statusPacket = json_decode(Resque::redis()->get((string)$this)); + $statusPacket = json_decode(Resque::redis()->get((string)$this), true); if(!$statusPacket) { return false; } - return $statusPacket->status; + return $statusPacket['status']; } /** diff --git a/lib/Resque/Worker.php b/lib/Resque/Worker.php index e2516d3..1ee24a9 100644 --- a/lib/Resque/Worker.php +++ b/lib/Resque/Worker.php @@ -543,10 +543,10 @@ class Resque_Worker { $job = Resque::redis()->get('worker:' . $this); if(!$job) { - return new stdClass; + return array(); } else { - return json_decode($job); + return json_decode($job, true); } } diff --git a/resque.php b/resque.php index 9643346..eb6aec9 100644 --- a/resque.php +++ b/resque.php @@ -16,7 +16,7 @@ if(!empty($_ENV['APP_INCLUDE'])) { } require 'lib/Resque.php'; -require 'Resque/Worker.php'; +require 'lib/Resque/Worker.php'; if(!empty($_ENV['REDIS_BACKEND'])) { Resque::setBackend($_ENV['REDIS_BACKEND']); diff --git a/test/Resque/Tests/JobTest.php b/test/Resque/Tests/JobTest.php index 061dda3..4505edf 100644 --- a/test/Resque/Tests/JobTest.php +++ b/test/Resque/Tests/JobTest.php @@ -41,35 +41,36 @@ class Resque_Tests_JobTest extends Resque_Tests_TestCase $this->fail('Job could not be reserved.'); } $this->assertEquals('jobs', $job->queue); - $this->assertEquals('Test_Job', $job->payload->class); + $this->assertEquals('Test_Job', $job->payload['class']); } /** * @expectedException InvalidArgumentException */ - public function testArrayArgumentsCannotBePassedToJob() + public function testObjectArgumentsCannotBePassedToJob() { - Resque::enqueue('jobs', 'Test_Job', array( - 'test' - )); + $args = new stdClass; + $args->test = 'somevalue'; + Resque::enqueue('jobs', 'Test_Job', $args); } public function testQueuedJobReturnsExactSamePassedInArguments() { - $args = new stdClass; - $args->int = 123; - $args->numArray = array( - 1, - 2, + $args = array( + 'int' => 123, + 'numArray' => array( + 1, + 2, + ), + 'assocArray' => array( + 'key1' => 'value1', + 'key2' => 'value2' + ), ); - $args->assocArray = new stdClass; - $args->assocArray->key1 = 'value1'; - $args->assocArray->key2 = 'value2'; - Resque::enqueue('jobs', 'Test_Job', $args); $job = Resque_Job::reserve('jobs'); - $this->assertEquals($args, $job->payload->args); + $this->assertEquals($args, $job->payload['args']); } public function testAfterJobIsReservedItIsRemoved() @@ -81,15 +82,17 @@ class Resque_Tests_JobTest extends Resque_Tests_TestCase public function testRecreatedJobMatchesExistingJob() { - $args = new stdClass; - $args->int = 123; - $args->numArray = array( - 1, - 2, + $args = array( + 'int' => 123, + 'numArray' => array( + 1, + 2, + ), + 'assocArray' => array( + 'key1' => 'value1', + 'key2' => 'value2' + ), ); - $args->assocArray = new stdClass; - $args->assocArray->key1 = 'value1'; - $args->assocArray->key2 = 'value2'; Resque::enqueue('jobs', 'Test_Job', $args); $job = Resque_Job::reserve('jobs'); @@ -98,15 +101,16 @@ class Resque_Tests_JobTest extends Resque_Tests_TestCase $job->recreate(); $newJob = Resque_Job::reserve('jobs'); - $this->assertEquals($job->payload->class, $newJob->payload->class); - $this->assertEquals($job->payload->args, $newJob->payload->args); + $this->assertEquals($job->payload['class'], $newJob->payload['class']); + $this->assertEquals($job->payload['args'], $newJob->payload['args']); } public function testFailedJobExceptionsAreCaught() { - $payload = new stdClass; - $payload->class = 'Failing_Job'; - $payload->args = null; + $payload = array( + 'class' => 'Failing_Job', + 'args' => null + ); $job = new Resque_Job('jobs', $payload); $job->worker = $this->worker; diff --git a/test/Resque/Tests/WorkerTest.php b/test/Resque/Tests/WorkerTest.php index 04f7c23..e9ab120 100644 --- a/test/Resque/Tests/WorkerTest.php +++ b/test/Resque/Tests/WorkerTest.php @@ -153,7 +153,7 @@ class Resque_Tests_WorkerTest extends Resque_Tests_TestCase $job = $worker->reserve(); $worker->workingOn($job); $worker->doneWorking(); - $this->assertEquals(new stdClass, $worker->job()); + $this->assertEquals(array(), $worker->job()); } public function testWorkerRecordsWhatItIsWorkingOn() @@ -161,17 +161,18 @@ class Resque_Tests_WorkerTest extends Resque_Tests_TestCase $worker = new Resque_Worker('jobs'); $worker->registerWorker(); - $payload = new stdClass; - $payload->class = 'Test_Job'; + $payload = array( + 'class' => 'Test_Job' + ); $job = new Resque_Job('jobs', $payload); $worker->workingOn($job); $job = $worker->job(); - $this->assertEquals('jobs', $job->queue); - if(!isset($job->run_at)) { + $this->assertEquals('jobs', $job['queue']); + if(!isset($job['run_at'])) { $this->fail('Job does not have run_at time'); } - $this->assertEquals($payload, $job->payload); + $this->assertEquals($payload, $job['payload']); } public function testWorkerErasesItsStatsWhenShutdown() @@ -239,8 +240,9 @@ class Resque_Tests_WorkerTest extends Resque_Tests_TestCase $worker = new Resque_Worker('jobs'); $worker->registerWorker(); - $payload = new stdClass; - $payload->class = 'Test_Job'; + $payload = array( + 'class' => 'Test_Job' + ); $job = new Resque_Job('jobs', $payload); $worker->workingOn($job);