Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] Add DeleteWhenMissingModels attribute #50890

Merged
merged 1 commit into from
Apr 3, 2024
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
11 changes: 11 additions & 0 deletions src/Illuminate/Queue/Attributes/DeleteWhenMissingModels.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Illuminate\Queue\Attributes;

use Attribute;

#[Attribute(Attribute::TARGET_CLASS)]
class DeleteWhenMissingModels
{
//
}
7 changes: 5 additions & 2 deletions src/Illuminate/Queue/CallQueuedHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Illuminate\Contracts\Queue\ShouldBeUniqueUntilProcessing;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Pipeline\Pipeline;
use Illuminate\Queue\Attributes\DeleteWhenMissingModels;
use ReflectionClass;
use RuntimeException;

Expand Down Expand Up @@ -218,8 +219,10 @@ protected function handleModelNotFound(Job $job, $e)
$class = $job->resolveName();

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

$shouldDelete = $reflectionClass->getDefaultProperties()['deleteWhenMissingModels']
?? count($reflectionClass->getAttributes(DeleteWhenMissingModels::class)) !== 0;
} catch (Exception) {
$shouldDelete = false;
}
Expand Down
36 changes: 36 additions & 0 deletions tests/Integration/Queue/CallQueuedHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\Job;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Queue\Attributes\DeleteWhenMissingModels;
use Illuminate\Queue\CallQueuedHandler;
use Illuminate\Queue\Events\JobFailed;
use Illuminate\Queue\InteractsWithQueue;
Expand Down Expand Up @@ -117,6 +118,27 @@ public function testJobIsDeletedIfHasDeleteProperty()

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

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

$instance = new CallQueuedHandler(new Dispatcher($this->app), $this->app);

$job = m::mock(Job::class);
$job->shouldReceive('getConnectionName')->andReturn('connection');
$job->shouldReceive('resolveName')->andReturn(CallQueuedHandlerAttributeExceptionThrower::class);
$job->shouldReceive('markAsFailed')->never();
$job->shouldReceive('isDeleted')->andReturn(false);
$job->shouldReceive('delete')->once();
$job->shouldReceive('failed')->never();

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

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

class CallQueuedHandlerTestJob
Expand Down Expand Up @@ -179,6 +201,20 @@ public function __wakeup()
}
}

#[DeleteWhenMissingModels]
class CallQueuedHandlerAttributeExceptionThrower
{
public function handle()
{
//
}

public function __wakeup()
{
throw new ModelNotFoundException('Foo');
}
}

class TestJobMiddleware
{
public function handle($command, $next)
Expand Down
Loading