Skip to content

Commit

Permalink
Refactor RedisServerMutex and TriggerSubscriber classes (#543)
Browse files Browse the repository at this point in the history
* Refactor RedisServerMutex and TriggerSubscriber classes

* Refactor RedisServerMutex constructor

* Fix RedisServerMutex connection assignment

---------

Co-authored-by: Deeka Wong <8337659+huangdijia@users.noreply.github.com>
  • Loading branch information
huangdijia and huangdijia authored Feb 3, 2024
1 parent f9bf752 commit a8e12b1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
14 changes: 9 additions & 5 deletions src/Mutex/RedisServerMutex.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@

use FriendsOfHyperf\Trigger\Traits\Logger;
use FriendsOfHyperf\Trigger\Util;
use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\Coordinator\CoordinatorManager;
use Hyperf\Coordinator\Timer;
use Hyperf\Redis\Redis;
use Psr\Log\LoggerInterface;
use RedisException;
use Throwable;

Expand All @@ -36,19 +36,23 @@ class RedisServerMutex implements ServerMutexInterface

protected string $connection = 'default';

protected ?LoggerInterface $logger = null;

public function __construct(
protected Redis $redis,
protected ?string $name = null,
protected ?string $owner = null,
array $options = [],
protected ?StdoutLoggerInterface $logger = null
array $options = []
) {
$this->expires = (int) ($options['expires'] ?? 60);
$this->keepaliveInterval = (int) ($options['keepalive_interval'] ?? 10);
$this->name = $name ?? sprintf('trigger:server:%s', $this->connection);
$this->owner = $owner ?? Util::getInternalIp();
$this->connection = $options['connection'];
$this->timer = new Timer($logger);
if (isset($options['connection'])) {
$this->connection = $options['connection'];
}
$this->logger = $this->getLogger();
$this->timer = new Timer($this->logger);
$this->retryInterval = (int) ($options['retry_interval'] ?? 10);
}

Expand Down
8 changes: 5 additions & 3 deletions src/Subscriber/TriggerSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
use FriendsOfHyperf\Trigger\Consumer;
use FriendsOfHyperf\Trigger\Traits\Logger;
use FriendsOfHyperf\Trigger\TriggerManager;
use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\Coroutine\Concurrent;
use MySQLReplication\Definitions\ConstEventsNames;
use MySQLReplication\Event\DTO\EventDTO;
use MySQLReplication\Event\DTO\RowsDTO;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
use Throwable;

use function Hyperf\Support\call;
Expand All @@ -30,15 +30,17 @@ class TriggerSubscriber extends AbstractSubscriber

protected Concurrent $concurrent;

protected ?LoggerInterface $logger = null;

public function __construct(
protected ContainerInterface $container,
protected TriggerManager $triggerManager,
protected Consumer $consumer,
protected ?StdoutLoggerInterface $logger = null
protected Consumer $consumer
) {
$this->concurrent = new Concurrent(
(int) ($consumer->getOption('concurrent.limit') ?? 1)
);
$this->logger = $this->getLogger();
}

public static function getSubscribedEvents(): array
Expand Down

0 comments on commit a8e12b1

Please sign in to comment.