Skip to content

Commit

Permalink
add methods for determining if job has failed.
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Jan 23, 2017
1 parent 7fbac1c commit fed36bd
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 19 deletions.
2 changes: 2 additions & 0 deletions src/Illuminate/Queue/FailingJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class FailingJob
*/
public static function handle($connectionName, $job, $e = null)
{
$job->markAsFailed();

if ($job->isDeleted()) {
return;
}
Expand Down
29 changes: 29 additions & 0 deletions src/Illuminate/Queue/Jobs/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ abstract class Job
*/
protected $released = false;

/**
* Indicates if the job has failed.
*
* @var bool
*/
protected $failed = false;

/**
* The name of the connection the job belongs to.
*/
Expand Down Expand Up @@ -113,6 +120,26 @@ public function isDeletedOrReleased()
return $this->isDeleted() || $this->isReleased();
}

/**
* Determine if the job has been marked as a failure.
*
* @return bool
*/
public function hasFailed()
{
return $this->failed;
}

/**
* Mark the job as "failed".
*
* @return void
*/
public function markAsFailed()
{
$this->failed = true;
}

/**
* Process an exception that caused the job to fail.
*
Expand All @@ -121,6 +148,8 @@ public function isDeletedOrReleased()
*/
public function failed($e)
{
$this->markAsFailed();

$payload = $this->payload();

list($class, $method) = JobName::parse($payload['job']);
Expand Down
24 changes: 5 additions & 19 deletions tests/Queue/QueueWorkerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,11 @@ public function attempts()
return $this->attempts;
}

public function markAsFailed()
{
//
}

public function failed($e)
{
$this->failedWith = $e;
Expand All @@ -313,23 +318,4 @@ public function setConnectionName($name)
{
$this->connectionName = $name;
}

public function testJobSleepsWhenAnExceptionIsThrownForADaemonWorker()
{
$exceptionHandler = m::mock('Illuminate\Contracts\Debug\ExceptionHandler');
$job = m::mock('Illuminate\Contracts\Queue\Job');
$job->shouldReceive('fire')->once()->andReturnUsing(function () {
throw new RuntimeException;
});
$worker = m::mock('Illuminate\Queue\Worker', [$manager = m::mock('Illuminate\Queue\QueueManager')])->makePartial();
$manager->shouldReceive('connection')->once()->with('connection')->andReturn($connection = m::mock('StdClass'));
$manager->shouldReceive('getName')->andReturn('connection');
$connection->shouldReceive('pop')->once()->with('queue')->andReturn($job);
$worker->shouldReceive('sleep')->once()->with(3);

$exceptionHandler->shouldReceive('report')->once();

$worker->setDaemonExceptionHandler($exceptionHandler);
$worker->pop('connection', 'queue');
}
}

0 comments on commit fed36bd

Please sign in to comment.