Skip to content

Commit

Permalink
Let queue handle DateInterval and DateTimeInterface.
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmichot committed Jul 17, 2017
1 parent 722798c commit 5abcd1f
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/Illuminate/Bus/Queueable.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ trait Queueable
/**
* The number of seconds before the job should be made available.
*
* @var \DateTime|int|null
* @var \DateTimeInterface|\DateInterval|int|null
*/
public $delay;

Expand Down Expand Up @@ -61,7 +61,7 @@ public function onQueue($queue)
/**
* Set the desired delay for the job.
*
* @param \DateTime|int|null $delay
* @param \DateTimeInterface|\Da|int|null $delay
* @return $this
*/
public function delay($delay)
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Contracts/Mail/MailQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function queue($view, $queue = null);
/**
* Queue a new e-mail message for sending after (n) seconds.
*
* @param \DateTime|int $delay
* @param \DateTimeInterface|\DateInterval|int $delay
* @param string|array|MailableContract $view
* @param string $queue
* @return mixed
Expand Down
4 changes: 2 additions & 2 deletions src/Illuminate/Contracts/Queue/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function pushRaw($payload, $queue = null, array $options = []);
/**
* Push a new job onto the queue after a delay.
*
* @param \DateTime|int $delay
* @param \DateTimeInterface|\DateInterval|int $delay
* @param string $job
* @param mixed $data
* @param string $queue
Expand All @@ -57,7 +57,7 @@ public function later($delay, $job, $data = '', $queue = null);
* Push a new job onto the queue after a delay.
*
* @param string $queue
* @param \DateTime|int $delay
* @param \DateTimeInterface|\DateInterval|int $delay
* @param string $job
* @param mixed $data
* @return mixed
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Mail/Mailable.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public function queue(Queue $queue)
/**
* Deliver the queued message after the given delay.
*
* @param \DateTime|int $delay
* @param \DateTimeInterface|\DateInterval|int $delay
* @param Queue $queue
* @return mixed
*/
Expand Down
4 changes: 2 additions & 2 deletions src/Illuminate/Mail/Mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ public function queueOn($queue, $view)
/**
* Queue a new e-mail message for sending after (n) seconds.
*
* @param \DateTime|int $delay
* @param \DateTimeInterface|\DateInterval|int $delay
* @param string|array|MailableContract $view
* @param string|null $queue
* @return mixed
Expand All @@ -404,7 +404,7 @@ public function later($delay, $view, $queue = null)
* Queue a new e-mail message for sending after (n) seconds on the given queue.
*
* @param string $queue
* @param \DateTime|int $delay
* @param \DateTimeInterface|\DateInterval|int $delay
* @param string|array $view
* @return mixed
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Mail/PendingMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public function queue(Mailable $mailable)
/**
* Deliver the queued message after the given delay.
*
* @param \DateTime|int $delay
* @param \DateTimeInterface|\DateInterval|int $delay
* @param Mailable $mailable
* @return mixed
*/
Expand Down
25 changes: 23 additions & 2 deletions src/Illuminate/Queue/InteractsWithTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Illuminate\Queue;

use DateTime;
use DateInterval;
use DateTimeInterface;
use Illuminate\Support\Carbon;

Expand All @@ -10,11 +12,13 @@ trait InteractsWithTime
/**
* Get the number of seconds until the given DateTime.
*
* @param \DateTimeInterface $delay
* @param \DateTimeInterface|\DateInterval $delay
* @return int
*/
protected function secondsUntil($delay)
{
$delay = $this->handlesInterval($delay);

return $delay instanceof DateTimeInterface
? max(0, $delay->getTimestamp() - $this->currentTime())
: (int) $delay;
Expand All @@ -23,16 +27,33 @@ protected function secondsUntil($delay)
/**
* Get the "available at" UNIX timestamp.
*
* @param \DateTimeInterface|int $delay
* @param \DateTimeInterface|\DateInterval|int $delay
* @return int
*/
protected function availableAt($delay = 0)
{
$delay = $this->handlesInterval($delay);

return $delay instanceof DateTimeInterface
? $delay->getTimestamp()
: Carbon::now()->addSeconds($delay)->getTimestamp();
}

/**
* Converts an interval to a DateTime instance.
*
* @param \DateTimeInterface|\DateInterval|int
* @return \DateTime
*/
protected function handlesInterval($delay)
{
if ($delay instanceof DateInterval) {
$delay = (new DateTime)->add($delay);
}

return $delay;
}

/**
* Get the current system time as a UNIX timestamp.
*
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Queue/SqsQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public function pushRaw($payload, $queue = null, array $options = [])
/**
* Push a new job onto the queue after a delay.
*
* @param \DateTime|int $delay
* @param \DateTimeInterface|\DateInterval|int $delay
* @param string $job
* @param mixed $data
* @param string $queue
Expand Down

0 comments on commit 5abcd1f

Please sign in to comment.