Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed TBClient reconnecting #1337

Merged
merged 2 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion thingsboard_gateway/gateway/tb_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,6 @@ def connect(self, min_reconnect_delay=10):
self.__stopped = False
self.__min_reconnect_delay = min_reconnect_delay

def run(self):
keep_alive = self.__config.get("keep_alive", 120)
try:
while not self.client.is_connected() and not self.__stopped:
Expand All @@ -274,6 +273,7 @@ def run(self):
self.__logger.exception(e)
sleep(10)

def run(self):
while not self.__stopped:
try:
if not self.__stopped:
Expand All @@ -290,3 +290,10 @@ def get_config_folder_path(self):

def register_service_subscription_callback(self, subscribe_to_required_topics):
self.__service_subscription_callbacks.append(subscribe_to_required_topics)

@property
def config(self):
return self.__config

def is_stopped(self):
return self.__stopped
4 changes: 2 additions & 2 deletions thingsboard_gateway/gateway/tb_gateway_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,10 +449,10 @@ def _watchers(self):
while not self.stopped:
cur_time = time() * 1000

if not self.tb_client.is_connected() and self.__subscribed_to_rpc_topics:
if not self.tb_client.client.is_connected() and self.__subscribed_to_rpc_topics:
self.__subscribed_to_rpc_topics = False

if self.tb_client.is_connected() and not self.__subscribed_to_rpc_topics:
if self.tb_client.is_connected() and not self.tb_client.is_stopped() and not self.__subscribed_to_rpc_topics:
for device in list(self.__saved_devices.keys()):
self.add_device(device,
{CONNECTOR_PARAMETER: self.__saved_devices[device][CONNECTOR_PARAMETER]},
Expand Down
44 changes: 29 additions & 15 deletions thingsboard_gateway/tb_utility/tb_gateway_remote_configurator.py
Original file line number Diff line number Diff line change
Expand Up @@ -576,26 +576,40 @@ def _handle_remote_logging_level_update(self, config):

# HANDLERS SUPPORT METHODS -----------------------------------------------------------------------------------------
def _apply_connection_config(self, config) -> bool:
apply_start = time() * 1000
old_tb_client = self._gateway.tb_client
old_tb_client_config_path = self._gateway.tb_client.get_config_folder_path()
old_tb_client_config = self._gateway.tb_client.config
connection_logger = getLogger('tb_connection')
try:
old_tb_client.disconnect()
self._gateway.tb_client.disconnect()
self._gateway.tb_client.stop()

connection_logger = getLogger('tb_connection')
new_tb_client = TBClient(config, old_tb_client.get_config_folder_path(), connection_logger)
while self._gateway.tb_client.client.is_connected():
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This loop can cause the gateway stuck, probably, it will be better to add check is the gateway stopped or no, to avoid hold before stop.

sleep(1)

apply_start = time()

connection_state = False
use_new_config = True
while not connection_state:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add check for gateway status - is it stopped or no.

for client in (new_tb_client, old_tb_client):
client.connect()
while time() * 1000 - apply_start >= 1000 and not connection_state:
connection_state = client.is_connected()
sleep(.1)

if connection_state:
self._gateway.tb_client = client
self._gateway.subscribe_to_required_topics()
return True
self._gateway.__subscribed_to_rpc_topics = False
new_tb_client = TBClient(config if use_new_config else old_tb_client_config, old_tb_client_config_path, connection_logger)
new_tb_client.connect()
while time() - apply_start <= 30 and not connection_state:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check is gateway running will be nice here as well.

connection_state = new_tb_client.is_connected()
sleep(.1)

if connection_state:
self._gateway.tb_client = new_tb_client
self._gateway.__subscribed_to_rpc_topics = False
self._gateway.subscribe_to_required_topics()
return True
else:
new_tb_client.disconnect()
new_tb_client.stop()
while new_tb_client.client.is_connected():
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a check is gateway running or stopped.

sleep(1)
apply_start = time() * 1000
use_new_config = not use_new_config
except Exception as e:
LOG.exception(e)
self._revert_connection()
Expand Down
Loading