diff --git a/ingestor/managers/opc_manager.py b/ingestor/managers/opc_manager.py index db77e35..0ad077d 100644 --- a/ingestor/managers/opc_manager.py +++ b/ingestor/managers/opc_manager.py @@ -1,7 +1,7 @@ import asyncio import json -from pathlib import Path import traceback +from pathlib import Path from asyncua import Client from asyncua.crypto.security_policies import SecurityPolicyBasic256 @@ -86,7 +86,6 @@ class OpcManager(BaseActivity): self.data_manager = data_manager self.metadata = metadata - BaseActivity.__init__( self, logger=logger, notification_handler=notification_handler, set_error_counter=True ) @@ -209,7 +208,7 @@ class OpcManager(BaseActivity): pod_id=self.pod_id, server_name=self.name, server_url=self.url ).set(1) self.logger.info(f'Connection to {self.name} successful.') - except Exception as e: + except Exception: await self.disconnect() raise @@ -239,7 +238,8 @@ class OpcManager(BaseActivity): raise ValueError('Client not connected. Call connect first.') try: self.subscriptions[name] = await self.client.create_subscription( - self.subscription_period_ms, self) + self.subscription_period_ms, self + ) self.logger.info(f'Subscription {name} created on {self.name}.') metrics.OPC_SUBSCRIPTIONS_CREATED.labels( pod_id=self.pod_id, server_name=self.name, slot_name=name @@ -332,16 +332,20 @@ class OpcManager(BaseActivity): error_stack = [] for i in range(5): try: - self.logger.info(f'Disconnecting from OPC UA server, attempt {i+1} of 5') + self.logger.info(f'Disconnecting from OPC UA server, attempt {i + 1} of 5') await self.client.disconnect() return [] except Exception as e: - self.logger.error(f'Failed to disconnect from OPC UA serve in attempt {i+1} of 5: {e}') - error_stack.append({ - 'attempt': i+1, - 'error': str(e), - 'traceback': traceback.format_exc(), - }) + self.logger.error( + f'Failed to disconnect from OPC UA serve in attempt {i + 1} of 5: {e}' + ) + error_stack.append( + { + 'attempt': i + 1, + 'error': str(e), + 'traceback': traceback.format_exc(), + } + ) await asyncio.sleep(0.1 * i) return error_stack @@ -375,7 +379,6 @@ class OpcManager(BaseActivity): except Exception as sub_error: self.logger.error(f'Failed to clean up subscription: {sub_error}') - errors = await self.disconnection_fallback() if errors: diff --git a/tests/unit/managers/test_opc_manager.py b/tests/unit/managers/test_opc_manager.py index fe72f46..f440e43 100644 --- a/tests/unit/managers/test_opc_manager.py +++ b/tests/unit/managers/test_opc_manager.py @@ -191,9 +191,10 @@ async def test_connect_exception_handling_and_metrics( with pytest.raises(Exception, match=simulated_error_message): await opc_manager_instance.connect() - + opc_manager_instance.disconnect.assert_called_once() + @mark.asyncio async def test_create_subscription_no_client(raw_opc_manager): try: @@ -209,7 +210,8 @@ async def test_create_subscription_success_has_period(opc_manager): await opc_manager.create_subscription('sub1') opc_manager.client.create_subscription.assert_called_once_with( - opc_manager.subscription_period_ms, opc_manager) + opc_manager.subscription_period_ms, opc_manager + ) assert opc_manager.subscriptions['sub1'] is not None @@ -218,7 +220,8 @@ async def test_create_subscription_success_no_period(opc_manager): await opc_manager.create_subscription('sub1') opc_manager.client.create_subscription.assert_called_once_with( - opc_manager.subscription_period_ms, opc_manager) + opc_manager.subscription_period_ms, opc_manager + ) assert opc_manager.subscriptions['sub1'] is not None @@ -302,7 +305,6 @@ async def test_unsubscribe_success(opc_manager_subscribed): assert opc_manager_subscribed.subscriptions.get('sub1') is None - @mark.asyncio async def test_disconnection_fallback_success(opc_manager): opc_manager.client = AsyncMock() @@ -310,6 +312,7 @@ async def test_disconnection_fallback_success(opc_manager): result = await opc_manager.disconnection_fallback() assert result == [] + @mark.asyncio async def test_disconnection_fallback_fail(opc_manager): opc_manager.client = AsyncMock() @@ -317,10 +320,11 @@ async def test_disconnection_fallback_fail(opc_manager): result = await opc_manager.disconnection_fallback() assert result == [ {'attempt': 1, 'error': 'Test error', 'traceback': ANY}, - {'attempt': 2, 'error': 'Test error', 'traceback': ANY}, + {'attempt': 2, 'error': 'Test error', 'traceback': ANY}, {'attempt': 3, 'error': 'Test error', 'traceback': ANY}, {'attempt': 4, 'error': 'Test error', 'traceback': ANY}, - {'attempt': 5, 'error': 'Test error', 'traceback': ANY}, ] + {'attempt': 5, 'error': 'Test error', 'traceback': ANY}, + ] assert opc_manager.client.disconnect.call_count == 5 @@ -373,7 +377,6 @@ async def test_disconnect_error(opc_manager_subscribed): opc_manager_subscribed.client = None - @patch('ingestor.managers.opc_manager.metrics') @mark.asyncio async def test_disconnect_metrics_on_successful_path(mock_metrics_module, raw_opc_manager): diff --git a/values.yaml b/values.yaml index c9a926d..5d22543 100644 --- a/values.yaml +++ b/values.yaml @@ -11,7 +11,7 @@ image: # This sets the pull policy for images. pullPolicy: Always # Overrides the image tag whose default is the chart appVersion. - tag: "0.4.5" + tag: "0.4.9" # This is for the secrets for pulling an image from a private repository more information can be found here: https://kubernetes.io/docs/tasks/configure-pod-container/pull-image-private-registry/ imagePullSecrets: