Skip to content

Commit 4d743dc

Browse files
author
Aviv Laufer
committed
Sync with the go client
1 parent 5584cea commit 4d743dc

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed

protoconfloader/protoconfloader.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,16 @@ class Configuration:
5656
function to be executed whenever the configuration changes.
5757
"""
5858

59-
def __init__(self, message: Any, service_name: str, logger: logging.Logger) -> None:
59+
def __init__(self, message: Any, config_path: str, logger: logging.Logger) -> None:
6060
"""
6161
Initializes the configuration with the given message and service name.
6262
"""
6363
self.message = message
64-
self.service_name = service_name
6564
self.logger = logger
6665
self.is_loaded = False
6766
self.is_watching_file = False
6867
self.is_watching_agent = False
69-
self.config_dir = None
68+
self.config_path = config_path
7069
self.config_file = None
7170
self.lock = asyncio.Lock()
7271
self.on_config_change_callback = None
@@ -78,14 +77,13 @@ def set_logger(self, logger: logging.Logger) -> None:
7877
"""
7978
self.logger = logger
8079

81-
async def load_config(self, config_dir: str, config_name: str) -> None:
80+
async def load_config(self, config_path: str, config_name: str) -> None:
8281
"""
8382
Loads the configuration from the specified file.
8483
"""
8584
if self.is_loaded:
8685
return
87-
self.config_dir = config_dir
88-
self.config_file = config_name
86+
self.config_file = os.path.join(config_path, config_name)
8987
try:
9088
await self._load_config()
9189
self.is_loaded = True
@@ -99,8 +97,7 @@ async def _load_config(self) -> None:
9997
"""
10098
try:
10199

102-
config_path = pathlib.Path(self.config_dir) / self.config_file
103-
async with aiofiles.open(config_path, "r", encoding="utf-8") as f:
100+
async with aiofiles.open(self.config_file, "r", encoding="utf-8") as f:
104101
config_data = json.loads(await f.read())
105102
async with self.lock:
106103

@@ -144,7 +141,7 @@ async def _file_watcher(self, delay: int = 0) -> None:
144141
self.logger.info("Starting watching config file")
145142
handler = _EventHandler(self._load_config, self.config_file)
146143
observer = Observer()
147-
observer.schedule(handler, self.service_name, recursive=True)
144+
observer.schedule(handler, self.config_file, recursive=True)
148145
observer.start()
149146
self.logger.info("Observer started")
150147
try:
@@ -160,7 +157,7 @@ async def watch_config(self, delay: int = 0) -> None:
160157
"""
161158
try:
162159
async with asyncio.TaskGroup() as tg:
163-
task1 = tg.create_task(self._listen_to_changes(self.service_name))
160+
task1 = tg.create_task(self._listen_to_changes(self.config_path))
164161
task2 = tg.create_task(self._file_watcher(delay))
165162
self.logger.info(
166163
"Both tasks have completed now: %s, %s",

tests/test_protoconfloader.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,14 @@ async def test_load_invalid_json_config():
3232
return_value='{"crawlers": [}' # Invalid JSON
3333
)
3434
message = CrawlerService()
35-
config = Configuration(message, "test_service", logging.getLogger())
35+
config = Configuration(message, "crawler/text_crawler", logging.getLogger())
3636

3737
# Act & Assert
3838
with patch("builtins.open", mock_open):
3939
with pytest.raises(RuntimeError, match="Error decoding JSON"):
4040
await config.load_config("tests/test_data", "invalid_config.json")
41-
41+
42+
4243
@pytest.mark.asyncio
4344
async def test_listen_to_changes_remote():
4445
# Setup
@@ -52,14 +53,17 @@ async def test_listen_to_changes_remote():
5253
config = Configuration(message, "crawler/text_crawler", logging.getLogger())
5354
mock_callback = AsyncMock()
5455
callback_event = asyncio.Event()
56+
5557
# Act
58+
await config.load_config("tests/test_data", "config.json")
59+
5660
async def async_callback(message):
5761
await mock_callback(message)
5862
callback_event.set()
5963

6064
config.on_config_change(async_callback)
6165
watch_task = asyncio.create_task(config.watch_config())
62-
66+
6367
await asyncio.sleep(0.3)
6468

6569
# Assert

0 commit comments

Comments
 (0)