Change arguments for jobs to an array instead of an object. Also change other json encoded items to decode to an array rather than objects

This commit is contained in:
Chris Boulton 2010-04-19 10:35:50 +10:00
parent 7ef1ebbd97
commit c5396f4e86
10 changed files with 81 additions and 78 deletions

View File

@ -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

View File

@ -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:

View File

@ -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";

View File

@ -73,7 +73,7 @@ class Resque
return;
}
return json_decode($item);
return json_decode($item, true);
}
/**

View File

@ -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) . ')';
}
}

View File

@ -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'];
}
/**

View File

@ -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);
}
}

View File

@ -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']);

View File

@ -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;

View File

@ -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);