Commit bc750a566c7e76e1320603e6254f937d0947d69b
1 parent
02c12676
Exists in
dev
Allow commands to be sent manually to an agent even if in TEST mode, without disturbing it
Showing
4 changed files
with
33 additions
and
16 deletions
Show diff stats
CHANGELOG
VERSION
src/core/pyros_django/agent/Agent.py
... | ... | @@ -55,6 +55,7 @@ import typing |
55 | 55 | |
56 | 56 | from django.utils import timezone |
57 | 57 | from django.conf import settings as djangosettings |
58 | +from django.db.models.query import QuerySet | |
58 | 59 | |
59 | 60 | # Conseil sur le net: |
60 | 61 | #https://stackoverflow.com/questions/16853649/how-to-execute-a-python-script-from-the-django-shell |
... | ... | @@ -540,7 +541,7 @@ class Agent: |
540 | 541 | ##_next_cmdts = None |
541 | 542 | |
542 | 543 | __agent_survey = None |
543 | - __pending_commands = [] | |
544 | + __pending_commands = QuerySet # [] | |
544 | 545 | |
545 | 546 | ''' |
546 | 547 | _current_device_cmd = None |
... | ... | @@ -1194,10 +1195,10 @@ class Agent: |
1194 | 1195 | log.info('-'*6 + "CMD finished WITH EXCEPTION ") |
1195 | 1196 | self.__process_exception(self.CCE) |
1196 | 1197 | self.__CCE = None |
1197 | - print("my name is", self.__CC.sender, self.__class__.__name__) | |
1198 | - if self.is_in_test_mode() and self.__CC.sender==self.__class__.__name__ : | |
1198 | + # Check current cmd res and status ONLY if in test mode and cmd sent by myself | |
1199 | + #if self.is_in_test_mode() and self.__CC.sender==self.__class__.__name__ : | |
1200 | + if self.is_in_test_mode(): | |
1199 | 1201 | self.__TEST_check_current_cmd_res_and_status() |
1200 | - #self.CC = None | |
1201 | 1202 | |
1202 | 1203 | |
1203 | 1204 | def __get_received_priority_cmd_if_exists(self)->Optional[AgentCmd]: |
... | ... | @@ -2018,6 +2019,9 @@ class Agent: |
2018 | 2019 | ) |
2019 | 2020 | ''' |
2020 | 2021 | |
2022 | + def get_my_received_pending_commands(self) -> QuerySet: | |
2023 | + return AgentCmd.get_pending_and_running_commands_for_agent(self.name) | |
2024 | + | |
2021 | 2025 | def __get_next_received_cmd_if_exists(self) -> Optional[AgentCmd]: |
2022 | 2026 | """ |
2023 | 2027 | Return next pending command |
... | ... | @@ -2035,7 +2039,7 @@ class Agent: |
2035 | 2039 | # but there might be a risk |
2036 | 2040 | # that a command status is modified while we are reading... |
2037 | 2041 | with transaction.atomic(): |
2038 | - self.__pending_commands = AgentCmd.get_pending_and_running_commands_for_agent(self.name) | |
2042 | + self.__pending_commands = self.get_my_received_pending_commands() | |
2039 | 2043 | commands = self.__pending_commands |
2040 | 2044 | if not commands.exists(): |
2041 | 2045 | log.info("<None>") |
... | ... | @@ -2740,10 +2744,11 @@ class Agent: |
2740 | 2744 | while not DO_IT: |
2741 | 2745 | DO_IT, cmd_full_name, validity, expected_res, expected_status = next(self.TEST_COMMANDS, (False,None,None,None,None)) |
2742 | 2746 | print(DO_IT, cmd_full_name) |
2747 | + if cmd_full_name is None: return None | |
2743 | 2748 | #print(expected_final_status) |
2744 | 2749 | #print(cmd_full_name, res_expected) |
2745 | 2750 | #return cmd_name |
2746 | - if cmd_full_name is None: return None | |
2751 | + #if cmd_full_name is None: return None | |
2747 | 2752 | # Remove excessive spaces => already done in AgentCmd.create() |
2748 | 2753 | cmd_full_name = re.sub(r"\s+", " ", cmd_full_name).strip() |
2749 | 2754 | if ' ' not in cmd_full_name: raise Exception('Command is malformed:', cmd_full_name) |
... | ... | @@ -2789,8 +2794,9 @@ class Agent: |
2789 | 2794 | |
2790 | 2795 | cmdts = self.__TEST_get_next_command_to_send() |
2791 | 2796 | # No more command to send (from simulator) => return and EXIT |
2792 | - if cmdts is None: | |
2793 | - self.DO_MAIN_LOOP = False | |
2797 | + if cmdts is None : | |
2798 | + if not self.get_my_received_pending_commands().exists(): | |
2799 | + self.DO_MAIN_LOOP = False | |
2794 | 2800 | return |
2795 | 2801 | |
2796 | 2802 | log.info("TTT") |
... | ... | @@ -2931,7 +2937,7 @@ class Agent: |
2931 | 2937 | cmd_num+=1 |
2932 | 2938 | if cmd_num == cmd_searched_num: |
2933 | 2939 | cmd_name = re.sub(r"\s+", " ", cmd_name).strip().split(' ')[1] |
2934 | - #print(cmd_name, self.CC.name) | |
2940 | + ##print(cmd_name, self.CC.name) | |
2935 | 2941 | assert cmd_name == self.CC.name |
2936 | 2942 | break |
2937 | 2943 | i+=1 |
... | ... | @@ -2939,7 +2945,7 @@ class Agent: |
2939 | 2945 | |
2940 | 2946 | |
2941 | 2947 | #def __TEST_check_current_cmd_res_and_status(self, cmd:AgentCmd): |
2942 | - def __TEST_check_current_cmd_res_and_status(self): | |
2948 | + def __TEST_check_current_cmd_res_and_status(self)->None: | |
2943 | 2949 | cmd = self.CC |
2944 | 2950 | |
2945 | 2951 | #if not self.is_in_test_mode(): return |
... | ... | @@ -2952,10 +2958,17 @@ class Agent: |
2952 | 2958 | log.debug("TTT CHECK TTT") |
2953 | 2959 | |
2954 | 2960 | # number of the current test command (finished) |
2955 | - self.__test_cmd_received_num+=1 | |
2961 | + self.__test_cmd_received_num += 1 | |
2962 | + | |
2963 | + ##print(self.__test_cmd_received_num, cmd.name) | |
2964 | + | |
2965 | + #FIXME: do special check in this case | |
2966 | + if cmd.is_agent_general_priority_cmd(): return | |
2967 | + | |
2956 | 2968 | #print("test cmd num", self.__test_cmd_received_num, "for current cmd", cmd) |
2957 | 2969 | # Format : (DO_IT, "self cmd_name cmd_args", timeout, "expected_result", expected_status), |
2958 | - _, _, _, test_cmd_sent_expected_res, test_cmd_sent_expected_status = self._TEST_get_sent_test_command_num(self.__test_cmd_received_num) | |
2970 | + _, cmd_name, _, test_cmd_sent_expected_res, test_cmd_sent_expected_status = self._TEST_get_sent_test_command_num(self.__test_cmd_received_num) | |
2971 | + ##print(self.__test_cmd_received_num, cmd.name, cmd_name) | |
2959 | 2972 | #if hasattr(self._cmdts,'expected_res'): |
2960 | 2973 | ##if cmd.is_executed() and self._cmdts.expected_res: |
2961 | 2974 | #if cmd.is_finished() and self._cmdts.expected_res is not None: | ... | ... |
src/core/pyros_django/common/models.py
... | ... | @@ -11,7 +11,7 @@ from datetime import datetime, timedelta, date |
11 | 11 | from dateutil.relativedelta import relativedelta |
12 | 12 | import os |
13 | 13 | import sys |
14 | -from typing import Any, List, Tuple | |
14 | +from typing import Any, List, Tuple, Optional | |
15 | 15 | import re |
16 | 16 | |
17 | 17 | # Django imports |
... | ... | @@ -868,7 +868,7 @@ class AgentCmd(models.Model): |
868 | 868 | printd("<None>") |
869 | 869 | |
870 | 870 | @classmethod |
871 | - def get_pending_and_running_commands_for_agent(cls, agent_name): | |
871 | + def get_pending_and_running_commands_for_agent(cls, agent_name)->QuerySet: | |
872 | 872 | #print("peremption date", COMMAND_PEREMPTION_DATE_FROM_NOW) |
873 | 873 | return cls.objects.filter( |
874 | 874 | # only pending or running commands |
... | ... | @@ -1215,7 +1215,8 @@ class AgentCmd(models.Model): |
1215 | 1215 | #now_time = datetime.utcnow().astimezone() |
1216 | 1216 | now_time = datetime.now(tz=timezone.utc) |
1217 | 1217 | |
1218 | - if result: | |
1218 | + # result can be "0" which is false... | |
1219 | + if result is not None: | |
1219 | 1220 | self.set_result(result, False) |
1220 | 1221 | |
1221 | 1222 | ''' | ... | ... |