Skip to content

Commit 7a9a8d6

Browse files
committed
Add a jobID property to the base Job class
1 parent 7e03f6e commit 7a9a8d6

File tree

5 files changed

+46
-6
lines changed

5 files changed

+46
-6
lines changed

lib/Job/Job.php

+6
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ abstract class Job
3131
*/
3232
public $queue;
3333

34+
/**
35+
* Unique job ID
36+
* @var string
37+
*/
38+
public $jobID;
39+
3440
/**
3541
* (Optional) Job setup
3642
*

lib/JobHandler.php

+5
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ public function __construct($queue, $payload)
7070
$this->queue = $queue;
7171
$this->payload = $payload;
7272
$this->popTime = microtime(true);
73+
74+
if (!isset($this->payload['id'])) {
75+
$this->payload['id'] = Resque::generateJobId();
76+
}
7377
}
7478

7579
/**
@@ -199,6 +203,7 @@ public function getInstance(): Job
199203

200204
$this->instance = $this->getJobFactory()->create($this->payload['class'], $this->getArguments(), $this->queue);
201205
$this->instance->job = $this;
206+
$this->instance->jobID = $this->payload['id'];
202207
return $this->instance;
203208
}
204209

test/Resque/Tests/EventTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public function getEventTestJob()
4949
'args' => array(
5050
array('somevar'),
5151
),
52+
'id' => Resque::generateJobId()
5253
);
5354
$job = new JobHandler('jobs', $payload);
5455
$job->worker = $this->worker;

test/Resque/Tests/JobHandlerTest.php

+12-5
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ public function testJobWithSetUpCallbackFiresSetUp()
163163
'somevar',
164164
'somevar2',
165165
)),
166+
'id' => Resque::generateJobId(),
166167
);
167168
$job = new JobHandler('jobs', $payload);
168169
$job->perform();
@@ -178,6 +179,7 @@ public function testJobWithTearDownCallbackFiresTearDown()
178179
'somevar',
179180
'somevar2',
180181
)),
182+
'id' => Resque::generateJobId(),
181183
);
182184
$job = new JobHandler('jobs', $payload);
183185
$job->perform();
@@ -384,7 +386,8 @@ public function testUseDefaultFactoryToGetJobInstance()
384386
{
385387
$payload = array(
386388
'class' => 'Resque\Tests\Some_Job_Class',
387-
'args' => null
389+
'args' => null,
390+
'id' => Resque::generateJobId()
388391
);
389392
$job = new JobHandler('jobs', $payload);
390393
$instance = $job->getInstance();
@@ -395,7 +398,8 @@ public function testUseFactoryToGetJobInstance()
395398
{
396399
$payload = array(
397400
'class' => 'Resque\Tests\Some_Job_Class',
398-
'args' => array(array())
401+
'args' => array(array()),
402+
'id' => Resque::generateJobId()
399403
);
400404
$job = new JobHandler('jobs', $payload);
401405
$factory = new Some_Stub_Factory();
@@ -408,7 +412,8 @@ public function testDoNotUseFactoryToGetInstance()
408412
{
409413
$payload = array(
410414
'class' => 'Resque\Tests\Some_Job_Class',
411-
'args' => array(array())
415+
'args' => array(array()),
416+
'id' => Resque::generateJobId()
412417
);
413418
$job = new JobHandler('jobs', $payload);
414419
$factory = $this->getMockBuilder('Resque\Job\FactoryInterface')
@@ -424,7 +429,8 @@ public function testJobStatusIsNullIfIdMissingFromPayload()
424429
{
425430
$payload = array(
426431
'class' => 'Resque\Tests\Some_Job_Class',
427-
'args' => null
432+
'args' => null,
433+
'id' => Resque::generateJobId()
428434
);
429435
$job = new JobHandler('jobs', $payload);
430436
$this->assertEquals(null, $job->getStatus());
@@ -434,7 +440,8 @@ public function testJobCanBeRecreatedFromLegacyPayload()
434440
{
435441
$payload = array(
436442
'class' => 'Resque\Tests\Some_Job_Class',
437-
'args' => null
443+
'args' => null,
444+
'id' => Resque::generateJobId()
438445
);
439446
$job = new JobHandler('jobs', $payload);
440447
$job->recreate();

test/Resque/Tests/WorkerTest.php

+22-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,8 @@ public function testWorkerRecordsWhatItIsWorkingOn()
179179
$worker->registerWorker();
180180

181181
$payload = array(
182-
'class' => 'Test_Job'
182+
'class' => 'Test_Job',
183+
'id' => '87993253a68c47e697fc03a515154339'
183184
);
184185
$job = new JobHandler('jobs', $payload);
185186
$worker->workingOn($job);
@@ -192,6 +193,26 @@ public function testWorkerRecordsWhatItIsWorkingOn()
192193
$this->assertEquals($payload, $job['payload']);
193194
}
194195

196+
public function testWorkerRecordsWhatItIsWorkingOnWithAutogeneratedId()
197+
{
198+
$worker = new ResqueWorker('jobs');
199+
$worker->setLogger($this->logger);
200+
$worker->registerWorker();
201+
202+
$payload = array(
203+
'class' => 'Test_Job',
204+
);
205+
$job = new JobHandler('jobs', $payload);
206+
$worker->workingOn($job);
207+
208+
$job = $worker->job();
209+
$this->assertEquals('jobs', $job['queue']);
210+
if(!isset($job['run_at'])) {
211+
$this->fail('Job does not have run_at time');
212+
}
213+
$this->assertArrayHasKey('id', $job['payload']);
214+
}
215+
195216
public function testWorkerErasesItsStatsWhenShutdown()
196217
{
197218
Resque::enqueue('jobs', 'Test_Job');

0 commit comments

Comments
 (0)