From bdd3ffdbaa01a741c5369cf61ff3153ca7b1c7fd Mon Sep 17 00:00:00 2001 From: Alex Bouma Date: Thu, 25 Jan 2024 19:50:57 +0100 Subject: [PATCH] [11.x] Expose queue name and delay on job queue events (#49837) * Expose queue name and delay on job queue events * formatting * fix type --------- Co-authored-by: Taylor Otwell --- src/Illuminate/Queue/Events/JobQueued.php | 30 ++++++++++++++------- src/Illuminate/Queue/Events/JobQueueing.php | 30 ++++++++++++++------- src/Illuminate/Queue/Queue.php | 30 +++++++++++++-------- 3 files changed, 61 insertions(+), 29 deletions(-) diff --git a/src/Illuminate/Queue/Events/JobQueued.php b/src/Illuminate/Queue/Events/JobQueued.php index b9cc0e90a147..346078888b7b 100644 --- a/src/Illuminate/Queue/Events/JobQueued.php +++ b/src/Illuminate/Queue/Events/JobQueued.php @@ -2,8 +2,6 @@ namespace Illuminate\Queue\Events; -use RuntimeException; - class JobQueued { /** @@ -13,6 +11,13 @@ class JobQueued */ public $connectionName; + /** + * The queue name. + * + * @var string + */ + public $queue; + /** * The job ID. * @@ -30,25 +35,36 @@ class JobQueued /** * The job payload. * - * @var string|null + * @var string */ public $payload; + /** + * The amount of time the job was delayed. + * + * @var int|null + */ + public $delay; + /** * Create a new event instance. * * @param string $connectionName + * @param string $queue * @param string|int|null $id * @param \Closure|string|object $job - * @param string|null $payload + * @param string $payload + * @param int|null $delay * @return void */ - public function __construct($connectionName, $id, $job, $payload = null) + public function __construct($connectionName, $queue, $id, $job, $payload, $delay) { $this->connectionName = $connectionName; + $this->queue = $queue; $this->id = $id; $this->job = $job; $this->payload = $payload; + $this->delay = $delay; } /** @@ -58,10 +74,6 @@ public function __construct($connectionName, $id, $job, $payload = null) */ 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/Events/JobQueueing.php b/src/Illuminate/Queue/Events/JobQueueing.php index ebb0769681b4..6a35a64b8cca 100644 --- a/src/Illuminate/Queue/Events/JobQueueing.php +++ b/src/Illuminate/Queue/Events/JobQueueing.php @@ -2,8 +2,6 @@ namespace Illuminate\Queue\Events; -use RuntimeException; - class JobQueueing { /** @@ -13,6 +11,13 @@ class JobQueueing */ public $connectionName; + /** + * The queue name. + * + * @var string + */ + public $queue; + /** * The job instance. * @@ -23,23 +28,34 @@ class JobQueueing /** * The job payload. * - * @var string|null + * @var string */ public $payload; + /** + * The number of seconds the job was delayed. + * + * @var int|null + */ + public $delay; + /** * Create a new event instance. * * @param string $connectionName + * @param string $queue * @param \Closure|string|object $job - * @param string|null $payload + * @param string $payload + * @param int|null $delay * @return void */ - public function __construct($connectionName, $job, $payload = null) + public function __construct($connectionName, $queue, $job, $payload, $delay) { $this->connectionName = $connectionName; + $this->queue = $queue; $this->job = $job; $this->payload = $payload; + $this->delay = $delay; } /** @@ -49,10 +65,6 @@ public function __construct($connectionName, $job, $payload = null) */ 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 cc18241427be..8a69b6837666 100755 --- a/src/Illuminate/Queue/Queue.php +++ b/src/Illuminate/Queue/Queue.php @@ -327,20 +327,20 @@ protected function enqueueUsing($job, $payload, $queue, $delay, $callback) if ($this->shouldDispatchAfterCommit($job) && $this->container->bound('db.transactions')) { return $this->container->make('db.transactions')->addCallback( - function () use ($payload, $queue, $delay, $callback, $job) { - $this->raiseJobQueueingEvent($job, $payload); + function () use ($queue, $job, $payload, $delay, $callback) { + $this->raiseJobQueueingEvent($queue, $job, $payload, $delay); - 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 ($queue, $job, $payload, $delay) { + $this->raiseJobQueuedEvent($queue, $jobId, $job, $payload, $delay); }); } ); } - $this->raiseJobQueueingEvent($job, $payload); + $this->raiseJobQueueingEvent($queue, $job, $payload, $delay); - 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 ($queue, $job, $payload, $delay) { + $this->raiseJobQueuedEvent($queue, $jobId, $job, $payload, $delay); }); } @@ -370,29 +370,37 @@ protected function shouldDispatchAfterCommit($job) /** * Raise the job queueing event. * + * @param string $queue * @param \Closure|string|object $job * @param string $payload + * @param \DateTimeInterface|\DateInterval|int|null $delay * @return void */ - protected function raiseJobQueueingEvent($job, $payload) + protected function raiseJobQueueingEvent($queue, $job, $payload, $delay) { if ($this->container->bound('events')) { - $this->container['events']->dispatch(new JobQueueing($this->connectionName, $job, $payload)); + $delay = ! is_null($delay) ? $this->secondsUntil($delay) : $delay; + + $this->container['events']->dispatch(new JobQueueing($this->connectionName, $queue, $job, $payload, $delay)); } } /** * Raise the job queued event. * + * @param string $queue * @param string|int|null $jobId * @param \Closure|string|object $job * @param string $payload + * @param \DateTimeInterface|\DateInterval|int|null $delay * @return void */ - protected function raiseJobQueuedEvent($jobId, $job, $payload) + protected function raiseJobQueuedEvent($queue, $jobId, $job, $payload, $delay) { if ($this->container->bound('events')) { - $this->container['events']->dispatch(new JobQueued($this->connectionName, $jobId, $job, $payload)); + $delay = ! is_null($delay) ? $this->secondsUntil($delay) : $delay; + + $this->container['events']->dispatch(new JobQueued($this->connectionName, $queue, $jobId, $job, $payload, $delay)); } }