From 8e4133709f0d0cc3d8a90546992779180032b68a Mon Sep 17 00:00:00 2001 From: cardosofede Date: Thu, 29 Aug 2024 21:32:31 -0300 Subject: [PATCH] (feat) add update bots on loop --- services/bots_orchestrator.py | 47 ++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/services/bots_orchestrator.py b/services/bots_orchestrator.py index 6c75db7..24ae656 100644 --- a/services/bots_orchestrator.py +++ b/services/bots_orchestrator.py @@ -79,29 +79,30 @@ def stop_update_active_bots_loop(self): self._update_bots_task.cancel() self._update_bots_task = None - async def update_active_bots(self, sleep_time=10): - active_hbot_containers = self.get_active_containers() - # Remove bots that are no longer active - for bot in list(self.active_bots): - if bot not in active_hbot_containers: - del self.active_bots[bot] - - # Add new bots or update existing ones - for bot in active_hbot_containers: - if bot not in self.active_bots: - hbot_listener = HummingbotPerformanceListener(host=self.broker_host, port=self.broker_port, - username=self.broker_username, - password=self.broker_password, - bot_id=bot) - hbot_listener.start() - self.active_bots[bot] = { - "bot_name": bot, - "broker_client": BotCommands(host=self.broker_host, port=self.broker_port, - username=self.broker_username, password=self.broker_password, - bot_id=bot), - "broker_listener": hbot_listener, - } - await asyncio.sleep(sleep_time) + async def update_active_bots(self, sleep_time=1): + while True: + active_hbot_containers = self.get_active_containers() + # Remove bots that are no longer active + for bot in list(self.active_bots): + if bot not in active_hbot_containers: + del self.active_bots[bot] + + # Add new bots or update existing ones + for bot in active_hbot_containers: + if bot not in self.active_bots: + hbot_listener = HummingbotPerformanceListener(host=self.broker_host, port=self.broker_port, + username=self.broker_username, + password=self.broker_password, + bot_id=bot) + hbot_listener.start() + self.active_bots[bot] = { + "bot_name": bot, + "broker_client": BotCommands(host=self.broker_host, port=self.broker_port, + username=self.broker_username, password=self.broker_password, + bot_id=bot), + "broker_listener": hbot_listener, + } + await asyncio.sleep(sleep_time) # Interact with a specific bot def start_bot(self, bot_name, **kwargs):