From bc750a566c7e76e1320603e6254f937d0947d69b Mon Sep 17 00:00:00 2001 From: Etienne Pallier Date: Mon, 7 Nov 2022 23:05:15 +0100 Subject: [PATCH] Allow commands to be sent manually to an agent even if in TEST mode, without disturbing it --- CHANGELOG | 3 +++ VERSION | 2 +- src/core/pyros_django/agent/Agent.py | 37 +++++++++++++++++++++++++------------ src/core/pyros_django/common/models.py | 7 ++++--- 4 files changed, 33 insertions(+), 16 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 30b46ab..a262888 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,6 @@ +07-11-2022 (EP): v0.6.7.4 + - Allow commands to be sent manually to an agent even if in TEST mode, without disturbing it + - Bugfix result = "0" (=false) => now shown 07-11-2022 (EP): v0.6.7.3 - Renamed Agent Status names diff --git a/VERSION b/VERSION index a3c2491..ab61db8 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.6.7.3 \ No newline at end of file +0.6.7.4 \ No newline at end of file diff --git a/src/core/pyros_django/agent/Agent.py b/src/core/pyros_django/agent/Agent.py index 5960ed3..5c9e6dc 100755 --- a/src/core/pyros_django/agent/Agent.py +++ b/src/core/pyros_django/agent/Agent.py @@ -55,6 +55,7 @@ import typing from django.utils import timezone from django.conf import settings as djangosettings +from django.db.models.query import QuerySet # Conseil sur le net: #https://stackoverflow.com/questions/16853649/how-to-execute-a-python-script-from-the-django-shell @@ -540,7 +541,7 @@ class Agent: ##_next_cmdts = None __agent_survey = None - __pending_commands = [] + __pending_commands = QuerySet # [] ''' _current_device_cmd = None @@ -1194,10 +1195,10 @@ class Agent: log.info('-'*6 + "CMD finished WITH EXCEPTION ") self.__process_exception(self.CCE) self.__CCE = None - print("my name is", self.__CC.sender, self.__class__.__name__) - if self.is_in_test_mode() and self.__CC.sender==self.__class__.__name__ : + # Check current cmd res and status ONLY if in test mode and cmd sent by myself + #if self.is_in_test_mode() and self.__CC.sender==self.__class__.__name__ : + if self.is_in_test_mode(): self.__TEST_check_current_cmd_res_and_status() - #self.CC = None def __get_received_priority_cmd_if_exists(self)->Optional[AgentCmd]: @@ -2018,6 +2019,9 @@ class Agent: ) ''' + def get_my_received_pending_commands(self) -> QuerySet: + return AgentCmd.get_pending_and_running_commands_for_agent(self.name) + def __get_next_received_cmd_if_exists(self) -> Optional[AgentCmd]: """ Return next pending command @@ -2035,7 +2039,7 @@ class Agent: # but there might be a risk # that a command status is modified while we are reading... with transaction.atomic(): - self.__pending_commands = AgentCmd.get_pending_and_running_commands_for_agent(self.name) + self.__pending_commands = self.get_my_received_pending_commands() commands = self.__pending_commands if not commands.exists(): log.info("") @@ -2740,10 +2744,11 @@ class Agent: while not DO_IT: DO_IT, cmd_full_name, validity, expected_res, expected_status = next(self.TEST_COMMANDS, (False,None,None,None,None)) print(DO_IT, cmd_full_name) + if cmd_full_name is None: return None #print(expected_final_status) #print(cmd_full_name, res_expected) #return cmd_name - if cmd_full_name is None: return None + #if cmd_full_name is None: return None # Remove excessive spaces => already done in AgentCmd.create() cmd_full_name = re.sub(r"\s+", " ", cmd_full_name).strip() if ' ' not in cmd_full_name: raise Exception('Command is malformed:', cmd_full_name) @@ -2789,8 +2794,9 @@ class Agent: cmdts = self.__TEST_get_next_command_to_send() # No more command to send (from simulator) => return and EXIT - if cmdts is None: - self.DO_MAIN_LOOP = False + if cmdts is None : + if not self.get_my_received_pending_commands().exists(): + self.DO_MAIN_LOOP = False return log.info("TTT") @@ -2931,7 +2937,7 @@ class Agent: cmd_num+=1 if cmd_num == cmd_searched_num: cmd_name = re.sub(r"\s+", " ", cmd_name).strip().split(' ')[1] - #print(cmd_name, self.CC.name) + ##print(cmd_name, self.CC.name) assert cmd_name == self.CC.name break i+=1 @@ -2939,7 +2945,7 @@ class Agent: #def __TEST_check_current_cmd_res_and_status(self, cmd:AgentCmd): - def __TEST_check_current_cmd_res_and_status(self): + def __TEST_check_current_cmd_res_and_status(self)->None: cmd = self.CC #if not self.is_in_test_mode(): return @@ -2952,10 +2958,17 @@ class Agent: log.debug("TTT CHECK TTT") # number of the current test command (finished) - self.__test_cmd_received_num+=1 + self.__test_cmd_received_num += 1 + + ##print(self.__test_cmd_received_num, cmd.name) + + #FIXME: do special check in this case + if cmd.is_agent_general_priority_cmd(): return + #print("test cmd num", self.__test_cmd_received_num, "for current cmd", cmd) # Format : (DO_IT, "self cmd_name cmd_args", timeout, "expected_result", expected_status), - _, _, _, test_cmd_sent_expected_res, test_cmd_sent_expected_status = self._TEST_get_sent_test_command_num(self.__test_cmd_received_num) + _, cmd_name, _, test_cmd_sent_expected_res, test_cmd_sent_expected_status = self._TEST_get_sent_test_command_num(self.__test_cmd_received_num) + ##print(self.__test_cmd_received_num, cmd.name, cmd_name) #if hasattr(self._cmdts,'expected_res'): ##if cmd.is_executed() and self._cmdts.expected_res: #if cmd.is_finished() and self._cmdts.expected_res is not None: diff --git a/src/core/pyros_django/common/models.py b/src/core/pyros_django/common/models.py index f6efdd6..27dd13b 100644 --- a/src/core/pyros_django/common/models.py +++ b/src/core/pyros_django/common/models.py @@ -11,7 +11,7 @@ from datetime import datetime, timedelta, date from dateutil.relativedelta import relativedelta import os import sys -from typing import Any, List, Tuple +from typing import Any, List, Tuple, Optional import re # Django imports @@ -868,7 +868,7 @@ class AgentCmd(models.Model): printd("") @classmethod - def get_pending_and_running_commands_for_agent(cls, agent_name): + def get_pending_and_running_commands_for_agent(cls, agent_name)->QuerySet: #print("peremption date", COMMAND_PEREMPTION_DATE_FROM_NOW) return cls.objects.filter( # only pending or running commands @@ -1215,7 +1215,8 @@ class AgentCmd(models.Model): #now_time = datetime.utcnow().astimezone() now_time = datetime.now(tz=timezone.utc) - if result: + # result can be "0" which is false... + if result is not None: self.set_result(result, False) ''' -- libgit2 0.21.2