Skip to content

Commit

Permalink
Use separate connection for publisher
Browse files Browse the repository at this point in the history
  • Loading branch information
akadlec committed Jan 18, 2022
1 parent 4565e92 commit 8a370f8
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
2 changes: 1 addition & 1 deletion redisdb_exchange_plugin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@
Redis DB exchange plugin
"""

__version__ = "0.12.0"
__version__ = "0.13.0"
13 changes: 12 additions & 1 deletion redisdb_exchange_plugin/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@

# Python base dependencies
import logging
import uuid
from typing import Dict, Optional, Union

# Library dependencies
from exchange.consumer import Consumer as ExchangeConsumer
from exchange.publisher import Publisher as ExchangePublisher
from kink import di
from redis import Redis
from whistle import EventDispatcher

# Library libs
Expand Down Expand Up @@ -70,7 +72,10 @@ def register_services(
di[Logger] = Logger(logger=logger)
di["fb-redisdb-exchange-plugin_logger"] = di[Logger]

identifier = uuid.uuid4().__str__()

di[Connection] = Connection(
identifier=identifier,
host=str(settings.get("host")),
port=int(str(settings.get("port"))),
channel_name=str(settings.get("channel_name")),
Expand All @@ -90,8 +95,14 @@ def register_services(
di["fb-redisdb-exchange-plugin_client"] = di[Client]

di[Publisher] = Publisher(
identifier=identifier,
channel_name=str(settings.get("channel_name", "fb_exchange")),
connection=di[Connection],
connection=Redis(
host=str(settings.get("host")),
port=int(str(settings.get("port"))),
username=str(settings.get("username", None)) if settings.get("username", None) is not None else None,
password=str(settings.get("password", None)) if settings.get("password", None) is not None else None,
),
logger=di[Logger],
)

Expand Down
4 changes: 2 additions & 2 deletions redisdb_exchange_plugin/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

# Python base dependencies
import json
import uuid
from typing import Dict, Optional, Union

# Library dependencies
Expand Down Expand Up @@ -57,6 +56,7 @@ class Connection(Redis): # pylint: disable=abstract-method,too-many-ancestors

def __init__( # pylint: disable=too-many-arguments
self,
identifier: str,
host: str,
port: int,
channel_name: str,
Expand All @@ -72,7 +72,7 @@ def __init__( # pylint: disable=too-many-arguments
password=password,
)

self.__identifier = uuid.uuid4().__str__()
self.__identifier = identifier
self.__channel_name = channel_name

self.__event_dispatcher = event_dispatcher
Expand Down
11 changes: 7 additions & 4 deletions redisdb_exchange_plugin/publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
from exchange.publisher import IPublisher
from metadata.routing import RoutingKey
from metadata.types import ModuleOrigin, PluginOrigin
from redis import Redis

# Library libs
from redisdb_exchange_plugin.connection import Connection
from redisdb_exchange_plugin.logger import Logger


Expand All @@ -42,20 +42,23 @@ class Publisher(IPublisher): # pylint: disable=too-few-public-methods
@author Adam Kadlec <adam.kadlec@fastybird.com>
"""

__identifier: str
__channel_name: str

__connection: Connection
__connection: Redis

__logger: Logger

# -----------------------------------------------------------------------------

def __init__(
self,
identifier: str,
channel_name: str,
connection: Connection,
connection: Redis,
logger: Logger,
) -> None:
self.__identifier = identifier
self.__channel_name = channel_name

self.__connection = connection
Expand All @@ -69,7 +72,7 @@ def publish(self, origin: Union[ModuleOrigin, PluginOrigin], routing_key: Routin
message = {
"routing_key": routing_key.value,
"origin": origin.value,
"sender_id": self.__connection.identifier,
"sender_id": self.__identifier,
"data": data,
}

Expand Down

0 comments on commit 8a370f8

Please sign in to comment.