From fc2473fb79fdbc1057d942e0c1592d3dd8153f9e Mon Sep 17 00:00:00 2001 From: Will Rowe Date: Fri, 2 May 2025 13:52:07 -0400 Subject: [PATCH 1/3] Add failing test --- tests/Integration/Queue/JobDispatchingTest.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/Integration/Queue/JobDispatchingTest.php b/tests/Integration/Queue/JobDispatchingTest.php index eddfd83c23c1..441cb59dea97 100644 --- a/tests/Integration/Queue/JobDispatchingTest.php +++ b/tests/Integration/Queue/JobDispatchingTest.php @@ -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'); From 5da65f23dd37389758d44609547ce4eebf23dda9 Mon Sep 17 00:00:00 2001 From: Will Rowe Date: Fri, 2 May 2025 13:52:55 -0400 Subject: [PATCH 2/3] Add ability to name queued closures --- src/Illuminate/Queue/CallQueuedClosure.php | 27 +++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Queue/CallQueuedClosure.php b/src/Illuminate/Queue/CallQueuedClosure.php index 732600ccfea1..e7c6a8efceb9 100644 --- a/src/Illuminate/Queue/CallQueuedClosure.php +++ b/src/Illuminate/Queue/CallQueuedClosure.php @@ -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. * @@ -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} - "; + } + + 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; } } From 1cc3237e4e9da606d7ab8c457a3ebcf3727cf1b4 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Sun, 4 May 2025 20:38:31 -0500 Subject: [PATCH 3/3] formatting --- src/Illuminate/Queue/CallQueuedClosure.php | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/src/Illuminate/Queue/CallQueuedClosure.php b/src/Illuminate/Queue/CallQueuedClosure.php index e7c6a8efceb9..34bdf3b85796 100644 --- a/src/Illuminate/Queue/CallQueuedClosure.php +++ b/src/Illuminate/Queue/CallQueuedClosure.php @@ -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. * @@ -36,13 +43,6 @@ class CallQueuedClosure implements ShouldQueue */ public $deleteWhenMissingModels = true; - /** - * Custom name for the job. - * - * @var string|null - */ - public $name = null; - /** * Create a new job instance. * @@ -110,18 +110,15 @@ public function failed($e) */ public function displayName() { - $prefix = ''; $reflection = new ReflectionFunction($this->closure->getClosure()); - if (! is_null($this->name)) { - $prefix = "{$this->name} - "; - } + $prefix = is_null($this->name) ? '' : "{$this->name} - "; return $prefix.'Closure ('.basename($reflection->getFileName()).':'.$reflection->getStartLine().')'; } /** - * Set a custom name for the job. + * Assign a name to the job. * * @param string $name * @return $this