Skip to content

[12.x] Allow naming queued closures #55634

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

Merged
merged 3 commits into from
May 5, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
27 changes: 26 additions & 1 deletion src/Illuminate/Queue/CallQueuedClosure.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ class CallQueuedClosure implements ShouldQueue
*/
public $deleteWhenMissingModels = true;

/**
* Custom name for the job.
*
* @var string|null
*/
public $name = null;

/**
* Create a new job instance.
*
Expand Down Expand Up @@ -103,8 +110,26 @@ public function failed($e)
*/
public function displayName()
{
$prefix = '';
$reflection = new ReflectionFunction($this->closure->getClosure());

return 'Closure ('.basename($reflection->getFileName()).':'.$reflection->getStartLine().')';
if (! is_null($this->name)) {
$prefix = "{$this->name} - ";
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$prefix = is_null($this->name) ? '' : "{$this->name} - ";


return $prefix.'Closure ('.basename($reflection->getFileName()).':'.$reflection->getStartLine().')';
}

/**
* Set a custom name for the job.
*
* @param string $name
* @return $this
*/
public function name($name)
{
$this->name = $name;

return $this;
}
}
18 changes: 18 additions & 0 deletions tests/Integration/Queue/JobDispatchingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,24 @@ public function testQueueMayBeNullForJobQueueingAndJobQueuedEvent()
$this->assertNull($events[3]->queue);
}

public function testQueuedClosureCanBeNamed()
{
Config::set('queue.default', 'database');
$events = [];
$this->app['events']->listen(function (JobQueued $e) use (&$events) {
$events[] = $e;
});

dispatch(function () {
//
})->name('custom name');

$this->assertCount(1, $events);
$this->assertInstanceOf(JobQueued::class, $events[0]);
$this->assertSame('custom name', $events[0]->job->name);
$this->assertStringContainsString('custom name', $events[0]->job->displayName());
}

public function testCanDisableDispatchingAfterResponse()
{
Job::dispatchAfterResponse('test');
Expand Down