Skip to content

Commit

Permalink
allow silent deletion on failed model retrieval
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Jun 27, 2017
1 parent 353adbd commit bceded6
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
33 changes: 30 additions & 3 deletions src/Illuminate/Queue/CallQueuedHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Illuminate\Queue;

use Exception;
use ReflectionClass;
use Illuminate\Contracts\Queue\Job;
use Illuminate\Contracts\Bus\Dispatcher;
use Illuminate\Database\Eloquent\ModelNotFoundException;
Expand Down Expand Up @@ -40,9 +42,7 @@ public function call(Job $job, array $data)
$job, unserialize($data['command'])
);
} catch (ModelNotFoundException $e) {
return FailingJob::handle(
$job->getConnectionName(), $job, $e
);
return $this->handleModelNotFound($job, $e);
}

$this->dispatcher->dispatchNow(
Expand Down Expand Up @@ -105,6 +105,33 @@ protected function ensureNextJobInChainIsDispatched($command)
}
}

/**
* Handle a model not found exception.
*
* @param \Illuminate\Contracts\Queue\Job $job
* @param \Exception $e
* @return void
*/
protected function handleModelNotFound(Job $job, $e)
{
$class = $job->resolveName();

try {
$shouldDelete = (new ReflectionClass($class))
->getDefaultProperties()['deleteWhenMissingModels'] ?? false;
} catch (Exception $e) {
$shouldDelete = false;
}

if ($shouldDelete) {
return $job->delete();
}

return FailingJob::handle(
$job->getConnectionName(), $job, $e
);
}

/**
* Call the failed method on the job instance.
*
Expand Down
24 changes: 24 additions & 0 deletions tests/Integration/Queue/CallQueuedHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function test_job_is_marked_as_failed_if_model_not_found_exception_is_thr

$job = Mockery::mock('Illuminate\Contracts\Queue\Job');
$job->shouldReceive('getConnectionName')->andReturn('connection');
$job->shouldReceive('resolveName')->andReturn(__CLASS__);
$job->shouldReceive('markAsFailed')->once();
$job->shouldReceive('isDeleted')->andReturn(false);
$job->shouldReceive('delete')->once();
Expand All @@ -48,6 +49,27 @@ public function test_job_is_marked_as_failed_if_model_not_found_exception_is_thr

Event::assertDispatched(JobFailed::class);
}

public function test_job_is_deleted_if_has_delete_property()
{
Event::fake();

$instance = new Illuminate\Queue\CallQueuedHandler(new Illuminate\Bus\Dispatcher(app()));

$job = Mockery::mock('Illuminate\Contracts\Queue\Job');
$job->shouldReceive('getConnectionName')->andReturn('connection');
$job->shouldReceive('resolveName')->andReturn(CallQueuedHandlerExceptionThrower::class);
$job->shouldReceive('markAsFailed')->never();
$job->shouldReceive('isDeleted')->andReturn(false);
$job->shouldReceive('delete')->once();
$job->shouldReceive('failed')->never();

$instance->call($job, [
'command' => serialize(new CallQueuedHandlerExceptionThrower),
]);

Event::assertNotDispatched(JobFailed::class);
}
}

class CallQueuedHandlerTestJob
Expand All @@ -64,6 +86,8 @@ public function handle()

class CallQueuedHandlerExceptionThrower
{
public $deleteWhenMissingModels = true;

public function handle()
{
//
Expand Down

0 comments on commit bceded6

Please sign in to comment.