Skip to content

[8.x] Add JobQueued event #32894

New issue

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

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

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
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
4 changes: 2 additions & 2 deletions src/Illuminate/Queue/BeanstalkdQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function size($queue = null)
* @param string|null $queue
* @return mixed
*/
public function push($job, $data = '', $queue = null)
protected function pushJob($job, $data = '', $queue = null)
{
return $this->pushRaw($this->createPayload($job, $this->getQueue($queue), $data), $queue);
}
Expand Down Expand Up @@ -104,7 +104,7 @@ public function pushRaw($payload, $queue = null, array $options = [])
* @param string|null $queue
* @return mixed
*/
public function later($delay, $job, $data = '', $queue = null)
protected function laterJob($delay, $job, $data = '', $queue = null)
{
$pheanstalk = $this->pheanstalk->useTube($this->getQueue($queue));

Expand Down
4 changes: 2 additions & 2 deletions src/Illuminate/Queue/DatabaseQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function size($queue = null)
* @param string|null $queue
* @return mixed
*/
public function push($job, $data = '', $queue = null)
protected function pushJob($job, $data = '', $queue = null)
{
return $this->pushToDatabase($queue, $this->createPayload(
$job, $this->getQueue($queue), $data
Expand Down Expand Up @@ -106,7 +106,7 @@ public function pushRaw($payload, $queue = null, array $options = [])
* @param string|null $queue
* @return void
*/
public function later($delay, $job, $data = '', $queue = null)
protected function laterJob($delay, $job, $data = '', $queue = null)
{
return $this->pushToDatabase($queue, $this->createPayload(
$job, $this->getQueue($queue), $data
Expand Down
29 changes: 29 additions & 0 deletions src/Illuminate/Queue/Events/JobQueued.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Illuminate\Queue\Events;

class JobQueued
{
/**
* @var string|object
*/
public $job;

/**
* @var string|int|null
*/
public $jobId;

/**
* JobQueued constructor.
*
* @param string|object $job
* @param string|int|null $jobId
* @return void
*/
public function __construct($job, $jobId)
{
$this->job = $job;
$this->jobId = $jobId;
}
}
4 changes: 2 additions & 2 deletions src/Illuminate/Queue/NullQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function size($queue = null)
* @param string|null $queue
* @return mixed
*/
public function push($job, $data = '', $queue = null)
protected function pushJob($job, $data = '', $queue = null)
{
//
}
Expand All @@ -52,7 +52,7 @@ public function pushRaw($payload, $queue = null, array $options = [])
* @param string|null $queue
* @return mixed
*/
public function later($delay, $job, $data = '', $queue = null)
protected function laterJob($delay, $job, $data = '', $queue = null)
{
//
}
Expand Down
67 changes: 67 additions & 0 deletions src/Illuminate/Queue/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Closure;
use DateTimeInterface;
use Illuminate\Container\Container;
use Illuminate\Queue\Events\JobQueued;
use Illuminate\Support\InteractsWithTime;
use Illuminate\Support\Str;

Expand Down Expand Up @@ -33,6 +34,31 @@ abstract class Queue
*/
protected static $createPayloadCallbacks = [];

/**
* Push a new job onto the queue.
*
* @param string|object $job
* @param mixed $data
* @param string|null $queue
* @return mixed
*/
public function push($job, $data = '', $queue = null)
{
return tap($this->pushJob($job, $data, $queue), function ($jobId) use ($job) {
$this->raiseJobQueuedEvent($job, $jobId);
});
}

/**
* Push a new job onto the queue.
*
* @param string $job
* @param mixed $data
* @param string|null $queue
* @return mixed
*/
abstract protected function pushJob($job, $data = '', $queue = null);

/**
* Push a new job onto the queue.
*
Expand All @@ -46,6 +72,33 @@ public function pushOn($queue, $job, $data = '')
return $this->push($job, $data, $queue);
}

/**
* Push a new job onto the queue after a delay.
*
* @param \DateTimeInterface|\DateInterval|int $delay
* @param string|object $job
* @param mixed $data
* @param string|null $queue
* @return mixed
*/
public function later($delay, $job, $data = '', $queue = null)
{
return tap($this->laterJob($delay, $job, $data, $queue), function ($jobId) use ($job) {
$this->raiseJobQueuedEvent($job, $jobId);
});
}

/**
* Push a new job onto the queue after a delay.
*
* @param \DateTimeInterface|\DateInterval|int $delay
* @param string $job
* @param mixed $data
* @param string|null $queue
* @return mixed
*/
abstract protected function laterJob($delay, $job, $data = '', $queue = null);

/**
* Push a new job onto the queue after a delay.
*
Expand Down Expand Up @@ -288,4 +341,18 @@ public function setContainer(Container $container)
{
$this->container = $container;
}

/**
* Raise the job queued event.
*
* @param string|object $job
* @param string|int|null $jobId
* @return void
*/
protected function raiseJobQueuedEvent($job, $jobId)
{
if ($this->container->bound('events')) {
$this->container['events']->dispatch(new JobQueued($job, $jobId));
}
}
}
4 changes: 2 additions & 2 deletions src/Illuminate/Queue/RedisQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public function bulk($jobs, $data = '', $queue = null)
* @param string|null $queue
* @return mixed
*/
public function push($job, $data = '', $queue = null)
protected function pushJob($job, $data = '', $queue = null)
{
return $this->pushRaw($this->createPayload($job, $this->getQueue($queue), $data), $queue);
}
Expand Down Expand Up @@ -137,7 +137,7 @@ public function pushRaw($payload, $queue = null, array $options = [])
* @param string|null $queue
* @return mixed
*/
public function later($delay, $job, $data = '', $queue = null)
protected function laterJob($delay, $job, $data = '', $queue = null)
{
return $this->laterRaw($delay, $this->createPayload($job, $this->getQueue($queue), $data), $queue);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Illuminate/Queue/SqsQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function size($queue = null)
* @param string|null $queue
* @return mixed
*/
public function push($job, $data = '', $queue = null)
protected function pushJob($job, $data = '', $queue = null)
{
return $this->pushRaw($this->createPayload($job, $queue ?: $this->default, $data), $queue);
}
Expand Down Expand Up @@ -109,7 +109,7 @@ public function pushRaw($payload, $queue = null, array $options = [])
* @param string|null $queue
* @return mixed
*/
public function later($delay, $job, $data = '', $queue = null)
protected function laterJob($delay, $job, $data = '', $queue = null)
{
return $this->sqs->sendMessage([
'QueueUrl' => $this->getQueue($queue),
Expand Down
4 changes: 2 additions & 2 deletions src/Illuminate/Queue/SyncQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function size($queue = null)
*
* @throws \Throwable
*/
public function push($job, $data = '', $queue = null)
protected function pushJob($job, $data = '', $queue = null)
{
$queueJob = $this->resolveJob($this->createPayload($job, $queue, $data), $queue);

Expand Down Expand Up @@ -142,7 +142,7 @@ public function pushRaw($payload, $queue = null, array $options = [])
* @param string|null $queue
* @return mixed
*/
public function later($delay, $job, $data = '', $queue = null)
protected function laterJob($delay, $job, $data = '', $queue = null)
{
return $this->push($job, $data, $queue);
}
Expand Down