Skip to content

Commit

Permalink
Merge pull request #34 from talis/fix_undefined_index_prefix
Browse files Browse the repository at this point in the history
Support legacy payload
  • Loading branch information
danhunsaker authored Apr 9, 2020
2 parents 061c75c + 908bbc3 commit 7b5c928
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 20 deletions.
58 changes: 38 additions & 20 deletions lib/Resque/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,18 +131,22 @@ public function updateStatus($status, $result = null)
return;
}

$statusInstance = new Resque_Job_Status($this->payload['id'], $this->payload['prefix']);
$statusInstance = new Resque_Job_Status($this->payload['id'], $this->getPrefix());
$statusInstance->update($status, $result);
}

/**
* Return the status of the current job.
*
* @return int The status of the job as one of the Resque_Job_Status constants.
* @return int|null The status of the job as one of the Resque_Job_Status constants or null if job is not being tracked.
*/
public function getStatus()
{
$status = new Resque_Job_Status($this->payload['id'], $this->payload['prefix']);
if(empty($this->payload['id'])) {
return null;
}

$status = new Resque_Job_Status($this->payload['id'], $this->getPrefix());
return $status->get();
}

Expand Down Expand Up @@ -171,9 +175,9 @@ public function getInstance()
return $this->instance;
}

$this->instance = $this->getJobFactory()->create($this->payload['class'], $this->getArguments(), $this->queue);
$this->instance->job = $this;
return $this->instance;
$this->instance = $this->getJobFactory()->create($this->payload['class'], $this->getArguments(), $this->queue);
$this->instance->job = $this;
return $this->instance;
}

/**
Expand Down Expand Up @@ -248,13 +252,15 @@ public function fail($exception)
*/
public function recreate()
{
$status = new Resque_Job_Status($this->payload['id'], $this->payload['prefix']);
$monitor = false;
if($status->isTracking()) {
$monitor = true;
if (!empty($this->payload['id'])) {
$status = new Resque_Job_Status($this->payload['id'], $this->getPrefix());
if($status->isTracking()) {
$monitor = true;
}
}

return self::create($this->queue, $this->payload['class'], $this->getArguments(), $monitor, $this->payload['prefix']);
return self::create($this->queue, $this->payload['class'], $this->getArguments(), $monitor, null, $this->getPrefix());
}

/**
Expand Down Expand Up @@ -288,14 +294,26 @@ public function setJobFactory(Resque_Job_FactoryInterface $jobFactory)
return $this;
}

/**
* @return Resque_Job_FactoryInterface
*/
public function getJobFactory()
{
if ($this->jobFactory === null) {
$this->jobFactory = new Resque_Job_Factory();
}
return $this->jobFactory;
}
/**
* @return Resque_Job_FactoryInterface
*/
public function getJobFactory()
{
if ($this->jobFactory === null) {
$this->jobFactory = new Resque_Job_Factory();
}
return $this->jobFactory;
}

/**
* @return string
*/
private function getPrefix()
{
if (isset($this->payload['prefix'])) {
return $this->payload['prefix'];
}

return '';
}
}
24 changes: 24 additions & 0 deletions test/Resque/Tests/JobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,30 @@ public function testDoNotUseFactoryToGetInstance()
$instance = $job->getInstance();
$this->assertInstanceOf('Resque_JobInterface', $instance);
}

public function testJobStatusIsNullIfIdMissingFromPayload()
{
$payload = array(
'class' => 'Some_Job_Class',
'args' => null
);
$job = new Resque_Job('jobs', $payload);
$this->assertEquals(null, $job->getStatus());
}

public function testJobCanBeRecreatedFromLegacyPayload()
{
$payload = array(
'class' => 'Some_Job_Class',
'args' => null
);
$job = new Resque_Job('jobs', $payload);
$job->recreate();
$newJob = Resque_Job::reserve('jobs');
$this->assertEquals('jobs', $newJob->queue);
$this->assertEquals('Some_Job_Class', $newJob->payload['class']);
$this->assertNotNull($newJob->payload['id']);
}
}

class Some_Job_Class implements Resque_JobInterface
Expand Down

0 comments on commit 7b5c928

Please sign in to comment.