Skip to content
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

Add delay capability to rejections for Redis #1333

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
35 changes: 31 additions & 4 deletions pkg/redis/RedisConsumer.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ class RedisConsumer implements Consumer
*/
private $redeliveryDelay = 300;

/**
* @var int
*/
private $deliveryDelay;

public function __construct(RedisContext $context, RedisDestination $queue)
{
$this->context = $context;
Expand All @@ -47,6 +52,19 @@ public function setRedeliveryDelay(int $delay): void
$this->redeliveryDelay = $delay;
}

/**
* @return int
*/
public function getDeliveryDelay(): ?int
{
return $this->deliveryDelay;
}

public function setDeliveryDelay(int $deliveryDelay): void
{
$this->deliveryDelay = $deliveryDelay;
}

/**
* @return RedisDestination
*/
Expand Down Expand Up @@ -92,7 +110,7 @@ public function acknowledge(Message $message): void
/**
* @param RedisMessage $message
*/
public function reject(Message $message, bool $requeue = false): void
public function reject(Message $message, bool $requeue = false, $delay = 0): void
{
InvalidMessageException::assertMessageInstanceOf($message, RedisMessage::class);

Expand All @@ -102,13 +120,22 @@ public function reject(Message $message, bool $requeue = false): void
$message = $this->getContext()->getSerializer()->toMessage($message->getReservedKey());
$message->setRedelivered(true);

if (null !== $this->deliveryDelay && null === $message->getDeliveryDelay()) {
$message->setDeliveryDelay($this->deliveryDelay);
}

if ($message->getTimeToLive()) {
$message->setHeader('expires_at', time() + $message->getTimeToLive());
}

$payload = $this->getContext()->getSerializer()->toString($message);

$this->getRedis()->lpush($this->queue->getName(), $payload);
if($message->getDeliveryDelay()) {
$deliveryAt = time() + $message->getDeliveryDelay() / 1000;
$payload = $this->getContext()->getSerializer()->toString($message);
$this->getRedis()->zadd($this->queue->getName().':delayed', $payload, $deliveryAt);
} else {
$payload = $this->getContext()->getSerializer()->toString($message);
$this->getRedis()->lpush($this->queue->getName(), $payload);
}
}
}

Expand Down