Skip to content

Commit 8426f87

Browse files
timacdonalddriesvintstaylorotwell
authored
[10.x] Give access to job UUID in the job queued event (#48179)
* Give access to job UUID in the job queued event * lint * Update src/Illuminate/Queue/Events/JobQueued.php Co-authored-by: Dries Vints <[email protected]> * Update src/Illuminate/Queue/Events/JobQueued.php Co-authored-by: Dries Vints <[email protected]> * Update JobQueued.php --------- Co-authored-by: Dries Vints <[email protected]> Co-authored-by: Taylor Otwell <[email protected]>
1 parent 0a0c1de commit 8426f87

File tree

3 files changed

+57
-8
lines changed

3 files changed

+57
-8
lines changed

src/Illuminate/Queue/Events/JobQueued.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Illuminate\Queue\Events;
44

5+
use RuntimeException;
6+
57
class JobQueued
68
{
79
/**
@@ -25,18 +27,41 @@ class JobQueued
2527
*/
2628
public $job;
2729

30+
/**
31+
* The job payload.
32+
*
33+
* @var string|null
34+
*/
35+
public $payload;
36+
2837
/**
2938
* Create a new event instance.
3039
*
3140
* @param string $connectionName
3241
* @param string|int|null $id
3342
* @param \Closure|string|object $job
43+
* @param string|null $payload
3444
* @return void
3545
*/
36-
public function __construct($connectionName, $id, $job)
46+
public function __construct($connectionName, $id, $job, $payload = null)
3747
{
3848
$this->connectionName = $connectionName;
3949
$this->id = $id;
4050
$this->job = $job;
51+
$this->payload = $payload;
52+
}
53+
54+
/**
55+
* Get the decoded job payload.
56+
*
57+
* @return array
58+
*/
59+
public function payload()
60+
{
61+
if ($this->payload === null) {
62+
throw new RuntimeException('The job payload was not provided when the event was dispatched.');
63+
}
64+
65+
return json_decode($this->payload, true, flags: JSON_THROW_ON_ERROR);
4166
}
4267
}

src/Illuminate/Queue/Queue.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -305,15 +305,15 @@ protected function enqueueUsing($job, $payload, $queue, $delay, $callback)
305305
$this->container->bound('db.transactions')) {
306306
return $this->container->make('db.transactions')->addCallback(
307307
function () use ($payload, $queue, $delay, $callback, $job) {
308-
return tap($callback($payload, $queue, $delay), function ($jobId) use ($job) {
309-
$this->raiseJobQueuedEvent($jobId, $job);
308+
return tap($callback($payload, $queue, $delay), function ($jobId) use ($job, $payload) {
309+
$this->raiseJobQueuedEvent($jobId, $job, $payload);
310310
});
311311
}
312312
);
313313
}
314314

315-
return tap($callback($payload, $queue, $delay), function ($jobId) use ($job) {
316-
$this->raiseJobQueuedEvent($jobId, $job);
315+
return tap($callback($payload, $queue, $delay), function ($jobId) use ($job, $payload) {
316+
$this->raiseJobQueuedEvent($jobId, $job, $payload);
317317
});
318318
}
319319

@@ -341,12 +341,13 @@ protected function shouldDispatchAfterCommit($job)
341341
*
342342
* @param string|int|null $jobId
343343
* @param \Closure|string|object $job
344+
* @param string $payload
344345
* @return void
345346
*/
346-
protected function raiseJobQueuedEvent($jobId, $job)
347+
protected function raiseJobQueuedEvent($jobId, $job, $payload)
347348
{
348349
if ($this->container->bound('events')) {
349-
$this->container['events']->dispatch(new JobQueued($this->connectionName, $jobId, $job));
350+
$this->container['events']->dispatch(new JobQueued($this->connectionName, $jobId, $job, $payload));
350351
}
351352
}
352353

tests/Queue/QueueDatabaseQueueIntegrationTest.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66
use Illuminate\Database\Capsule\Manager as DB;
77
use Illuminate\Database\Eloquent\Model as Eloquent;
88
use Illuminate\Database\Schema\Blueprint;
9+
use Illuminate\Events\Dispatcher;
910
use Illuminate\Queue\DatabaseQueue;
11+
use Illuminate\Queue\Events\JobQueued;
1012
use Illuminate\Support\Carbon;
13+
use Illuminate\Support\Str;
1114
use PHPUnit\Framework\TestCase;
1215

1316
class QueueDatabaseQueueIntegrationTest extends TestCase
@@ -44,7 +47,9 @@ protected function setUp(): void
4447

4548
$this->queue = new DatabaseQueue($this->connection(), $this->table);
4649

47-
$this->container = $this->createMock(Container::class);
50+
$this->container = new Container;
51+
52+
$this->container->instance('events', new Dispatcher($this->container));
4853

4954
$this->queue->setContainer($this->container);
5055

@@ -241,4 +246,22 @@ public function testThatReservedJobsAreNotPopped()
241246

242247
$this->assertNull($popped_job);
243248
}
249+
250+
public function testJobPayloadIsAvailableOnEvent()
251+
{
252+
$event = null;
253+
Str::createUuidsUsingSequence([
254+
'expected-job-uuid',
255+
]);
256+
$this->container['events']->listen(function (JobQueued $e) use (&$event) {
257+
$event = $e;
258+
});
259+
260+
$this->queue->push('MyJob', [
261+
'laravel' => 'Framework',
262+
]);
263+
264+
$this->assertIsArray($event->payload());
265+
$this->assertSame('expected-job-uuid', $event->payload()['uuid']);
266+
}
244267
}

0 commit comments

Comments
 (0)