-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
92d8bf5
commit cd98523
Showing
14 changed files
with
286 additions
and
236 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Enqueue\Redis; | ||
|
||
class JsonSerializer implements Serializer | ||
{ | ||
public function toString(RedisMessage $message): string | ||
{ | ||
$json = json_encode([ | ||
'body' => $message->getBody(), | ||
'properties' => $message->getProperties(), | ||
'headers' => $message->getHeaders(), | ||
]); | ||
|
||
if (JSON_ERROR_NONE !== json_last_error()) { | ||
throw new \InvalidArgumentException(sprintf( | ||
'The malformed json given. Error %s and message %s', | ||
json_last_error(), | ||
json_last_error_msg() | ||
)); | ||
} | ||
|
||
return $json; | ||
} | ||
|
||
public function toMessage(string $string): RedisMessage | ||
{ | ||
$data = json_decode($string, true); | ||
if (JSON_ERROR_NONE !== json_last_error()) { | ||
throw new \InvalidArgumentException(sprintf( | ||
'The malformed json given. Error %s and message %s', | ||
json_last_error(), | ||
json_last_error_msg() | ||
)); | ||
} | ||
|
||
return new RedisMessage($data['body'], $data['properties'], $data['headers']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Enqueue\Redis; | ||
|
||
trait RedisConsumerHelperTrait | ||
{ | ||
/** | ||
* @var string[] | ||
*/ | ||
protected $queueNames; | ||
|
||
abstract function getContext(): RedisContext; | ||
|
||
/** | ||
* @param RedisDestination[] $queues | ||
* @param int $timeout | ||
* @param int $redeliveryDelay | ||
* | ||
* @return RedisMessage|null | ||
*/ | ||
protected function receiveMessage(array $queues, int $timeout, int $redeliveryDelay): ?RedisMessage | ||
{ | ||
$startAt = time(); | ||
$thisTimeout = $timeout; | ||
|
||
if (null === $this->queueNames) { | ||
$this->queueNames = []; | ||
foreach ($queues as $queue) { | ||
$this->queueNames[] = $queue->getName(); | ||
} | ||
} | ||
|
||
while ($thisTimeout > 0) { | ||
$this->migrateExpiredMessages($this->queueNames); | ||
|
||
if ($result = $this->getContext()->getRedis()->brpop($this->queueNames, $thisTimeout)) { | ||
$this->pushQueueNameBack($result->getKey()); | ||
|
||
if ($message = $this->processResult($result, $redeliveryDelay)) { | ||
return $message; | ||
} | ||
} | ||
|
||
$thisTimeout -= time() - $startAt; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
protected function receiveMessageNoWait(RedisDestination $destination, int $redeliveryDelay): ?RedisMessage | ||
{ | ||
$this->migrateExpiredMessages([$destination->getName()]); | ||
|
||
if ($result = $this->getContext()->getRedis()->rpop($destination->getName())) { | ||
return $this->processResult($result, $redeliveryDelay); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
protected function processResult(RedisResult $result, int $redeliveryDelay): ?RedisMessage | ||
{ | ||
$message = $this->getContext()->getSerializer()->toMessage($result->getMessage()); | ||
|
||
$now = time(); | ||
|
||
if ($expiresAt = $message->getHeader('expires_at')) { | ||
if ($now > $expiresAt) { | ||
return null; | ||
} | ||
} | ||
|
||
$message->setHeader('attempts', $message->getAttempts() + 1); | ||
$message->setRedelivered($message->getAttempts() > 1); | ||
$message->setKey($result->getKey()); | ||
$message->setReservedKey($this->getContext()->getSerializer()->toString($message)); | ||
|
||
$reservedQueue = $result->getKey().':reserved'; | ||
$redeliveryAt = $now + $redeliveryDelay; | ||
|
||
$this->getContext()->getRedis()->zadd($reservedQueue, $message->getReservedKey(), $redeliveryAt); | ||
|
||
return $message; | ||
} | ||
|
||
protected function pushQueueNameBack(string $queueName): void | ||
{ | ||
if (count($this->queueNames) <= 1) { | ||
return; | ||
} | ||
|
||
if (false === $from = array_search($queueName, $this->queueNames, true)) { | ||
throw new \LogicException(sprintf('Queue name was not found: "%s"', $queueName)); | ||
} | ||
|
||
$to = count($this->queueNames) - 1; | ||
|
||
$out = array_splice($this->queueNames, $from, 1); | ||
array_splice($this->queueNames, $to, 0, $out); | ||
} | ||
|
||
protected function migrateExpiredMessages(array $queueNames): void | ||
{ | ||
$now = time(); | ||
|
||
foreach ($queueNames as $queueName) { | ||
$this->getContext()->getRedis() | ||
->eval(LuaScripts::migrateExpired(), [$queueName.':delayed', $queueName], [$now]); | ||
|
||
$this->getContext()->getRedis() | ||
->eval(LuaScripts::migrateExpired(), [$queueName.':reserved', $queueName], [$now]); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.