Skip to content

Commit 2b65f50

Browse files
authored
Track job milestone timestamps directly in JobHandler (#90)
This makes it easier for hooks to work with these times, rather than having to track those for every job themselves. Previously you could do this with dynamic properties on the JobHandler class, but that is deprecated in PHP 8.2.
1 parent 5b0e4b2 commit 2b65f50

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

lib/JobHandler.php

+22
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,21 @@ class JobHandler
3434
*/
3535
public $payload;
3636

37+
/**
38+
* @var float Timestamp of when the data was popped from redis.
39+
*/
40+
public $popTime;
41+
42+
/**
43+
* @var float Timestamp of when the job started processing.
44+
*/
45+
public $startTime;
46+
47+
/**
48+
* @var float Timestamp of when the job finished processing.
49+
*/
50+
public $endTime;
51+
3752
/**
3853
* @var object|\Resque\Job\JobInterface Instance of the class performing work for this job.
3954
*/
@@ -54,6 +69,7 @@ public function __construct($queue, $payload)
5469
{
5570
$this->queue = $queue;
5671
$this->payload = $payload;
72+
$this->popTime = microtime(true);
5773
}
5874

5975
/**
@@ -206,6 +222,8 @@ public function perform()
206222
try {
207223
Event::trigger('beforePerform', $this);
208224

225+
$this->startTime = microtime(true);
226+
209227
$instance = $this->getInstance();
210228
if (is_callable([$instance, 'setUp'])) {
211229
$instance->setUp();
@@ -217,6 +235,8 @@ public function perform()
217235
$instance->tearDown();
218236
}
219237

238+
$this->endTime = microtime(true);
239+
220240
Event::trigger('afterPerform', $this);
221241
} catch (DoNotPerformException $e) {
222242
// beforePerform/setUp have said don't perform this job. Return.
@@ -233,6 +253,8 @@ public function perform()
233253
*/
234254
public function fail($exception)
235255
{
256+
$this->endTime = microtime(true);
257+
236258
Event::trigger('onFailure', array(
237259
'exception' => $exception,
238260
'job' => $this,

test/Resque/Tests/JobHandlerTest.php

+51
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,57 @@ public function testJobCanBeRecreatedFromLegacyPayload()
452452
$this->assertEquals('Resque\Tests\Some_Job_Class', $newJob->payload['class']);
453453
$this->assertNotNull($newJob->payload['id']);
454454
}
455+
456+
public function testJobHandlerSetsPopTime()
457+
{
458+
$payload = array(
459+
'class' => 'Resque\Tests\Some_Job_Class',
460+
'args' => null
461+
);
462+
463+
$now = microtime(true);
464+
465+
$job = new JobHandler('jobs', $payload);
466+
$instance = $job->getInstance();
467+
468+
$this->assertIsFloat($job->popTime);
469+
$this->assertTrue($job->popTime >= $now);
470+
}
471+
472+
public function testJobHandlerSetsStartAndEndTimeForSuccessfulJob()
473+
{
474+
$payload = array(
475+
'class' => 'Resque\Tests\Some_Job_Class',
476+
'args' => null
477+
);
478+
479+
$job = new JobHandler('jobs', $payload);
480+
$job->perform();
481+
482+
$this->assertIsFloat($job->startTime);
483+
$this->assertTrue($job->startTime >= $job->popTime);
484+
485+
$this->assertIsFloat($job->endTime);
486+
$this->assertTrue($job->endTime >= $job->startTime);
487+
}
488+
489+
public function testJobHandlerSetsStartAndEndTimeForFailedJob()
490+
{
491+
$payload = array(
492+
'class' => 'Failing_Job',
493+
'args' => null
494+
);
495+
$job = new JobHandler('jobs', $payload);
496+
$job->worker = $this->worker;
497+
498+
$this->worker->perform($job);
499+
500+
$this->assertIsFloat($job->startTime);
501+
$this->assertTrue($job->startTime >= $job->popTime);
502+
503+
$this->assertIsFloat($job->endTime);
504+
$this->assertTrue($job->endTime >= $job->startTime);
505+
}
455506
}
456507

457508
class Some_Job_Class implements JobInterface

0 commit comments

Comments
 (0)