Skip to content

Commit d4e7c5f

Browse files
author
Aviv Laufer
committed
Demo app
1 parent 10b7d39 commit d4e7c5f

File tree

2 files changed

+65
-3
lines changed

2 files changed

+65
-3
lines changed

demo_app/demo_app.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import asyncio
2+
import logging
3+
4+
from tests.test_data.crawler_pb2 import CrawlerService
5+
from google.protobuf.struct_pb2 import Struct
6+
7+
8+
from protoconfloader.protoconfloader import Configuration
9+
10+
11+
# Set up logging
12+
logging.basicConfig(level=logging.INFO)
13+
logger = logging.getLogger(__name__)
14+
15+
# Create a Struct message to hold our configuration
16+
config_message = CrawlerService()
17+
18+
# Initialize the Configuration object
19+
config = Configuration(
20+
config_message,
21+
"crawler/text_crawler",
22+
logger,
23+
)
24+
25+
26+
# Callback function to handle configuration changes
27+
async def on_config_change(new_config):
28+
"""
29+
This function is a callback for configuration changes.
30+
It logs the changes in the configuration.
31+
"""
32+
logger.info(
33+
"DemoApp Configuration changed:: New Log Level %s", new_config.log_level
34+
)
35+
36+
37+
async def main():
38+
"""
39+
This is the main function of the application.
40+
It sets up the configuration, loads the initial configuration, watches for configuration changes,
41+
and simulates a long-running process.
42+
"""
43+
# Set the callback function
44+
config.on_config_change(on_config_change)
45+
46+
# Load the initial configuration
47+
await config.load_config(".", "config.json")
48+
49+
# Start watching for configuration changes
50+
watch_task = asyncio.create_task(config.watch_config())
51+
52+
# Run the app for a while (simulating a long-running process)
53+
try:
54+
await asyncio.sleep(300) # Run for 5 minutes
55+
except asyncio.CancelledError:
56+
logger.info("App is shutting down")
57+
finally:
58+
watch_task.cancel()
59+
await asyncio.gather(watch_task, return_exceptions=True)
60+
61+
62+
if __name__ == "__main__":
63+
asyncio.run(main())

run_demo.sh

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,5 @@
66

77
pkill -9 protoconf >/dev/null 2>&1
88
protoconf agent -dev tests/test_data/. &
9-
python demo_app.py
10-
pkill -9 protoconf >/dev/null 2>&1
11-
9+
python demo_app/demo_app.py
10+
pkill -9 protoconf >/dev/null 2>&1

0 commit comments

Comments
 (0)