Skip to content

Commit ffb001d

Browse files
vdauchytaylorotwell
authored andcommitted
Add JobQueued event
(Follows: laravel/framework#32894)
1 parent f279735 commit ffb001d

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

Events/JobQueued.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Illuminate\Queue\Events;
4+
5+
class JobQueued
6+
{
7+
/**
8+
* @var string|int|null
9+
*/
10+
public $jobId;
11+
12+
/**
13+
* @var string|object
14+
*/
15+
public $job;
16+
17+
/**
18+
* JobQueued constructor.
19+
*
20+
* @param string|int|null $jobId
21+
* @param \Closure|string|object $job
22+
* @return void
23+
*/
24+
public function __construct($jobId, $job)
25+
{
26+
$this->jobId = $jobId;
27+
$this->job = $job;
28+
}
29+
}

Queue.php

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Container\Container;
88
use Illuminate\Contracts\Encryption\Encrypter;
99
use Illuminate\Contracts\Queue\ShouldBeEncrypted;
10+
use Illuminate\Queue\Events\JobQueued;
1011
use Illuminate\Support\Arr;
1112
use Illuminate\Support\InteractsWithTime;
1213
use Illuminate\Support\Str;
@@ -284,13 +285,17 @@ protected function enqueueUsing($job, $payload, $queue, $delay, $callback)
284285
if ($this->shouldDispatchAfterCommit($job) &&
285286
$this->container->bound('db.transactions')) {
286287
return $this->container->make('db.transactions')->addCallback(
287-
function () use ($payload, $queue, $delay, $callback) {
288-
return $callback($payload, $queue, $delay);
288+
function () use ($payload, $queue, $delay, $callback, $job) {
289+
return tap($callback($payload, $queue, $delay), function ($jobId) use ($job) {
290+
$this->raiseJobQueuedEvent($jobId, $job);
291+
});
289292
}
290293
);
291294
}
292295

293-
return $callback($payload, $queue, $delay);
296+
return tap($callback($payload, $queue, $delay), function ($jobId) use ($job) {
297+
$this->raiseJobQueuedEvent($jobId, $job);
298+
});
294299
}
295300

296301
/**
@@ -345,4 +350,18 @@ public function setContainer(Container $container)
345350
{
346351
$this->container = $container;
347352
}
353+
354+
/**
355+
* Raise the job queued event.
356+
*
357+
* @param string|int|null $jobId
358+
* @param \Closure|string|object $job
359+
* @return void
360+
*/
361+
protected function raiseJobQueuedEvent($jobId, $job)
362+
{
363+
if ($this->container->bound('events')) {
364+
$this->container['events']->dispatch(new JobQueued($jobId, $job));
365+
}
366+
}
348367
}

0 commit comments

Comments
 (0)