Skip to content

Commit

Permalink
Added support for delay message
Browse files Browse the repository at this point in the history
  • Loading branch information
munir131 committed Oct 7, 2019
1 parent 04fedb1 commit e7847ea
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/PubSubQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ public function pop($subscriber = null)
'returnImmediately' => true,
'maxMessages' => 1,
]);
$messages = $this->checkAvailabilty($messages);

if (!empty($messages) && count($messages) > 0) {
return new PubSubJob(
Expand All @@ -178,6 +179,18 @@ public function pop($subscriber = null)
}
}

public function checkAvailabilty($messages) {
if (!empty($messages) && count($messages) > 0) {
$availableAt = $messages[0]->attribute('available_at');
$now = (new \DateTime('now'))->getTimestamp();
if (!$availableAt) {
return $messages;
} else if ($availableAt < $now) {
return $messages;
}
}
}

/**
* Push an array of jobs onto the queue.
*
Expand Down
26 changes: 26 additions & 0 deletions tests/Unit/PubSubQueueTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,4 +298,30 @@ public function testGetPubSub()
{
$this->assertTrue($this->queue->getPubSub() instanceof PubSubClient);
}

public function testDelayedMessage()
{
$this->subscription->method('pull')
->willReturn([$this->message]);

$this->message->method('attribute')
->with($this->equalTo('available_at'))
->willReturn((new \DateTime('now +1seconds'))->getTimestamp());

$this->topic->method('subscription')
->willReturn($this->subscription);

$this->topic->method('exists')
->willReturn(true);

$this->queue->method('getTopic')
->willReturn($this->topic);

$this->queue->setContainer($this->createMock(Container::class));

$this->assertTrue(is_null($this->queue->pop('test')));
sleep(2);
$this->assertTrue($this->queue->pop('test') instanceof PubSubJob);

}
}

0 comments on commit e7847ea

Please sign in to comment.