diff --git a/src/Illuminate/Queue/Events/JobQueued.php b/src/Illuminate/Queue/Events/JobQueued.php index c91d14095963..b9cc0e90a147 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,18 +27,41 @@ class JobQueued */ public $job; + /** + * The job payload. + * + * @var string|null + */ + public $payload; + /** * Create a new event instance. * * @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) + public function __construct($connectionName, $id, $job, $payload = null) { $this->connectionName = $connectionName; $this->id = $id; $this->job = $job; + $this->payload = $payload; + } + + /** + * Get the decoded job 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..259fe46adcbe 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']); + } }