SIENTIAPDE-1205
Refactor Ingestor and OPC Manager for Asynchronous Operations - Updated main function to be asynchronous and integrated asyncio for better concurrency. - Refactored Ingestor methods to support async operations, including prepare_ingestor, loop, and shutdown. - Enhanced IngestorManager and OpcManager with async methods for improved performance and responsiveness. - Replaced blocking calls with await statements to ensure non-blocking behavior during operations. - Added a new run_async_main function to handle the async event loop setup.
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import os
|
||||
import asyncio
|
||||
import signal
|
||||
import traceback
|
||||
from threading import Event
|
||||
@@ -13,11 +14,11 @@ exit_signal = Event()
|
||||
POD_ID = os.getenv("HOSTNAME", "localhost")
|
||||
|
||||
|
||||
def main():
|
||||
async def main():
|
||||
start_prometheus_server()
|
||||
ingestor = Ingestor()
|
||||
try:
|
||||
ingestor.prepare_ingestor()
|
||||
await ingestor.prepare_ingestor()
|
||||
except Exception as e:
|
||||
metrics.APP_ERRORS_TOTAL.labels(
|
||||
pod_id=POD_ID).inc() # Increment errors
|
||||
@@ -28,11 +29,12 @@ def main():
|
||||
while not exit_signal.is_set():
|
||||
start_time = time() # Start loop timer
|
||||
try:
|
||||
ingestor.loop()
|
||||
await ingestor.loop()
|
||||
metrics.APP_LOOP_COUNT.labels(
|
||||
pod_id=POD_ID).inc() # Increment loop counter
|
||||
|
||||
exit_signal.wait(ingestor.poll_interval)
|
||||
# Use asyncio.sleep instead of exit_signal.wait for better async compatibility
|
||||
await asyncio.sleep(ingestor.poll_interval)
|
||||
|
||||
except KeyboardInterrupt: # Handle Ctrl+C gracefully
|
||||
print("KeyboardInterrupt received. Setting exit_signal flag.")
|
||||
@@ -48,13 +50,13 @@ def main():
|
||||
duration = time() - start_time
|
||||
metrics.APP_LOOP_DURATION.labels(pod_id=POD_ID).observe(duration)
|
||||
|
||||
ingestor.shutdown()
|
||||
await ingestor.shutdown()
|
||||
metrics.APP_UP.labels(pod_id=POD_ID).set(0) # Mark app as DOWN
|
||||
|
||||
ingestor.logger.info("Main loop exit_signaled.")
|
||||
|
||||
# Give Prometheus a chance to scrape one last time before exiting (optional)
|
||||
sleep(5)
|
||||
await asyncio.sleep(5)
|
||||
|
||||
os._exit(0)
|
||||
|
||||
@@ -75,8 +77,22 @@ def start_prometheus_server():
|
||||
os._exit(1)
|
||||
|
||||
|
||||
def run_async_main():
|
||||
"""Run the async main function with proper event loop setup"""
|
||||
try:
|
||||
loop = asyncio.new_event_loop()
|
||||
asyncio.set_event_loop(loop)
|
||||
loop.run_until_complete(main())
|
||||
except KeyboardInterrupt:
|
||||
print("KeyboardInterrupt received in main thread.")
|
||||
exit_signal.set()
|
||||
finally:
|
||||
loop.close()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
signal.signal(signal.SIGINT, signal_handler)
|
||||
signal.signal(signal.SIGTERM, signal_handler)
|
||||
signal.signal(signal.SIGHUP, signal_handler)
|
||||
main()
|
||||
|
||||
run_async_main()
|
||||
|
||||
Reference in New Issue
Block a user