diff --git a/privatedev/plugin/agent/AgentBasic.py b/privatedev/plugin/agent/AgentBasic.py index 2ebec3b..8a3b30d 100755 --- a/privatedev/plugin/agent/AgentBasic.py +++ b/privatedev/plugin/agent/AgentBasic.py @@ -46,22 +46,22 @@ class AgentBasic(Agent): # Normal Commands #("set_specific2", 5, 0), - ("do_specific10", 10, Agent.EXEC_MODE.SEQUENTIAL), - ("do_specific30", 3, Agent.EXEC_MODE.SEQUENTIAL), - ("do_cmd_with_long_exec_time", 3, Agent.EXEC_MODE.SEQUENTIAL), + ("do_specific10", 1, Agent.EXEC_MODE.SEQUENTIAL), + ("do_specific30", 0, Agent.EXEC_MODE.SEQUENTIAL), + ("do_cmd_with_long_exec_time", 50, Agent.EXEC_MODE.THREAD), ] # Deactivate some tests, so that test scenario runs faster during DEV # on DEV - #COMMIT_ONLY = False + COMMIT_ONLY = False # on commit only - COMMIT_ONLY = True + #COMMIT_ONLY = True # @override _TEST_COMMANDS_LIST = [ - # Format : (DO_IT, "self cmd_name cmd_args", timeout, "expected_result", expected_status), + # Format : (DO_IT, "self cmd_name cmd_args", validity, "expected_result", expected_status), #("self do_stop now", 200, '', Agent.CMD_STATUS.CMD_EXECUTED), @@ -71,14 +71,16 @@ class AgentBasic(Agent): #("self do_stop asap", 200, 'STOPPING asap', Agent.CMD_STATUS.CMD_EXECUTED), # get_specific_cmds - (True, " self get_specific_cmds ", 200, + (True, " self get_all_cmds ", 100, None, #'do_specific10(arg1:int,arg2:int,arg3:float,arg4:str,arg5:typing.Tuple[int, str, int],arg6:typing.List[int]);do_specific30();do_cmd_raising_some_exception();do_cmd_unimplemented(U)', Agent.CMD_STATUS.CMD_EXECUTED ), - # do_restart (COMMIT_ONLY, "self do_restart asap", 200, 'RESTARTING asap', Agent.CMD_STATUS.CMD_EXECUTED), + + # do_restart + (True, "self do_cmd_with_long_exec_time", 200, None, Agent.CMD_STATUS.CMD_EXECUTED), #("self do_stop", 200, 'STOPPING asap', Agent.CMD_STATUS.CMD_EXECUTED), # ---------------------------------------------------- @@ -371,8 +373,39 @@ class AgentBasic(Agent): # Long time execution command def do_cmd_with_long_exec_time(self): - self.waitfor(100) - return "took 100s to execute" + nbsec=8 + + res = f"1 - now sleeping {nbsec} sec" + self.CC.set_result(res, True) + #self.CC.get_updated_result() + while self.CC.get_updated_result() != res: self.sleep(1) + self.sleep(nbsec) + + res = f"2 - now sleeping {nbsec} sec" + self.CC.set_result(res, True) + #self.CC.get_updated_result() + while self.CC.get_updated_result() != res: self.sleep(1) + self.sleep(nbsec) + + res = f"3 - now sleeping {nbsec} sec" + self.CC.set_result(res, True) + #self.CC.get_updated_result() + while self.CC.get_updated_result() != res: self.sleep(1) + self.sleep(nbsec) + + res = f"4 - now sleeping {nbsec} sec" + self.CC.set_result(res, True) + #self.CC.get_updated_result() + while self.CC.get_updated_result() != res: self.sleep(1) + self.sleep(nbsec) + + res = f"5 - now sleeping {nbsec} sec" + self.CC.set_result(res, True) + #self.CC.get_updated_result() + while self.CC.get_updated_result() != res: self.sleep(1) + self.sleep(nbsec) + + return f"should have taken {5*nbsec}s to execute" def do_specific10(self, arg1:int, diff --git a/src/core/pyros_django/agent/Agent.py b/src/core/pyros_django/agent/Agent.py index d9d3cca..ce4a990 100755 --- a/src/core/pyros_django/agent/Agent.py +++ b/src/core/pyros_django/agent/Agent.py @@ -327,6 +327,7 @@ class Agent: #_AGENT_SPECIFIC_COMMANDS: List[ Tuple[str, int, int] ] = [ _AGENT_SPECIFIC_COMMANDS: List[ Tuple[str, int, EXEC_MODE] ] = [ # Format : (“cmd_name”, timeout, exec_mode) + ("do_specific1", 10, EXEC_MODE.SEQUENTIAL), #("set_specific2", 5, 0), ("do_specific3", 3, EXEC_MODE.THREAD), @@ -368,7 +369,7 @@ class Agent: # - cmd : the command name # - args : (optional) the list of command arguments, separated by blanks : arg1 arg2 arg3 ... # - # - validity : the command is valid for this duration, afterwards you can forget it + # - validity : the command is valid for this duration, afterwards you can skip it # # - expected_res : the expected result (set to None if not to be tested) # @@ -381,9 +382,10 @@ class Agent: # - "self do_restart_loop" => means to send the command "do_restart_loop" to MYSELF (no args) # #_TEST_COMMANDS_LIST: List[ Tuple[ bool, str, int, Union[str,None], Union[int,None] ] ] = [ - _TEST_COMMANDS_LIST: List[ Tuple[ bool, str, int, Optional[str], Optional[int]] ] = [ + ##_TEST_COMMANDS_LIST: List[ Tuple[ bool, str, int, Optional[str], AgentCmd.CMD_STATUS_CODES ] ] = [ + _TEST_COMMANDS_LIST: List[ Tuple[ bool, str, Optional[int], Optional[str], Optional[int]] ] = [ + # Format : (DO_IT, "self cmd_name cmd_args", validity, "expected_result", expected_status), - # Format : (DO_IT, "self cmd_name cmd_args", timeout, "expected_result", expected_status), #("self do_stop now", 200, '15.5', None), (True, "self do_specific1 1 2 3.5 titi (3,'titi',5) [1,3,5,7,9]", 200, '15.5', None), @@ -1127,7 +1129,7 @@ class Agent: ###cmd = self.__start_next_received_cmd_if_possible_and_exists() # Better : cmd = self.__get_next_received_cmd_if_possible_and_exists() - if cmd: + if cmd: self.__CC_prev = self.__CC # This way, started commands will be able to access the current command via self.__CC self.__CC = cmd @@ -1169,7 +1171,9 @@ class Agent: def __get_next_received_cmd_if_possible_and_exists(self)->Optional[AgentCmd]: + self.__set_and_log_status(self.AGT_STATUS.IN_MAIN_LOOP_GET_NEXT_CMD) cmd = None + # GET NEXT COMMAND if exists and if current cmd is finished #if self.__CC is None or self.__CC.is_finished() or self.__priority_cmd_received(): CMD_PRIO = self.__get_received_priority_cmd_if_exists() @@ -1189,10 +1193,12 @@ class Agent: log.info("RUNNING PRIORITY COMMAND:") log.info(str(CMD_PRIO)) cmd = CMD_PRIO if CMD_PRIO else self.__get_next_received_cmd_if_exists() + return cmd def __start_next_received_cmd(self, cmd:AgentCmd)->None: + cmd.set_read_time() print() print() log.info("*"*10 + " NEXT CMD RECEIVED PROCESSING (START) " + "*"*10 + '\n') @@ -1427,7 +1433,7 @@ class Agent: def __start_cmd(self, cmd:AgentCmd)->None: ''' Processing the next pending command (self.__CC) ''' #cmd = self.__CC - self.__set_and_log_status(self.AGT_STATUS.IN_MAIN_LOOP_GET_NEXT_CMD) + #self.__set_and_log_status(self.AGT_STATUS.IN_MAIN_LOOP_GET_NEXT_CMD) #print() #print() @@ -1640,11 +1646,11 @@ class Agent: old_commands.delete() """ - def sleep(self, nbsec): + def sleep(self, nbsec:float): log.info(f"Now, waiting (sleeping) for {nbsec} second(s)...") time.sleep(nbsec) # alias to sleep - def waitfor(self, nbsec:float=2.0): + def waitfor(self, nbsec:float): ''' # thread if self._current_device_cmd_thread and self.RUN_IN_THREAD: @@ -1661,7 +1667,7 @@ class Agent: # MODE & STATUS # - def get_state(self): + def get_state(self)->str: return f"MODE is {self.__get_mode()} ; STATUS is {self.get_status()} ; ITERATION #{self.__iter_num}" def show_state(self): @@ -1674,20 +1680,21 @@ class Agent: # # - GET - def get_status(self): + def get_status(self)->AgentSurvey.STATUS_CHOICES: return self.__status # - SET # Private because automatic, must not be changed, depends only on the step the Agent is currently running, used only by Agent (superclass) - def __set_status(self, status:str): + def __set_status(self, status:AgentSurvey.STATUS_CHOICES): #self.printd(f"[{status}] (switching from status {self.__status})") log.debug(f"[{status}]") self.__status = status return False # Private because automatic, must not be changed, used only by Agent (superclass) - def __set_and_log_status(self, status:str): + ##def __set_and_log_status(self, status:str): + def __set_and_log_status(self, status:AgentSurvey.STATUS_CHOICES): self.__set_status(status) self.__log_agent_state() @@ -1697,12 +1704,12 @@ class Agent: # # - GET - def __get_mode(self): + def __get_mode(self)->AgentSurvey.MODE_CHOICES: return self.__mode # Test mode - def IS_MODE_IDLE(self): return self.__get_mode() == AgentSurvey.MODE_CHOICES.IDLE - def IS_MODE_ROUTINE(self): return self.__get_mode() == AgentSurvey.MODE_CHOICES.ROUTINE - def IS_MODE_ATTENTIVE(self): return self.__get_mode() == AgentSurvey.MODE_CHOICES.ATTENTIVE + def IS_MODE_IDLE(self)->bool: return self.__get_mode() == AgentSurvey.MODE_CHOICES.IDLE + def IS_MODE_ROUTINE(self)->bool: return self.__get_mode() == AgentSurvey.MODE_CHOICES.ROUTINE + def IS_MODE_ATTENTIVE(self)->bool: return self.__get_mode() == AgentSurvey.MODE_CHOICES.ATTENTIVE # @deprecated def IS_IDLE(self): return self.IS_MODE_IDLE() # @deprecated @@ -1743,7 +1750,7 @@ class Agent: """ def suspend(self): """ - TODO: + FIXME: Mode IDLE (doit rester à l'écoute d'un resume, et doit continuer à alimenter les tables pour informer de son état via tables agents_logs, et lire table agents_command pour reprendre via resume, @@ -1754,6 +1761,7 @@ class Agent: def resume(self): """ + FIXME: Quit suspend() mode """ self._set_active() @@ -2105,7 +2113,7 @@ class Agent: # so return it for execution #self.printd(f"Got command {cmd.name} sent by agent {cmd.sender} at {cmd.sender_deposit_time}") #self.printd(f"Starting processing of this command") - next_cmd.set_read_time() + ##next_cmd.set_read_time() return next_cmd @@ -2785,7 +2793,7 @@ class Agent: # To be overriden by subclass (AgentDevice...) # @abstract - def is_device_level_cmd(self, cmd): + def is_device_level_cmd(self, cmd)->bool: return False ''' @@ -2802,7 +2810,7 @@ class Agent: ''' - def __cmd_was_sent_before_my_start(self, cmd:AgentCmd): + def __cmd_was_sent_before_my_start(self, cmd:AgentCmd)->bool: return cmd.s_deposit_time < self.__UP_SINCE diff --git a/src/core/pyros_django/agent/AgentBasic.py b/src/core/pyros_django/agent/AgentBasic.py index 2ebec3b..8a3b30d 100755 --- a/src/core/pyros_django/agent/AgentBasic.py +++ b/src/core/pyros_django/agent/AgentBasic.py @@ -46,22 +46,22 @@ class AgentBasic(Agent): # Normal Commands #("set_specific2", 5, 0), - ("do_specific10", 10, Agent.EXEC_MODE.SEQUENTIAL), - ("do_specific30", 3, Agent.EXEC_MODE.SEQUENTIAL), - ("do_cmd_with_long_exec_time", 3, Agent.EXEC_MODE.SEQUENTIAL), + ("do_specific10", 1, Agent.EXEC_MODE.SEQUENTIAL), + ("do_specific30", 0, Agent.EXEC_MODE.SEQUENTIAL), + ("do_cmd_with_long_exec_time", 50, Agent.EXEC_MODE.THREAD), ] # Deactivate some tests, so that test scenario runs faster during DEV # on DEV - #COMMIT_ONLY = False + COMMIT_ONLY = False # on commit only - COMMIT_ONLY = True + #COMMIT_ONLY = True # @override _TEST_COMMANDS_LIST = [ - # Format : (DO_IT, "self cmd_name cmd_args", timeout, "expected_result", expected_status), + # Format : (DO_IT, "self cmd_name cmd_args", validity, "expected_result", expected_status), #("self do_stop now", 200, '', Agent.CMD_STATUS.CMD_EXECUTED), @@ -71,14 +71,16 @@ class AgentBasic(Agent): #("self do_stop asap", 200, 'STOPPING asap', Agent.CMD_STATUS.CMD_EXECUTED), # get_specific_cmds - (True, " self get_specific_cmds ", 200, + (True, " self get_all_cmds ", 100, None, #'do_specific10(arg1:int,arg2:int,arg3:float,arg4:str,arg5:typing.Tuple[int, str, int],arg6:typing.List[int]);do_specific30();do_cmd_raising_some_exception();do_cmd_unimplemented(U)', Agent.CMD_STATUS.CMD_EXECUTED ), - # do_restart (COMMIT_ONLY, "self do_restart asap", 200, 'RESTARTING asap', Agent.CMD_STATUS.CMD_EXECUTED), + + # do_restart + (True, "self do_cmd_with_long_exec_time", 200, None, Agent.CMD_STATUS.CMD_EXECUTED), #("self do_stop", 200, 'STOPPING asap', Agent.CMD_STATUS.CMD_EXECUTED), # ---------------------------------------------------- @@ -371,8 +373,39 @@ class AgentBasic(Agent): # Long time execution command def do_cmd_with_long_exec_time(self): - self.waitfor(100) - return "took 100s to execute" + nbsec=8 + + res = f"1 - now sleeping {nbsec} sec" + self.CC.set_result(res, True) + #self.CC.get_updated_result() + while self.CC.get_updated_result() != res: self.sleep(1) + self.sleep(nbsec) + + res = f"2 - now sleeping {nbsec} sec" + self.CC.set_result(res, True) + #self.CC.get_updated_result() + while self.CC.get_updated_result() != res: self.sleep(1) + self.sleep(nbsec) + + res = f"3 - now sleeping {nbsec} sec" + self.CC.set_result(res, True) + #self.CC.get_updated_result() + while self.CC.get_updated_result() != res: self.sleep(1) + self.sleep(nbsec) + + res = f"4 - now sleeping {nbsec} sec" + self.CC.set_result(res, True) + #self.CC.get_updated_result() + while self.CC.get_updated_result() != res: self.sleep(1) + self.sleep(nbsec) + + res = f"5 - now sleeping {nbsec} sec" + self.CC.set_result(res, True) + #self.CC.get_updated_result() + while self.CC.get_updated_result() != res: self.sleep(1) + self.sleep(nbsec) + + return f"should have taken {5*nbsec}s to execute" def do_specific10(self, arg1:int, -- libgit2 0.21.2