|
| 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()) |
0 commit comments