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
22 changes: 22 additions & 0 deletions lib/JobHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,21 @@ class JobHandler
*/
public $payload;

/**
* @var float Timestamp of when the data was popped from redis.
*/
public $popTime;

/**
* @var float Timestamp of when the job started processing.
*/
public $startTime;

/**
* @var float Timestamp of when the job finished processing.
*/
public $endTime;

/**
* @var object|\Resque\Job\JobInterface Instance of the class performing work for this job.
*/
Expand All @@ -54,6 +69,7 @@ public function __construct($queue, $payload)
{
$this->queue = $queue;
$this->payload = $payload;
$this->popTime = microtime(true);
}

/**
Expand Down Expand Up @@ -206,6 +222,8 @@ public function perform()
try {
Event::trigger('beforePerform', $this);

$this->startTime = microtime(true);

$instance = $this->getInstance();
if (is_callable([$instance, 'setUp'])) {
$instance->setUp();
Expand All @@ -217,6 +235,8 @@ public function perform()
$instance->tearDown();
}

$this->endTime = microtime(true);

Event::trigger('afterPerform', $this);
} catch (DoNotPerformException $e) {
// beforePerform/setUp have said don't perform this job. Return.
Expand All @@ -233,6 +253,8 @@ public function perform()
*/
public function fail($exception)
{
$this->endTime = microtime(true);

Event::trigger('onFailure', array(
'exception' => $exception,
'job' => $this,
Expand Down
51 changes: 51 additions & 0 deletions test/Resque/Tests/JobHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,57 @@ public function testJobCanBeRecreatedFromLegacyPayload()
$this->assertEquals('Resque\Tests\Some_Job_Class', $newJob->payload['class']);
$this->assertNotNull($newJob->payload['id']);
}

public function testJobHandlerSetsPopTime()
{
$payload = array(
'class' => 'Resque\Tests\Some_Job_Class',
'args' => null
);

$now = microtime(true);

$job = new JobHandler('jobs', $payload);
$instance = $job->getInstance();

$this->assertIsFloat($job->popTime);
$this->assertTrue($job->popTime >= $now);
}

public function testJobHandlerSetsStartAndEndTimeForSuccessfulJob()
{
$payload = array(
'class' => 'Resque\Tests\Some_Job_Class',
'args' => null
);

$job = new JobHandler('jobs', $payload);
$job->perform();

$this->assertIsFloat($job->startTime);
$this->assertTrue($job->startTime >= $job->popTime);

$this->assertIsFloat($job->endTime);
$this->assertTrue($job->endTime >= $job->startTime);
}

public function testJobHandlerSetsStartAndEndTimeForFailedJob()
{
$payload = array(
'class' => 'Failing_Job',
'args' => null
);
$job = new JobHandler('jobs', $payload);
$job->worker = $this->worker;

$this->worker->perform($job);

$this->assertIsFloat($job->startTime);
$this->assertTrue($job->startTime >= $job->popTime);

$this->assertIsFloat($job->endTime);
$this->assertTrue($job->endTime >= $job->startTime);
}
}

class Some_Job_Class implements JobInterface
Expand Down