Skip to content
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
26 changes: 25 additions & 1 deletion src/Illuminate/Queue/Events/JobQueued.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Illuminate\Queue\Events;

use RuntimeException;

class JobQueued
{
/**
Expand All @@ -25,6 +27,13 @@ class JobQueued
*/
public $job;

/**
* The job payload.
*
* @var ?string
*/
public $payload;

/**
* Create a new event instance.
*
Expand All @@ -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);
}
}
13 changes: 7 additions & 6 deletions src/Illuminate/Queue/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}

Expand Down Expand Up @@ -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)
Copy link
Member Author

Choose a reason for hiding this comment

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

This is technically a breaking change, however I'm not sure if we expect anyone to override it.

If we are concerned about this, we use magic and just access it via func_get_args() but not have it in the method signature. We have done this before in the framework.

{
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));
}
}

Expand Down
25 changes: 24 additions & 1 deletion tests/Queue/QueueDatabaseQueueIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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']);
}
}