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

[12.x] Introduce Job@resolveQueuedJobClass() #54613

Merged
merged 3 commits into from
Feb 14, 2025
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: 10 additions & 1 deletion src/Illuminate/Contracts/Queue/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,23 @@ public function retryUntil();
public function getName();

/**
* Get the resolved name of the queued job class.
* Get the display name of the queued job class.
*
* Resolves the name of "wrapped" jobs such as class-based handlers.
*
* @return string
*/
public function resolveName();

/**
* Get the class of the queued job.
*
* Resolves the class of "wrapped" jobs such as class-based handlers.
*
* @return string
*/
public function resolveQueuedJobClass();

/**
* Get the name of the connection the job belongs to.
*
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Queue/CallQueuedHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ protected function ensureUniqueJobLockIsReleased($command)
*/
protected function handleModelNotFound(Job $job, $e)
{
$class = $job->resolveName();
$class = $job->resolveQueuedJobClass();

try {
$reflectionClass = new ReflectionClass($class);
Expand Down
14 changes: 13 additions & 1 deletion src/Illuminate/Queue/Jobs/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ public function getName()
}

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

/**
* Get the class of the queued job.
*
* Resolves the class of "wrapped" jobs such as class-based handlers.
*
* @return string
*/
public function resolveQueuedJobClass()
{
return JobName::resolveClassName($this->getName(), $this->payload());
}

/**
* Get the name of the connection the job belongs to.
*
Expand Down
16 changes: 16 additions & 0 deletions src/Illuminate/Queue/Jobs/JobName.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,20 @@ public static function resolve($name, $payload)

return $name;
}

/**
* Get the class name for queued job class.
*
* @param string $name
* @param array<string, mixed> $payload
* @return string
*/
public static function resolveClassName($name, $payload)
{
if (is_string($payload['data']['commandName'] ?? null)) {
return $payload['data']['commandName'];
}

return $name;
}
}
6 changes: 3 additions & 3 deletions tests/Integration/Queue/CallQueuedHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function testJobIsMarkedAsFailedIfModelNotFoundExceptionIsThrown()
$instance = new CallQueuedHandler(new Dispatcher($this->app), $this->app);

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

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

$job = m::mock(Job::class);
$job->shouldReceive('getConnectionName')->andReturn('connection');
$job->shouldReceive('resolveName')->andReturn(CallQueuedHandlerExceptionThrower::class);
$job->shouldReceive('resolveQueuedJobClass')->andReturn(CallQueuedHandlerExceptionThrower::class);
$job->shouldReceive('markAsFailed')->never();
$job->shouldReceive('isDeleted')->andReturn(false);
$job->shouldReceive('delete')->once();
Expand All @@ -127,7 +127,7 @@ public function testJobIsDeletedIfHasDeleteAttribute()

$job = m::mock(Job::class);
$job->shouldReceive('getConnectionName')->andReturn('connection');
$job->shouldReceive('resolveName')->andReturn(CallQueuedHandlerAttributeExceptionThrower::class);
$job->shouldReceive('resolveQueuedJobClass')->andReturn(CallQueuedHandlerAttributeExceptionThrower::class);
$job->shouldReceive('markAsFailed')->never();
$job->shouldReceive('isDeleted')->andReturn(false);
$job->shouldReceive('delete')->once();
Expand Down
93 changes: 93 additions & 0 deletions tests/Integration/Queue/DeleteModelWhenMissingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

namespace Illuminate\Tests\Integration\Queue;

use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Schema;
use Orchestra\Testbench\Attributes\WithMigration;

#[WithMigration]
#[WithMigration('queue')]
class DeleteModelWhenMissingTest extends QueueTestCase
{
protected function defineEnvironment($app)
{
parent::defineEnvironment($app);
$app['config']->set('queue.default', 'database');
$this->driver = 'database';
}

protected function defineDatabaseMigrations()
{
Schema::create('delete_model_test_models', function (Blueprint $table) {
$table->id();
$table->string('name');
});
}

protected function destroyDatabaseMigrations()
{
Schema::dropIfExists('delete_model_test_models');
}

#[\Override]
protected function tearDown(): void
{
parent::tearDown();

DeleteMissingModelJob::$handled = false;
}

public function test_deleteModelWhenMissing_and_display_name(): void
{
$model = MyTestModel::query()->create(['name' => 'test']);

DeleteMissingModelJob::dispatch($model);

MyTestModel::query()->where('name', 'test')->delete();

$this->runQueueWorkerCommand(['--once' => '1']);

$this->assertFalse(DeleteMissingModelJob::$handled);
$this->assertNull(\DB::table('failed_jobs')->first());
}
}

class DeleteMissingModelJob implements ShouldQueue
{
use InteractsWithQueue;
use Dispatchable;
use SerializesModels;

public static bool $handled = false;

public $deleteWhenMissingModels = true;

public function __construct(public MyTestModel $model)
{
}

public function displayName(): string
{
return 'sorry-ma-forgot-to-take-out-the-trash';
}

public function handle()
{
self::$handled = true;
}
}

class MyTestModel extends Model
{
protected $table = 'delete_model_test_models';

public $timestamps = false;

protected $guarded = [];
}
5 changes: 5 additions & 0 deletions tests/Queue/QueueWorkerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,11 @@ public function timeout()
{
return time() + 60;
}

public function resolveQueuedJobClass()
{
return 'WorkerFakeJob';
}
}

class LoopBreakerException extends RuntimeException
Expand Down
Loading