2020-09-26 23:05:33 +00:00
|
|
|
<?php
|
2020-10-14 22:19:31 +00:00
|
|
|
|
2020-09-26 23:05:33 +00:00
|
|
|
namespace App\MessageQueue;
|
|
|
|
|
|
|
|
use App\Message\AbstractMessage;
|
|
|
|
use Redis;
|
|
|
|
use Symfony\Component\Messenger\Bridge\Redis\Transport\Connection;
|
|
|
|
use Symfony\Component\Messenger\Bridge\Redis\Transport\RedisTransport;
|
|
|
|
use Symfony\Component\Messenger\Envelope;
|
|
|
|
use Symfony\Component\Messenger\Transport\Sender\SendersLocatorInterface;
|
|
|
|
|
|
|
|
class QueueManager implements SendersLocatorInterface
|
|
|
|
{
|
|
|
|
public const QUEUE_HIGH_PRIORITY = 'high_priority';
|
|
|
|
public const QUEUE_NORMAL_PRIORITY = 'normal_priority';
|
|
|
|
public const QUEUE_LOW_PRIORITY = 'low_priority';
|
|
|
|
public const QUEUE_MEDIA = 'media';
|
|
|
|
|
|
|
|
public Redis $redis;
|
|
|
|
|
|
|
|
/** @var Connection[] */
|
|
|
|
public array $connections = [];
|
|
|
|
|
2020-11-15 00:06:34 +00:00
|
|
|
public string $workerName = 'app';
|
|
|
|
|
2020-09-26 23:05:33 +00:00
|
|
|
public function __construct(Redis $redis)
|
|
|
|
{
|
|
|
|
$this->redis = $redis;
|
|
|
|
}
|
|
|
|
|
2020-11-15 00:06:34 +00:00
|
|
|
public function setWorkerName(string $workerName): void
|
|
|
|
{
|
|
|
|
$this->workerName = $workerName;
|
|
|
|
}
|
|
|
|
|
2020-10-14 22:19:31 +00:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
2020-09-26 23:05:33 +00:00
|
|
|
public function getSenders(Envelope $envelope): iterable
|
|
|
|
{
|
|
|
|
$message = $envelope->getMessage();
|
|
|
|
|
|
|
|
if (!$message instanceof AbstractMessage) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
$queue = $message->getQueue();
|
|
|
|
return [
|
|
|
|
$this->getTransport($queue),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getConnection(string $queueName): Connection
|
|
|
|
{
|
2020-11-15 00:06:34 +00:00
|
|
|
$cacheName = $queueName . '_' . $this->workerName;
|
|
|
|
|
|
|
|
if (!isset($this->connections[$cacheName])) {
|
|
|
|
$this->connections[$cacheName] = new Connection(
|
2020-09-26 23:05:33 +00:00
|
|
|
[
|
|
|
|
'stream' => 'messages_' . $queueName,
|
2020-11-15 00:06:34 +00:00
|
|
|
'consumer' => $this->workerName,
|
2020-09-26 23:05:33 +00:00
|
|
|
'delete_after_ack' => true,
|
|
|
|
'redeliver_timeout' => 43200,
|
|
|
|
'claim_interval' => 86400,
|
|
|
|
],
|
|
|
|
array_filter([
|
|
|
|
'host' => $this->redis->getHost(),
|
|
|
|
'port' => $this->redis->getPort(),
|
|
|
|
'auth' => $this->redis->getAuth(),
|
|
|
|
])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-11-15 00:06:34 +00:00
|
|
|
return $this->connections[$cacheName];
|
2020-09-26 23:05:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getTransport(string $queueName): RedisTransport
|
|
|
|
{
|
|
|
|
return new RedisTransport($this->getConnection($queueName));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return RedisTransport[]
|
|
|
|
*/
|
|
|
|
public function getTransports(): array
|
|
|
|
{
|
|
|
|
$allQueues = self::getAllQueues();
|
|
|
|
|
|
|
|
$transports = [];
|
|
|
|
foreach ($allQueues as $queueName) {
|
|
|
|
$transports[$queueName] = $this->getTransport($queueName);
|
|
|
|
}
|
|
|
|
return $transports;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Connection[]
|
|
|
|
*/
|
|
|
|
public function getConnections(): array
|
|
|
|
{
|
|
|
|
$allQueues = self::getAllQueues();
|
|
|
|
|
|
|
|
$connections = [];
|
|
|
|
foreach ($allQueues as $queueName) {
|
|
|
|
$connections[$queueName] = $this->getConnection($queueName);
|
|
|
|
}
|
|
|
|
return $connections;
|
|
|
|
}
|
|
|
|
|
2020-09-30 03:49:16 +00:00
|
|
|
public function clearQueue(string $queueName): void
|
|
|
|
{
|
|
|
|
$connection = $this->getConnection($queueName);
|
|
|
|
|
|
|
|
$connection->cleanup();
|
|
|
|
$connection->setup();
|
|
|
|
}
|
|
|
|
|
2020-10-14 22:19:31 +00:00
|
|
|
/**
|
|
|
|
* @return string[]
|
|
|
|
*/
|
2020-09-26 23:05:33 +00:00
|
|
|
public static function getAllQueues(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
self::QUEUE_HIGH_PRIORITY,
|
|
|
|
self::QUEUE_NORMAL_PRIORITY,
|
|
|
|
self::QUEUE_LOW_PRIORITY,
|
|
|
|
self::QUEUE_MEDIA,
|
|
|
|
];
|
|
|
|
}
|
2020-10-14 22:19:31 +00:00
|
|
|
}
|