Skip to content

Commit 7b5c928

Browse files
authored
Merge pull request #34 from talis/fix_undefined_index_prefix
Support legacy payload
2 parents 061c75c + 908bbc3 commit 7b5c928

File tree

2 files changed

+62
-20
lines changed

2 files changed

+62
-20
lines changed

lib/Resque/Job.php

+38-20
Original file line numberDiff line numberDiff line change
@@ -131,18 +131,22 @@ public function updateStatus($status, $result = null)
131131
return;
132132
}
133133

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

138138
/**
139139
* Return the status of the current job.
140140
*
141-
* @return int The status of the job as one of the Resque_Job_Status constants.
141+
* @return int|null The status of the job as one of the Resque_Job_Status constants or null if job is not being tracked.
142142
*/
143143
public function getStatus()
144144
{
145-
$status = new Resque_Job_Status($this->payload['id'], $this->payload['prefix']);
145+
if(empty($this->payload['id'])) {
146+
return null;
147+
}
148+
149+
$status = new Resque_Job_Status($this->payload['id'], $this->getPrefix());
146150
return $status->get();
147151
}
148152

@@ -171,9 +175,9 @@ public function getInstance()
171175
return $this->instance;
172176
}
173177

174-
$this->instance = $this->getJobFactory()->create($this->payload['class'], $this->getArguments(), $this->queue);
175-
$this->instance->job = $this;
176-
return $this->instance;
178+
$this->instance = $this->getJobFactory()->create($this->payload['class'], $this->getArguments(), $this->queue);
179+
$this->instance->job = $this;
180+
return $this->instance;
177181
}
178182

179183
/**
@@ -248,13 +252,15 @@ public function fail($exception)
248252
*/
249253
public function recreate()
250254
{
251-
$status = new Resque_Job_Status($this->payload['id'], $this->payload['prefix']);
252255
$monitor = false;
253-
if($status->isTracking()) {
254-
$monitor = true;
256+
if (!empty($this->payload['id'])) {
257+
$status = new Resque_Job_Status($this->payload['id'], $this->getPrefix());
258+
if($status->isTracking()) {
259+
$monitor = true;
260+
}
255261
}
256262

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

260266
/**
@@ -288,14 +294,26 @@ public function setJobFactory(Resque_Job_FactoryInterface $jobFactory)
288294
return $this;
289295
}
290296

291-
/**
292-
* @return Resque_Job_FactoryInterface
293-
*/
294-
public function getJobFactory()
295-
{
296-
if ($this->jobFactory === null) {
297-
$this->jobFactory = new Resque_Job_Factory();
298-
}
299-
return $this->jobFactory;
300-
}
297+
/**
298+
* @return Resque_Job_FactoryInterface
299+
*/
300+
public function getJobFactory()
301+
{
302+
if ($this->jobFactory === null) {
303+
$this->jobFactory = new Resque_Job_Factory();
304+
}
305+
return $this->jobFactory;
306+
}
307+
308+
/**
309+
* @return string
310+
*/
311+
private function getPrefix()
312+
{
313+
if (isset($this->payload['prefix'])) {
314+
return $this->payload['prefix'];
315+
}
316+
317+
return '';
318+
}
301319
}

test/Resque/Tests/JobTest.php

+24
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,30 @@ public function testDoNotUseFactoryToGetInstance()
416416
$instance = $job->getInstance();
417417
$this->assertInstanceOf('Resque_JobInterface', $instance);
418418
}
419+
420+
public function testJobStatusIsNullIfIdMissingFromPayload()
421+
{
422+
$payload = array(
423+
'class' => 'Some_Job_Class',
424+
'args' => null
425+
);
426+
$job = new Resque_Job('jobs', $payload);
427+
$this->assertEquals(null, $job->getStatus());
428+
}
429+
430+
public function testJobCanBeRecreatedFromLegacyPayload()
431+
{
432+
$payload = array(
433+
'class' => 'Some_Job_Class',
434+
'args' => null
435+
);
436+
$job = new Resque_Job('jobs', $payload);
437+
$job->recreate();
438+
$newJob = Resque_Job::reserve('jobs');
439+
$this->assertEquals('jobs', $newJob->queue);
440+
$this->assertEquals('Some_Job_Class', $newJob->payload['class']);
441+
$this->assertNotNull($newJob->payload['id']);
442+
}
419443
}
420444

421445
class Some_Job_Class implements Resque_JobInterface

0 commit comments

Comments
 (0)