mirror of
https://github.com/idanoo/php-resque.git
synced 2024-11-22 08:15:14 +00:00
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:
parent
7ef1ebbd97
commit
c5396f4e86
@ -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) ##
|
## 1.0 (2010-04-18) ##
|
||||||
|
|
||||||
* Initial release
|
* Initial release
|
@ -45,8 +45,9 @@ Jobs are queued as follows:
|
|||||||
// Required if redis is located elsewhere
|
// Required if redis is located elsewhere
|
||||||
Resque::setBackend('localhost', 6379);
|
Resque::setBackend('localhost', 6379);
|
||||||
|
|
||||||
$args = new stdClass;
|
$args = array(
|
||||||
$args->name = 'Chris';
|
'name' => 'Chris'
|
||||||
|
);
|
||||||
Resque::enqueue('default', 'My_Job', $args);
|
Resque::enqueue('default', 'My_Job', $args);
|
||||||
|
|
||||||
### Defining Jobs ###
|
### Defining Jobs ###
|
||||||
@ -59,7 +60,7 @@ It's important to note that classes are called statically.
|
|||||||
public static function perform($args)
|
public static function perform($args)
|
||||||
{
|
{
|
||||||
// Work work work
|
// 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
|
`Resque::enqueue`. A token used for tracking the job status will be
|
||||||
returned:
|
returned:
|
||||||
|
|
||||||
$token = Resque::enqueue('default', 'My_Job', $args);
|
$token = Resque::enqueue('default', 'My_Job', $args, true);
|
||||||
echo $token;
|
echo $token;
|
||||||
|
|
||||||
To fetch the status of a job:
|
To fetch the status of a job:
|
||||||
|
@ -7,12 +7,12 @@ require '../lib/Resque.php';
|
|||||||
date_default_timezone_set('GMT');
|
date_default_timezone_set('GMT');
|
||||||
Resque::setBackend('127.0.0.1:6379');
|
Resque::setBackend('127.0.0.1:6379');
|
||||||
|
|
||||||
$class = new stdClass;
|
$args = array(
|
||||||
$class->test = 'test';
|
'time' => time(),
|
||||||
|
'array' => array(
|
||||||
$args = new stdClass;
|
'test' => 'test',
|
||||||
$args->time = time();
|
),
|
||||||
$args->class = $class;
|
);
|
||||||
|
|
||||||
$jobId = Resque::enqueue('default', $argv[1], $args, true);
|
$jobId = Resque::enqueue('default', $argv[1], $args, true);
|
||||||
echo "Queued job ".$jobId."\n\n";
|
echo "Queued job ".$jobId."\n\n";
|
||||||
|
@ -73,7 +73,7 @@ class Resque
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
return json_decode($item);
|
return json_decode($item, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -43,14 +43,14 @@ class Resque_Job
|
|||||||
*
|
*
|
||||||
* @param string $queue The name of the queue to place the job in.
|
* @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 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.
|
* @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)
|
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(
|
throw new InvalidArgumentException(
|
||||||
'Supplied $args must be an object and an instance of stdClass.'
|
'Supplied $args must be an array.'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
$id = md5(uniqid('', true));
|
$id = md5(uniqid('', true));
|
||||||
@ -95,7 +95,7 @@ class Resque_Job
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$statusInstance = new Resque_Job_Status($this->payload->id);
|
$statusInstance = new Resque_Job_Status($this->payload['id']);
|
||||||
$statusInstance->update($status);
|
$statusInstance->update($status);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -106,7 +106,7 @@ class Resque_Job
|
|||||||
*/
|
*/
|
||||||
public function getStatus()
|
public function getStatus()
|
||||||
{
|
{
|
||||||
$status = new Resque_Job_Status($this->payload->id);
|
$status = new Resque_Job_Status($this->payload['id']);
|
||||||
return $status->get();
|
return $status->get();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -118,19 +118,19 @@ class Resque_Job
|
|||||||
*/
|
*/
|
||||||
public function perform()
|
public function perform()
|
||||||
{
|
{
|
||||||
if(!class_exists($this->payload->class)) {
|
if(!class_exists($this->payload['class'])) {
|
||||||
throw new Resque_Exception(
|
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(
|
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()
|
public function recreate()
|
||||||
{
|
{
|
||||||
$status = new Resque_Job_Status($this->payload->id);
|
$status = new Resque_Job_Status($this->payload['id']);
|
||||||
$monitor = false;
|
$monitor = false;
|
||||||
if($status->isTracking()) {
|
if($status->isTracking()) {
|
||||||
$monitor = true;
|
$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()
|
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(
|
$name = array(
|
||||||
'Job{' . $this->queue .'}'
|
'Job{' . $this->queue .'}'
|
||||||
);
|
);
|
||||||
if(!empty($this->payload->id)) {
|
if(!empty($this->payload['id'])) {
|
||||||
$name[] = 'ID: ' . $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) . ')';
|
return '(' . implode(' | ', $name) . ')';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -115,12 +115,12 @@ class Resque_Job_Status
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$statusPacket = json_decode(Resque::redis()->get((string)$this));
|
$statusPacket = json_decode(Resque::redis()->get((string)$this), true);
|
||||||
if(!$statusPacket) {
|
if(!$statusPacket) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $statusPacket->status;
|
return $statusPacket['status'];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -543,10 +543,10 @@ class Resque_Worker
|
|||||||
{
|
{
|
||||||
$job = Resque::redis()->get('worker:' . $this);
|
$job = Resque::redis()->get('worker:' . $this);
|
||||||
if(!$job) {
|
if(!$job) {
|
||||||
return new stdClass;
|
return array();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return json_decode($job);
|
return json_decode($job, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -16,7 +16,7 @@ if(!empty($_ENV['APP_INCLUDE'])) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
require 'lib/Resque.php';
|
require 'lib/Resque.php';
|
||||||
require 'Resque/Worker.php';
|
require 'lib/Resque/Worker.php';
|
||||||
|
|
||||||
if(!empty($_ENV['REDIS_BACKEND'])) {
|
if(!empty($_ENV['REDIS_BACKEND'])) {
|
||||||
Resque::setBackend($_ENV['REDIS_BACKEND']);
|
Resque::setBackend($_ENV['REDIS_BACKEND']);
|
||||||
|
@ -41,35 +41,36 @@ class Resque_Tests_JobTest extends Resque_Tests_TestCase
|
|||||||
$this->fail('Job could not be reserved.');
|
$this->fail('Job could not be reserved.');
|
||||||
}
|
}
|
||||||
$this->assertEquals('jobs', $job->queue);
|
$this->assertEquals('jobs', $job->queue);
|
||||||
$this->assertEquals('Test_Job', $job->payload->class);
|
$this->assertEquals('Test_Job', $job->payload['class']);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @expectedException InvalidArgumentException
|
* @expectedException InvalidArgumentException
|
||||||
*/
|
*/
|
||||||
public function testArrayArgumentsCannotBePassedToJob()
|
public function testObjectArgumentsCannotBePassedToJob()
|
||||||
{
|
{
|
||||||
Resque::enqueue('jobs', 'Test_Job', array(
|
$args = new stdClass;
|
||||||
'test'
|
$args->test = 'somevalue';
|
||||||
));
|
Resque::enqueue('jobs', 'Test_Job', $args);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testQueuedJobReturnsExactSamePassedInArguments()
|
public function testQueuedJobReturnsExactSamePassedInArguments()
|
||||||
{
|
{
|
||||||
$args = new stdClass;
|
$args = array(
|
||||||
$args->int = 123;
|
'int' => 123,
|
||||||
$args->numArray = array(
|
'numArray' => array(
|
||||||
1,
|
1,
|
||||||
2,
|
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);
|
Resque::enqueue('jobs', 'Test_Job', $args);
|
||||||
$job = Resque_Job::reserve('jobs');
|
$job = Resque_Job::reserve('jobs');
|
||||||
|
|
||||||
$this->assertEquals($args, $job->payload->args);
|
$this->assertEquals($args, $job->payload['args']);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testAfterJobIsReservedItIsRemoved()
|
public function testAfterJobIsReservedItIsRemoved()
|
||||||
@ -81,15 +82,17 @@ class Resque_Tests_JobTest extends Resque_Tests_TestCase
|
|||||||
|
|
||||||
public function testRecreatedJobMatchesExistingJob()
|
public function testRecreatedJobMatchesExistingJob()
|
||||||
{
|
{
|
||||||
$args = new stdClass;
|
$args = array(
|
||||||
$args->int = 123;
|
'int' => 123,
|
||||||
$args->numArray = array(
|
'numArray' => array(
|
||||||
1,
|
1,
|
||||||
2,
|
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);
|
Resque::enqueue('jobs', 'Test_Job', $args);
|
||||||
$job = Resque_Job::reserve('jobs');
|
$job = Resque_Job::reserve('jobs');
|
||||||
@ -98,15 +101,16 @@ class Resque_Tests_JobTest extends Resque_Tests_TestCase
|
|||||||
$job->recreate();
|
$job->recreate();
|
||||||
|
|
||||||
$newJob = Resque_Job::reserve('jobs');
|
$newJob = Resque_Job::reserve('jobs');
|
||||||
$this->assertEquals($job->payload->class, $newJob->payload->class);
|
$this->assertEquals($job->payload['class'], $newJob->payload['class']);
|
||||||
$this->assertEquals($job->payload->args, $newJob->payload->args);
|
$this->assertEquals($job->payload['args'], $newJob->payload['args']);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testFailedJobExceptionsAreCaught()
|
public function testFailedJobExceptionsAreCaught()
|
||||||
{
|
{
|
||||||
$payload = new stdClass;
|
$payload = array(
|
||||||
$payload->class = 'Failing_Job';
|
'class' => 'Failing_Job',
|
||||||
$payload->args = null;
|
'args' => null
|
||||||
|
);
|
||||||
$job = new Resque_Job('jobs', $payload);
|
$job = new Resque_Job('jobs', $payload);
|
||||||
$job->worker = $this->worker;
|
$job->worker = $this->worker;
|
||||||
|
|
||||||
|
@ -153,7 +153,7 @@ class Resque_Tests_WorkerTest extends Resque_Tests_TestCase
|
|||||||
$job = $worker->reserve();
|
$job = $worker->reserve();
|
||||||
$worker->workingOn($job);
|
$worker->workingOn($job);
|
||||||
$worker->doneWorking();
|
$worker->doneWorking();
|
||||||
$this->assertEquals(new stdClass, $worker->job());
|
$this->assertEquals(array(), $worker->job());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testWorkerRecordsWhatItIsWorkingOn()
|
public function testWorkerRecordsWhatItIsWorkingOn()
|
||||||
@ -161,17 +161,18 @@ class Resque_Tests_WorkerTest extends Resque_Tests_TestCase
|
|||||||
$worker = new Resque_Worker('jobs');
|
$worker = new Resque_Worker('jobs');
|
||||||
$worker->registerWorker();
|
$worker->registerWorker();
|
||||||
|
|
||||||
$payload = new stdClass;
|
$payload = array(
|
||||||
$payload->class = 'Test_Job';
|
'class' => 'Test_Job'
|
||||||
|
);
|
||||||
$job = new Resque_Job('jobs', $payload);
|
$job = new Resque_Job('jobs', $payload);
|
||||||
$worker->workingOn($job);
|
$worker->workingOn($job);
|
||||||
|
|
||||||
$job = $worker->job();
|
$job = $worker->job();
|
||||||
$this->assertEquals('jobs', $job->queue);
|
$this->assertEquals('jobs', $job['queue']);
|
||||||
if(!isset($job->run_at)) {
|
if(!isset($job['run_at'])) {
|
||||||
$this->fail('Job does not have run_at time');
|
$this->fail('Job does not have run_at time');
|
||||||
}
|
}
|
||||||
$this->assertEquals($payload, $job->payload);
|
$this->assertEquals($payload, $job['payload']);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testWorkerErasesItsStatsWhenShutdown()
|
public function testWorkerErasesItsStatsWhenShutdown()
|
||||||
@ -239,8 +240,9 @@ class Resque_Tests_WorkerTest extends Resque_Tests_TestCase
|
|||||||
$worker = new Resque_Worker('jobs');
|
$worker = new Resque_Worker('jobs');
|
||||||
$worker->registerWorker();
|
$worker->registerWorker();
|
||||||
|
|
||||||
$payload = new stdClass;
|
$payload = array(
|
||||||
$payload->class = 'Test_Job';
|
'class' => 'Test_Job'
|
||||||
|
);
|
||||||
$job = new Resque_Job('jobs', $payload);
|
$job = new Resque_Job('jobs', $payload);
|
||||||
|
|
||||||
$worker->workingOn($job);
|
$worker->workingOn($job);
|
||||||
|
Loading…
Reference in New Issue
Block a user