Skip to content
Open
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
60 changes: 43 additions & 17 deletions dapr/clients/grpc/_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import contextlib
import json
import logging
import threading
from enum import Enum
from typing import (
Expand Down Expand Up @@ -669,13 +670,20 @@ def items(self) -> Dict[Text, ConfigurationItem]:
return self._items


logger = logging.getLogger(__name__)


class ConfigurationWatcher:
def __init__(self):
self.store_name = None
self.keys = None
self.event: threading.Event = threading.Event()
self._stopped: threading.Event = threading.Event()
self.id: str = ''

def stop(self):
self._stopped.set()

def watch_configuration(
self,
stub: api_service_v1.DaprStub,
Expand All @@ -687,11 +695,11 @@ def watch_configuration(
req = api_v1.SubscribeConfigurationRequest(
store_name=store_name, keys=keys, metadata=config_metadata
)
self.keys = keys
self.store_name = store_name
thread = threading.Thread(target=self._read_subscribe_config, args=(stub, req, handler))
thread.daemon = True
thread.start()
self.keys = keys
self.store_name = store_name
check = self.event.wait(timeout=5)
if not check:
print(f'Unable to get configuration id for keys {self.keys}')
Expand All @@ -704,21 +712,39 @@ def _read_subscribe_config(
req: api_v1.SubscribeConfigurationRequest,
handler: Callable[[Text, ConfigurationResponse], None],
):
try:
responses: List[api_v1.SubscribeConfigurationResponse] = (
stub.SubscribeConfigurationAlpha1(req)
)
isFirst = True
for response in responses:
if isFirst:
self.id = response.id
self.event.set()
isFirst = False
if len(response.items) > 0:
handler(response.id, ConfigurationResponse(response.items))
except Exception:
print(f'{self.store_name} configuration watcher for keys {self.keys} stopped.')
pass
while not self._stopped.is_set():
try:
responses: List[api_v1.SubscribeConfigurationResponse] = (
stub.SubscribeConfigurationAlpha1(req)
)
isFirst = True
for response in responses:
if self._stopped.is_set():
return
if isFirst:
self.id = response.id
self.event.set()
isFirst = False
if len(response.items) > 0:
try:
handler(response.id, ConfigurationResponse(response.items))
except Exception:
logger.exception(
'%s configuration handler for keys %s raised an exception.',
self.store_name,
self.keys,
)
except Exception:
if self._stopped.is_set():
return
logger.warning(
'%s configuration watcher for keys %s disconnected; reconnecting.',
self.store_name,
self.keys,
exc_info=True,
)
if self._stopped.wait(5):
return


class BulkPublishResponseFailedEntry:
Expand Down