Skip to content

Commit dc63a5d

Browse files
authored
[12.x] Introduce Job@resolveQueuedJobClass() (#54613)
* introduce Job@resolveQueuedJobClass * remove unused method * style
1 parent 4d73980 commit dc63a5d

File tree

7 files changed

+141
-6
lines changed

7 files changed

+141
-6
lines changed

src/Illuminate/Contracts/Queue/Job.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,23 @@ public function retryUntil();
133133
public function getName();
134134

135135
/**
136-
* Get the resolved name of the queued job class.
136+
* Get the display name of the queued job class.
137137
*
138138
* Resolves the name of "wrapped" jobs such as class-based handlers.
139139
*
140140
* @return string
141141
*/
142142
public function resolveName();
143143

144+
/**
145+
* Get the class of the queued job.
146+
*
147+
* Resolves the class of "wrapped" jobs such as class-based handlers.
148+
*
149+
* @return string
150+
*/
151+
public function resolveQueuedJobClass();
152+
144153
/**
145154
* Get the name of the connection the job belongs to.
146155
*

src/Illuminate/Queue/CallQueuedHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ protected function ensureUniqueJobLockIsReleased($command)
218218
*/
219219
protected function handleModelNotFound(Job $job, $e)
220220
{
221-
$class = $job->resolveName();
221+
$class = $job->resolveQueuedJobClass();
222222

223223
try {
224224
$reflectionClass = new ReflectionClass($class);

src/Illuminate/Queue/Jobs/Job.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ public function getName()
357357
}
358358

359359
/**
360-
* Get the resolved name of the queued job class.
360+
* Get the resolved display name of the queued job class.
361361
*
362362
* Resolves the name of "wrapped" jobs such as class-based handlers.
363363
*
@@ -368,6 +368,18 @@ public function resolveName()
368368
return JobName::resolve($this->getName(), $this->payload());
369369
}
370370

371+
/**
372+
* Get the class of the queued job.
373+
*
374+
* Resolves the class of "wrapped" jobs such as class-based handlers.
375+
*
376+
* @return string
377+
*/
378+
public function resolveQueuedJobClass()
379+
{
380+
return JobName::resolveClassName($this->getName(), $this->payload());
381+
}
382+
371383
/**
372384
* Get the name of the connection the job belongs to.
373385
*

src/Illuminate/Queue/Jobs/JobName.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,20 @@ public static function resolve($name, $payload)
3232

3333
return $name;
3434
}
35+
36+
/**
37+
* Get the class name for queued job class.
38+
*
39+
* @param string $name
40+
* @param array<string, mixed> $payload
41+
* @return string
42+
*/
43+
public static function resolveClassName($name, $payload)
44+
{
45+
if (is_string($payload['data']['commandName'] ?? null)) {
46+
return $payload['data']['commandName'];
47+
}
48+
49+
return $name;
50+
}
3551
}

tests/Integration/Queue/CallQueuedHandlerTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public function testJobIsMarkedAsFailedIfModelNotFoundExceptionIsThrown()
9090
$instance = new CallQueuedHandler(new Dispatcher($this->app), $this->app);
9191

9292
$job = m::mock(Job::class);
93-
$job->shouldReceive('resolveName')->andReturn(__CLASS__);
93+
$job->shouldReceive('resolveQueuedJobClass')->andReturn(__CLASS__);
9494
$job->shouldReceive('fail')->once();
9595

9696
$instance->call($job, [
@@ -106,7 +106,7 @@ public function testJobIsDeletedIfHasDeleteProperty()
106106

107107
$job = m::mock(Job::class);
108108
$job->shouldReceive('getConnectionName')->andReturn('connection');
109-
$job->shouldReceive('resolveName')->andReturn(CallQueuedHandlerExceptionThrower::class);
109+
$job->shouldReceive('resolveQueuedJobClass')->andReturn(CallQueuedHandlerExceptionThrower::class);
110110
$job->shouldReceive('markAsFailed')->never();
111111
$job->shouldReceive('isDeleted')->andReturn(false);
112112
$job->shouldReceive('delete')->once();
@@ -127,7 +127,7 @@ public function testJobIsDeletedIfHasDeleteAttribute()
127127

128128
$job = m::mock(Job::class);
129129
$job->shouldReceive('getConnectionName')->andReturn('connection');
130-
$job->shouldReceive('resolveName')->andReturn(CallQueuedHandlerAttributeExceptionThrower::class);
130+
$job->shouldReceive('resolveQueuedJobClass')->andReturn(CallQueuedHandlerAttributeExceptionThrower::class);
131131
$job->shouldReceive('markAsFailed')->never();
132132
$job->shouldReceive('isDeleted')->andReturn(false);
133133
$job->shouldReceive('delete')->once();
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Integration\Queue;
4+
5+
use Illuminate\Contracts\Queue\ShouldQueue;
6+
use Illuminate\Database\Eloquent\Model;
7+
use Illuminate\Database\Schema\Blueprint;
8+
use Illuminate\Foundation\Bus\Dispatchable;
9+
use Illuminate\Queue\InteractsWithQueue;
10+
use Illuminate\Queue\SerializesModels;
11+
use Illuminate\Support\Facades\Schema;
12+
use Orchestra\Testbench\Attributes\WithMigration;
13+
14+
#[WithMigration]
15+
#[WithMigration('queue')]
16+
class DeleteModelWhenMissingTest extends QueueTestCase
17+
{
18+
protected function defineEnvironment($app)
19+
{
20+
parent::defineEnvironment($app);
21+
$app['config']->set('queue.default', 'database');
22+
$this->driver = 'database';
23+
}
24+
25+
protected function defineDatabaseMigrations()
26+
{
27+
Schema::create('delete_model_test_models', function (Blueprint $table) {
28+
$table->id();
29+
$table->string('name');
30+
});
31+
}
32+
33+
protected function destroyDatabaseMigrations()
34+
{
35+
Schema::dropIfExists('delete_model_test_models');
36+
}
37+
38+
#[\Override]
39+
protected function tearDown(): void
40+
{
41+
parent::tearDown();
42+
43+
DeleteMissingModelJob::$handled = false;
44+
}
45+
46+
public function test_deleteModelWhenMissing_and_display_name(): void
47+
{
48+
$model = MyTestModel::query()->create(['name' => 'test']);
49+
50+
DeleteMissingModelJob::dispatch($model);
51+
52+
MyTestModel::query()->where('name', 'test')->delete();
53+
54+
$this->runQueueWorkerCommand(['--once' => '1']);
55+
56+
$this->assertFalse(DeleteMissingModelJob::$handled);
57+
$this->assertNull(\DB::table('failed_jobs')->first());
58+
}
59+
}
60+
61+
class DeleteMissingModelJob implements ShouldQueue
62+
{
63+
use InteractsWithQueue;
64+
use Dispatchable;
65+
use SerializesModels;
66+
67+
public static bool $handled = false;
68+
69+
public $deleteWhenMissingModels = true;
70+
71+
public function __construct(public MyTestModel $model)
72+
{
73+
}
74+
75+
public function displayName(): string
76+
{
77+
return 'sorry-ma-forgot-to-take-out-the-trash';
78+
}
79+
80+
public function handle()
81+
{
82+
self::$handled = true;
83+
}
84+
}
85+
86+
class MyTestModel extends Model
87+
{
88+
protected $table = 'delete_model_test_models';
89+
90+
public $timestamps = false;
91+
92+
protected $guarded = [];
93+
}

tests/Queue/QueueWorkerTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,11 @@ public function timeout()
662662
{
663663
return time() + 60;
664664
}
665+
666+
public function resolveQueuedJobClass()
667+
{
668+
return 'WorkerFakeJob';
669+
}
665670
}
666671

667672
class LoopBreakerException extends RuntimeException

0 commit comments

Comments
 (0)