8000 [12.x] Allow naming queued closures by willrowe · Pull Request #55634 · laravel/framework · GitHub
[go: up one dir, main page]

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
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
24 changes: 23 additions & 1 deletion src/Illuminate/Queue/CallQueuedClosure.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ class CallQueuedClosure implements ShouldQueue
*/
public $closure;

/**
* The name assigned to the job.
*
* @var string|null
*/
public $name = null;

/**
* The callbacks that should be executed on failure.
*
Expand Down Expand Up @@ -105,6 +112,21 @@ public function displayName()
{
$reflection = new ReflectionFunction($this->closure->getClosure());

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

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

/**
* Assign a name to 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
0