From 6660291a349259f2c2359cf6a4cffbf902e28d9c Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Fri, 25 Aug 2023 16:00:44 +1000 Subject: [PATCH 1/5] Give access to job UUID in the job queued event --- src/Illuminate/Queue/Events/JobQueued.php | 26 ++++++++++++++++++- src/Illuminate/Queue/Queue.php | 13 +++++----- .../QueueDatabaseQueueIntegrationTest.php | 25 +++++++++++++++++- 3 files changed, 56 insertions(+), 8 deletions(-) diff --git a/src/Illuminate/Queue/Events/JobQueued.php b/src/Illuminate/Queue/Events/JobQueued.php index c91d14095963..1a35230a7946 100644 --- a/src/Illuminate/Queue/Events/JobQueued.php +++ b/src/Illuminate/Queue/Events/JobQueued.php @@ -2,6 +2,8 @@ namespace Illuminate\Queue\Events; +use RuntimeException; + class JobQueued { /** @@ -25,6 +27,13 @@ class JobQueued */ public $job; + /** + * The job payload. + * + * @var ?string + */ + public $payload; + /** * Create a new event instance. * @@ -33,10 +42,25 @@ class JobQueued * @param \Closure|string|object $job * @return void */ - public function __construct($connectionName, $id, $job) + public function __construct($connectionName, $id, $job, $payload = null) { $this->connectionName = $connectionName; $this->id = $id; $this->job = $job; + $this->payload = $payload; + } + + /** + * The decoded payload. + * + * @return array + */ + public function payload() + { + if ($this->payload === null) { + throw new RuntimeException('The job payload was not provided when the event was dispatched.'); + } + + return json_decode($this->payload, true, flags: JSON_THROW_ON_ERROR); } } diff --git a/src/Illuminate/Queue/Queue.php b/src/Illuminate/Queue/Queue.php index 7dcffb4758eb..03085f60a05d 100755 --- a/src/Illuminate/Queue/Queue.php +++ b/src/Illuminate/Queue/Queue.php @@ -305,15 +305,15 @@ protected function enqueueUsing($job, $payload, $queue, $delay, $callback) $this->container->bound('db.transactions')) { return $this->container->make('db.transactions')->addCallback( function () use ($payload, $queue, $delay, $callback, $job) { - return tap($callback($payload, $queue, $delay), function ($jobId) use ($job) { - $this->raiseJobQueuedEvent($jobId, $job); + return tap($callback($payload, $queue, $delay), function ($jobId) use ($job, $payload) { + $this->raiseJobQueuedEvent($jobId, $job, $payload); }); } ); } - return tap($callback($payload, $queue, $delay), function ($jobId) use ($job) { - $this->raiseJobQueuedEvent($jobId, $job); + return tap($callback($payload, $queue, $delay), function ($jobId) use ($job, $payload) { + $this->raiseJobQueuedEvent($jobId, $job, $payload); }); } @@ -341,12 +341,13 @@ protected function shouldDispatchAfterCommit($job) * * @param string|int|null $jobId * @param \Closure|string|object $job + * @param string $payload * @return void */ - protected function raiseJobQueuedEvent($jobId, $job) + protected function raiseJobQueuedEvent($jobId, $job, $payload) { if ($this->container->bound('events')) { - $this->container['events']->dispatch(new JobQueued($this->connectionName, $jobId, $job)); + $this->container['events']->dispatch(new JobQueued($this->connectionName, $jobId, $job, $payload)); } } diff --git a/tests/Queue/QueueDatabaseQueueIntegrationTest.php b/tests/Queue/QueueDatabaseQueueIntegrationTest.php index fa1955268e2f..e30f69525ea4 100644 --- a/tests/Queue/QueueDatabaseQueueIntegrationTest.php +++ b/tests/Queue/QueueDatabaseQueueIntegrationTest.php @@ -6,8 +6,11 @@ use Illuminate\Database\Capsule\Manager as DB; use Illuminate\Database\Eloquent\Model as Eloquent; use Illuminate\Database\Schema\Blueprint; +use Illuminate\Events\Dispatcher; use Illuminate\Queue\DatabaseQueue; +use Illuminate\Queue\Events\JobQueued; use Illuminate\Support\Carbon; +use Illuminate\Support\Str; use PHPUnit\Framework\TestCase; class QueueDatabaseQueueIntegrationTest extends TestCase @@ -44,7 +47,9 @@ protected function setUp(): void $this->queue = new DatabaseQueue($this->connection(), $this->table); - $this->container = $this->createMock(Container::class); + $this->container = new Container; + + $this->container->instance('events', new Dispatcher($this->container)); $this->queue->setContainer($this->container); @@ -241,4 +246,22 @@ public function testThatReservedJobsAreNotPopped() $this->assertNull($popped_job); } + + public function testJobPayloadIsAvailableOnEvent() + { + $event = null; + Str::createUuidsUsingSequence([ + 'expected-job-uuid', + ]); + $this->container['events']->listen(function (JobQueued $e) use (&$event) { + $event = $e; + }); + + $this->queue->push('MyJob', [ + 'laravel' => 'Framework' + ]); + + $this->assertIsArray($event->payload()); + $this->assertSame('expected-job-uuid', $event->payload()['uuid']); + } } From 7756ac3aeb35693f5a8dafacf3e786ce628450dc Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Fri, 25 Aug 2023 16:25:45 +1000 Subject: [PATCH 2/5] lint --- tests/Queue/QueueDatabaseQueueIntegrationTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Queue/QueueDatabaseQueueIntegrationTest.php b/tests/Queue/QueueDatabaseQueueIntegrationTest.php index e30f69525ea4..259fe46adcbe 100644 --- a/tests/Queue/QueueDatabaseQueueIntegrationTest.php +++ b/tests/Queue/QueueDatabaseQueueIntegrationTest.php @@ -258,7 +258,7 @@ public function testJobPayloadIsAvailableOnEvent() }); $this->queue->push('MyJob', [ - 'laravel' => 'Framework' + 'laravel' => 'Framework', ]); $this->assertIsArray($event->payload()); From 5a50ed38d09dd3912965395a0e317cb708a269cd Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Fri, 25 Aug 2023 18:07:16 +1000 Subject: [PATCH 3/5] Update src/Illuminate/Queue/Events/JobQueued.php Co-authored-by: Dries Vints --- src/Illuminate/Queue/Events/JobQueued.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Illuminate/Queue/Events/JobQueued.php b/src/Illuminate/Queue/Events/JobQueued.php index 1a35230a7946..7e8365498608 100644 --- a/src/Illuminate/Queue/Events/JobQueued.php +++ b/src/Illuminate/Queue/Events/JobQueued.php @@ -40,6 +40,7 @@ class JobQueued * @param string $connectionName * @param string|int|null $id * @param \Closure|string|object $job + * @param string|null $payload * @return void */ public function __construct($connectionName, $id, $job, $payload = null) From ba9a57096634f9dd110e18001a54639cb173f567 Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Fri, 25 Aug 2023 18:07:24 +1000 Subject: [PATCH 4/5] Update src/Illuminate/Queue/Events/JobQueued.php Co-authored-by: Dries Vints --- src/Illuminate/Queue/Events/JobQueued.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Queue/Events/JobQueued.php b/src/Illuminate/Queue/Events/JobQueued.php index 7e8365498608..3894554667c6 100644 --- a/src/Illuminate/Queue/Events/JobQueued.php +++ b/src/Illuminate/Queue/Events/JobQueued.php @@ -30,7 +30,7 @@ class JobQueued /** * The job payload. * - * @var ?string + * @var string|null */ public $payload; From 4bdb87e44207e1e7743b67649e9270bef3e7f079 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Fri, 25 Aug 2023 08:45:21 -0500 Subject: [PATCH 5/5] Update JobQueued.php --- src/Illuminate/Queue/Events/JobQueued.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Queue/Events/JobQueued.php b/src/Illuminate/Queue/Events/JobQueued.php index 3894554667c6..b9cc0e90a147 100644 --- a/src/Illuminate/Queue/Events/JobQueued.php +++ b/src/Illuminate/Queue/Events/JobQueued.php @@ -52,7 +52,7 @@ public function __construct($connectionName, $id, $job, $payload = null) } /** - * The decoded payload. + * Get the decoded job payload. * * @return array */