Skip to content

Commit 96593e2

Browse files
authored
Add DeleteWhenMissingModels attribute (#50890)
1 parent da0120a commit 96593e2

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Illuminate\Queue\Attributes;
4+
5+
use Attribute;
6+
7+
#[Attribute(Attribute::TARGET_CLASS)]
8+
class DeleteWhenMissingModels
9+
{
10+
//
11+
}

src/Illuminate/Queue/CallQueuedHandler.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Illuminate\Contracts\Queue\ShouldBeUniqueUntilProcessing;
1515
use Illuminate\Database\Eloquent\ModelNotFoundException;
1616
use Illuminate\Pipeline\Pipeline;
17+
use Illuminate\Queue\Attributes\DeleteWhenMissingModels;
1718
use ReflectionClass;
1819
use RuntimeException;
1920

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

220221
try {
221-
$shouldDelete = (new ReflectionClass($class))
222-
->getDefaultProperties()['deleteWhenMissingModels'] ?? false;
222+
$reflectionClass = new ReflectionClass($class);
223+
224+
$shouldDelete = $reflectionClass->getDefaultProperties()['deleteWhenMissingModels']
225+
?? count($reflectionClass->getAttributes(DeleteWhenMissingModels::class)) !== 0;
223226
} catch (Exception) {
224227
$shouldDelete = false;
225228
}

tests/Integration/Queue/CallQueuedHandlerTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Bus\Queueable;
77
use Illuminate\Contracts\Queue\Job;
88
use Illuminate\Database\Eloquent\ModelNotFoundException;
9+
use Illuminate\Queue\Attributes\DeleteWhenMissingModels;
910
use Illuminate\Queue\CallQueuedHandler;
1011
use Illuminate\Queue\Events\JobFailed;
1112
use Illuminate\Queue\InteractsWithQueue;
@@ -117,6 +118,27 @@ public function testJobIsDeletedIfHasDeleteProperty()
117118

118119
Event::assertNotDispatched(JobFailed::class);
119120
}
121+
122+
public function testJobIsDeletedIfHasDeleteAttribute()
123+
{
124+
Event::fake();
125+
126+
$instance = new CallQueuedHandler(new Dispatcher($this->app), $this->app);
127+
128+
$job = m::mock(Job::class);
129+
$job->shouldReceive('getConnectionName')->andReturn('connection');
130+
$job->shouldReceive('resolveName')->andReturn(CallQueuedHandlerAttributeExceptionThrower::class);
131+
$job->shouldReceive('markAsFailed')->never();
132+
$job->shouldReceive('isDeleted')->andReturn(false);
133+
$job->shouldReceive('delete')->once();
134+
$job->shouldReceive('failed')->never();
135+
136+
$instance->call($job, [
137+
'command' => serialize(new CallQueuedHandlerAttributeExceptionThrower()),
138+
]);
139+
140+
Event::assertNotDispatched(JobFailed::class);
141+
}
120142
}
121143

122144
class CallQueuedHandlerTestJob
@@ -179,6 +201,20 @@ public function __wakeup()
179201
}
180202
}
181203

204+
#[DeleteWhenMissingModels]
205+
class CallQueuedHandlerAttributeExceptionThrower
206+
{
207+
public function handle()
208+
{
209+
//
210+
}
211+
212+
public function __wakeup()
213+
{
214+
throw new ModelNotFoundException('Foo');
215+
}
216+
}
217+
182218
class TestJobMiddleware
183219
{
184220
public function handle($command, $next)

0 commit comments

Comments
 (0)