diff --git a/lib/Job/Job.php b/lib/Job/Job.php index 34b7aee..184cc92 100644 --- a/lib/Job/Job.php +++ b/lib/Job/Job.php @@ -31,6 +31,12 @@ abstract class Job */ public $queue; + /** + * Unique job ID + * @var string + */ + public $jobID; + /** * (Optional) Job setup * diff --git a/lib/JobHandler.php b/lib/JobHandler.php index 3279f0a..a6d2cef 100755 --- a/lib/JobHandler.php +++ b/lib/JobHandler.php @@ -70,6 +70,10 @@ public function __construct($queue, $payload) $this->queue = $queue; $this->payload = $payload; $this->popTime = microtime(true); + + if (!isset($this->payload['id'])) { + $this->payload['id'] = Resque::generateJobId(); + } } /** @@ -199,6 +203,7 @@ public function getInstance(): Job $this->instance = $this->getJobFactory()->create($this->payload['class'], $this->getArguments(), $this->queue); $this->instance->job = $this; + $this->instance->jobID = $this->payload['id']; return $this->instance; } diff --git a/test/Resque/Tests/EventTest.php b/test/Resque/Tests/EventTest.php index 819e0a0..5a53af4 100644 --- a/test/Resque/Tests/EventTest.php +++ b/test/Resque/Tests/EventTest.php @@ -49,6 +49,7 @@ public function getEventTestJob() 'args' => array( array('somevar'), ), + 'id' => Resque::generateJobId() ); $job = new JobHandler('jobs', $payload); $job->worker = $this->worker; diff --git a/test/Resque/Tests/JobHandlerTest.php b/test/Resque/Tests/JobHandlerTest.php index 4b376ec..72ad1b1 100644 --- a/test/Resque/Tests/JobHandlerTest.php +++ b/test/Resque/Tests/JobHandlerTest.php @@ -163,6 +163,7 @@ public function testJobWithSetUpCallbackFiresSetUp() 'somevar', 'somevar2', )), + 'id' => Resque::generateJobId(), ); $job = new JobHandler('jobs', $payload); $job->perform(); @@ -178,6 +179,7 @@ public function testJobWithTearDownCallbackFiresTearDown() 'somevar', 'somevar2', )), + 'id' => Resque::generateJobId(), ); $job = new JobHandler('jobs', $payload); $job->perform(); @@ -384,7 +386,8 @@ public function testUseDefaultFactoryToGetJobInstance() { $payload = array( 'class' => 'Resque\Tests\Some_Job_Class', - 'args' => null + 'args' => null, + 'id' => Resque::generateJobId() ); $job = new JobHandler('jobs', $payload); $instance = $job->getInstance(); @@ -395,7 +398,8 @@ public function testUseFactoryToGetJobInstance() { $payload = array( 'class' => 'Resque\Tests\Some_Job_Class', - 'args' => array(array()) + 'args' => array(array()), + 'id' => Resque::generateJobId() ); $job = new JobHandler('jobs', $payload); $factory = new Some_Stub_Factory(); @@ -408,7 +412,8 @@ public function testDoNotUseFactoryToGetInstance() { $payload = array( 'class' => 'Resque\Tests\Some_Job_Class', - 'args' => array(array()) + 'args' => array(array()), + 'id' => Resque::generateJobId() ); $job = new JobHandler('jobs', $payload); $factory = $this->getMockBuilder('Resque\Job\FactoryInterface') @@ -424,7 +429,8 @@ public function testJobStatusIsNullIfIdMissingFromPayload() { $payload = array( 'class' => 'Resque\Tests\Some_Job_Class', - 'args' => null + 'args' => null, + 'id' => Resque::generateJobId() ); $job = new JobHandler('jobs', $payload); $this->assertEquals(null, $job->getStatus()); @@ -434,7 +440,8 @@ public function testJobCanBeRecreatedFromLegacyPayload() { $payload = array( 'class' => 'Resque\Tests\Some_Job_Class', - 'args' => null + 'args' => null, + 'id' => Resque::generateJobId() ); $job = new JobHandler('jobs', $payload); $job->recreate(); diff --git a/test/Resque/Tests/WorkerTest.php b/test/Resque/Tests/WorkerTest.php index 44d961b..0d96946 100644 --- a/test/Resque/Tests/WorkerTest.php +++ b/test/Resque/Tests/WorkerTest.php @@ -179,7 +179,8 @@ public function testWorkerRecordsWhatItIsWorkingOn() $worker->registerWorker(); $payload = array( - 'class' => 'Test_Job' + 'class' => 'Test_Job', + 'id' => '87993253a68c47e697fc03a515154339' ); $job = new JobHandler('jobs', $payload); $worker->workingOn($job); @@ -192,6 +193,26 @@ public function testWorkerRecordsWhatItIsWorkingOn() $this->assertEquals($payload, $job['payload']); } + public function testWorkerRecordsWhatItIsWorkingOnWithAutogeneratedId() + { + $worker = new ResqueWorker('jobs'); + $worker->setLogger($this->logger); + $worker->registerWorker(); + + $payload = array( + 'class' => 'Test_Job', + ); + $job = new JobHandler('jobs', $payload); + $worker->workingOn($job); + + $job = $worker->job(); + $this->assertEquals('jobs', $job['queue']); + if(!isset($job['run_at'])) { + $this->fail('Job does not have run_at time'); + } + $this->assertArrayHasKey('id', $job['payload']); + } + public function testWorkerErasesItsStatsWhenShutdown() { Resque::enqueue('jobs', 'Test_Job');