Skip to content

[10.x] Give access to job UUID in the job queued event #48179

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 5 commits into from
Aug 25, 2023
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
27 changes: 26 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,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);
}
}
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']);
}
}