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,3 +1,4 @@
|
||||
import asyncio
|
||||
import traceback
|
||||
from typing import Dict, List
|
||||
from copy import deepcopy
|
||||
@@ -57,7 +58,7 @@ class IngestorManager(BaseActivity):
|
||||
notification_handler=notification_handler,
|
||||
set_error_counter=True)
|
||||
|
||||
def initialize_opc_from_config(self, server_config: dict) -> OpcManager | None:
|
||||
async def initialize_opc_from_config(self, server_config: dict) -> OpcManager | None:
|
||||
"""
|
||||
Initializes an OPC Manager instance using the provided server configuration.
|
||||
Args:
|
||||
@@ -93,7 +94,7 @@ class IngestorManager(BaseActivity):
|
||||
)
|
||||
|
||||
manager.config = server_config
|
||||
manager.connect()
|
||||
await manager.connect()
|
||||
except Exception as e:
|
||||
|
||||
trace = traceback.format_exc()
|
||||
@@ -111,15 +112,15 @@ class IngestorManager(BaseActivity):
|
||||
|
||||
return manager
|
||||
|
||||
def shutdown(self):
|
||||
async def shutdown(self):
|
||||
for _server_name, server in self.opc_managers.items():
|
||||
server.disconnect()
|
||||
await server.disconnect()
|
||||
self.data_manager.shutdown()
|
||||
|
||||
def __del__(self):
|
||||
self.shutdown()
|
||||
asyncio.run(self.shutdown())
|
||||
|
||||
def update_opc_servers(self):
|
||||
async def update_opc_servers(self):
|
||||
"""
|
||||
Updates the OPC (OLE for Process Control) server connections managed by the ingestor.
|
||||
This method ensures that the OPC servers defined in `self.managed_tags` are properly
|
||||
@@ -156,7 +157,7 @@ class IngestorManager(BaseActivity):
|
||||
self.logger.info(
|
||||
f"Initializing OPC manager for server {server}"
|
||||
)
|
||||
server_instance = self.initialize_opc_from_config(
|
||||
server_instance = await self.initialize_opc_from_config(
|
||||
server_config
|
||||
)
|
||||
|
||||
@@ -166,7 +167,7 @@ class IngestorManager(BaseActivity):
|
||||
)
|
||||
server_instance.disconnect()
|
||||
del self.opc_managers[server]
|
||||
server_instance = self.initialize_opc_from_config(
|
||||
server_instance = await self.initialize_opc_from_config(
|
||||
server_config
|
||||
)
|
||||
else:
|
||||
@@ -191,7 +192,7 @@ class IngestorManager(BaseActivity):
|
||||
f"Server {server} not found in managed tags. "
|
||||
f"Desconnecting from server."
|
||||
)
|
||||
self.opc_managers[server].disconnect()
|
||||
await self.opc_managers[server].disconnect()
|
||||
self.opc_managers.pop(server, None)
|
||||
|
||||
metrics.OPC_MANAGERS_ACTIVE.labels(
|
||||
@@ -312,7 +313,7 @@ class IngestorManager(BaseActivity):
|
||||
pod_id=self.pod_id).set(len(self.managed_tags))
|
||||
return acquired
|
||||
|
||||
def unsubscribe_slot(self, slot: str):
|
||||
async def unsubscribe_slot(self, slot: str):
|
||||
"""
|
||||
Unsubscribes a specific slot from all associated OPC servers.
|
||||
Args:
|
||||
@@ -323,7 +324,7 @@ class IngestorManager(BaseActivity):
|
||||
|
||||
for server in self.managed_tags[slot].keys():
|
||||
if server in self.opc_managers:
|
||||
self.opc_managers[server].unsubscribe(slot)
|
||||
await self.opc_managers[server].unsubscribe(slot)
|
||||
|
||||
def update_slot_config(self):
|
||||
"""
|
||||
@@ -378,7 +379,7 @@ class IngestorManager(BaseActivity):
|
||||
self.resource_manager.drop_tag_lease(lease_id)
|
||||
metrics.SLOTS_RELEASED.labels(pod_id=self.pod_id).inc()
|
||||
|
||||
def manage_server(self, slot: str, server: str, server_config: dict, tags: dict) -> int:
|
||||
async def manage_server(self, slot: str, server: str, server_config: dict, tags: dict) -> int:
|
||||
"""
|
||||
Manages the subscription of tags to a specified OPC server and slot.
|
||||
This method ensures that the specified server and slot have an active subscription
|
||||
@@ -416,7 +417,7 @@ class IngestorManager(BaseActivity):
|
||||
return 1
|
||||
if slot not in self.opc_managers[server].subscriptions:
|
||||
try:
|
||||
self.opc_managers[server].create_subscription(
|
||||
await self.opc_managers[server].create_subscription(
|
||||
slot
|
||||
)
|
||||
except Exception as e:
|
||||
@@ -428,7 +429,7 @@ class IngestorManager(BaseActivity):
|
||||
self.logger.info(
|
||||
tags_to_sub
|
||||
)
|
||||
self.opc_managers[server].subscribe(
|
||||
await self.opc_managers[server].subscribe(
|
||||
slot, deepcopy(tags_to_sub), self.poll_interval
|
||||
)
|
||||
self.logger.info(
|
||||
@@ -452,11 +453,11 @@ class IngestorManager(BaseActivity):
|
||||
"Removing subscription from server "
|
||||
f"{server} for slot {slot}"
|
||||
)
|
||||
self.opc_managers[server].unsubscribe(slot)
|
||||
await self.opc_managers[server].unsubscribe(slot)
|
||||
return 2
|
||||
return 0
|
||||
|
||||
def subscribe_to_tags(self, tags: Dict) -> None:
|
||||
async def subscribe_to_tags(self, tags: Dict) -> None:
|
||||
"""
|
||||
Subscribes to a set of tags and manages their configurations.
|
||||
This method processes a dictionary of tags, iterating through each slot and server
|
||||
@@ -478,7 +479,7 @@ class IngestorManager(BaseActivity):
|
||||
self.logger.info(tags)
|
||||
for slot, slot_config in tags.items():
|
||||
for server, server_config in slot_config.items():
|
||||
response = self.manage_server(
|
||||
response = await self.manage_server(
|
||||
slot, server, server_config, tags
|
||||
)
|
||||
if response == 2:
|
||||
|
||||
Reference in New Issue
Block a user