Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/Job/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ abstract class Job
*/
public $queue;

/**
* Unique job ID
* @var string
*/
public $jobID;

/**
* (Optional) Job setup
*
Expand Down
5 changes: 5 additions & 0 deletions lib/JobHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

/**
Expand Down Expand Up @@ -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;
}

Expand Down
1 change: 1 addition & 0 deletions test/Resque/Tests/EventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public function getEventTestJob()
'args' => array(
array('somevar'),
),
'id' => Resque::generateJobId()
);
$job = new JobHandler('jobs', $payload);
$job->worker = $this->worker;
Expand Down
17 changes: 12 additions & 5 deletions test/Resque/Tests/JobHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ public function testJobWithSetUpCallbackFiresSetUp()
'somevar',
'somevar2',
)),
'id' => Resque::generateJobId(),
);
$job = new JobHandler('jobs', $payload);
$job->perform();
Expand All @@ -178,6 +179,7 @@ public function testJobWithTearDownCallbackFiresTearDown()
'somevar',
'somevar2',
)),
'id' => Resque::generateJobId(),
);
$job = new JobHandler('jobs', $payload);
$job->perform();
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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')
Expand All @@ -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());
Expand All @@ -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();
Expand Down
23 changes: 22 additions & 1 deletion test/Resque/Tests/WorkerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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');
Expand Down