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

Fix: Redis client name is lost after reconnection #173

Merged
merged 4 commits into from
Jul 14, 2022
Merged
Show file tree
Hide file tree
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
1 change: 0 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ jobs:
- uses: CERT-Polska/lint-python-action@v1
with:
source: karton/
extra-requirements: "types-redis"
unittest:
runs-on: ubuntu-latest
strategy:
Expand Down
20 changes: 14 additions & 6 deletions karton/core/backend.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import enum
import json
import time
import warnings
from collections import defaultdict, namedtuple
from io import BytesIO
from typing import Any, BinaryIO, Dict, Iterator, List, Optional, Set, Tuple, Union
Expand Down Expand Up @@ -40,27 +41,30 @@ class KartonMetrics(enum.Enum):


class KartonBackend:
def __init__(self, config):
def __init__(self, config, identity: Optional[str] = None) -> None:
self.config = config
self.redis = self.make_redis(config)
self.identity = identity
self.redis = self.make_redis(config, identity=identity)
self.minio = Minio(
endpoint=config["minio"]["address"],
access_key=config["minio"]["access_key"],
secret_key=config["minio"]["secret_key"],
secure=config.config.getboolean("minio", "secure", fallback=True),
)

def make_redis(self, config) -> StrictRedis:
def make_redis(self, config, identity: Optional[str] = None) -> StrictRedis:
"""
Create and test a Redis connection.

:param config: The karton configuration
:return: Redis conection
:param identity: Karton service identity
:return: Redis connection
"""
redis_args = {
"host": config["redis"]["host"],
"port": int(config["redis"].get("port", 6379)),
"password": config["redis"].get("password"),
"client_name": identity,
"decode_responses": True,
}
try:
Expand Down Expand Up @@ -213,11 +217,15 @@ def unregister_bind(self, identity: str) -> None:
"""
self.redis.hdel(KARTON_BINDS_HSET, identity)

def set_consumer_identity(self, identity: str) -> None:
def set_consumer_identity(self, _: str) -> None:
"""
Sets identity for current Redis connection
"""
return self.redis.client_setname(identity)
warnings.warn(
"set_consumer_identity is deprecated and does nothing from v4.5.0. "
"Use identity constructor argument instead",
DeprecationWarning,
)

def get_online_consumers(self) -> Dict[str, List[str]]:
"""
Expand Down
2 changes: 1 addition & 1 deletion karton/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def __init__(
self.identity = identity

self.config = config or Config()
self.backend = backend or KartonBackend(self.config)
self.backend = backend or KartonBackend(self.config, identity=self.identity)

self.log_handler = KartonLogHandler(backend=self.backend, channel=self.identity)
self.current_task: Optional[Task] = None
Expand Down
2 changes: 0 additions & 2 deletions karton/core/karton.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,6 @@ def loop(self) -> None:
for task_filter in self.filters:
self.log.info("Binding on: %s", task_filter)

self.backend.set_consumer_identity(self.identity)

try:
while not self.shutdown:
if self.backend.get_bind(self.identity) != self._bind:
Expand Down