Commit 975bf9f67db19fcdcdfc7a2d9047830859ea8fa1

Authored by Etienne Pallier
1 parent 088d73f1
Exists in dev

bugfix priority command read time

privatedev/plugin/agent/AgentBasic.py
... ... @@ -46,22 +46,22 @@ class AgentBasic(Agent):
46 46  
47 47 # Normal Commands
48 48 #("set_specific2", 5, 0),
49   - ("do_specific10", 10, Agent.EXEC_MODE.SEQUENTIAL),
50   - ("do_specific30", 3, Agent.EXEC_MODE.SEQUENTIAL),
51   - ("do_cmd_with_long_exec_time", 3, Agent.EXEC_MODE.SEQUENTIAL),
  49 + ("do_specific10", 1, Agent.EXEC_MODE.SEQUENTIAL),
  50 + ("do_specific30", 0, Agent.EXEC_MODE.SEQUENTIAL),
  51 + ("do_cmd_with_long_exec_time", 50, Agent.EXEC_MODE.THREAD),
52 52  
53 53 ]
54 54  
55 55 # Deactivate some tests, so that test scenario runs faster during DEV
56 56 # on DEV
57   - #COMMIT_ONLY = False
  57 + COMMIT_ONLY = False
58 58 # on commit only
59   - COMMIT_ONLY = True
  59 + #COMMIT_ONLY = True
60 60  
61 61 # @override
62 62 _TEST_COMMANDS_LIST = [
63 63  
64   - # Format : (DO_IT, "self cmd_name cmd_args", timeout, "expected_result", expected_status),
  64 + # Format : (DO_IT, "self cmd_name cmd_args", validity, "expected_result", expected_status),
65 65  
66 66 #("self do_stop now", 200, '', Agent.CMD_STATUS.CMD_EXECUTED),
67 67  
... ... @@ -71,14 +71,16 @@ class AgentBasic(Agent):
71 71 #("self do_stop asap", 200, 'STOPPING asap', Agent.CMD_STATUS.CMD_EXECUTED),
72 72  
73 73 # get_specific_cmds
74   - (True, " self get_specific_cmds ", 200,
  74 + (True, " self get_all_cmds ", 100,
75 75 None,
76 76 #'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)',
77 77 Agent.CMD_STATUS.CMD_EXECUTED
78 78 ),
79 79  
80   - # do_restart
81 80 (COMMIT_ONLY, "self do_restart asap", 200, 'RESTARTING asap', Agent.CMD_STATUS.CMD_EXECUTED),
  81 +
  82 + # do_restart
  83 + (True, "self do_cmd_with_long_exec_time", 200, None, Agent.CMD_STATUS.CMD_EXECUTED),
82 84 #("self do_stop", 200, 'STOPPING asap', Agent.CMD_STATUS.CMD_EXECUTED),
83 85  
84 86 # ----------------------------------------------------
... ... @@ -371,8 +373,39 @@ class AgentBasic(Agent):
371 373  
372 374 # Long time execution command
373 375 def do_cmd_with_long_exec_time(self):
374   - self.waitfor(100)
375   - return "took 100s to execute"
  376 + nbsec=8
  377 +
  378 + res = f"1 - now sleeping {nbsec} sec"
  379 + self.CC.set_result(res, True)
  380 + #self.CC.get_updated_result()
  381 + while self.CC.get_updated_result() != res: self.sleep(1)
  382 + self.sleep(nbsec)
  383 +
  384 + res = f"2 - now sleeping {nbsec} sec"
  385 + self.CC.set_result(res, True)
  386 + #self.CC.get_updated_result()
  387 + while self.CC.get_updated_result() != res: self.sleep(1)
  388 + self.sleep(nbsec)
  389 +
  390 + res = f"3 - now sleeping {nbsec} sec"
  391 + self.CC.set_result(res, True)
  392 + #self.CC.get_updated_result()
  393 + while self.CC.get_updated_result() != res: self.sleep(1)
  394 + self.sleep(nbsec)
  395 +
  396 + res = f"4 - now sleeping {nbsec} sec"
  397 + self.CC.set_result(res, True)
  398 + #self.CC.get_updated_result()
  399 + while self.CC.get_updated_result() != res: self.sleep(1)
  400 + self.sleep(nbsec)
  401 +
  402 + res = f"5 - now sleeping {nbsec} sec"
  403 + self.CC.set_result(res, True)
  404 + #self.CC.get_updated_result()
  405 + while self.CC.get_updated_result() != res: self.sleep(1)
  406 + self.sleep(nbsec)
  407 +
  408 + return f"should have taken {5*nbsec}s to execute"
376 409  
377 410 def do_specific10(self,
378 411 arg1:int,
... ...
src/core/pyros_django/agent/Agent.py
... ... @@ -327,6 +327,7 @@ class Agent:
327 327 #_AGENT_SPECIFIC_COMMANDS: List[ Tuple[str, int, int] ] = [
328 328 _AGENT_SPECIFIC_COMMANDS: List[ Tuple[str, int, EXEC_MODE] ] = [
329 329 # Format : (โ€œcmd_nameโ€, timeout, exec_mode)
  330 +
330 331 ("do_specific1", 10, EXEC_MODE.SEQUENTIAL),
331 332 #("set_specific2", 5, 0),
332 333 ("do_specific3", 3, EXEC_MODE.THREAD),
... ... @@ -368,7 +369,7 @@ class Agent:
368 369 # - cmd : the command name
369 370 # - args : (optional) the list of command arguments, separated by blanks : arg1 arg2 arg3 ...
370 371 #
371   - # - validity : the command is valid for this duration, afterwards you can forget it
  372 + # - validity : the command is valid for this duration, afterwards you can skip it
372 373 #
373 374 # - expected_res : the expected result (set to None if not to be tested)
374 375 #
... ... @@ -381,9 +382,10 @@ class Agent:
381 382 # - "self do_restart_loop" => means to send the command "do_restart_loop" to MYSELF (no args)
382 383 #
383 384 #_TEST_COMMANDS_LIST: List[ Tuple[ bool, str, int, Union[str,None], Union[int,None] ] ] = [
384   - _TEST_COMMANDS_LIST: List[ Tuple[ bool, str, int, Optional[str], Optional[int]] ] = [
  385 + ##_TEST_COMMANDS_LIST: List[ Tuple[ bool, str, int, Optional[str], AgentCmd.CMD_STATUS_CODES ] ] = [
  386 + _TEST_COMMANDS_LIST: List[ Tuple[ bool, str, Optional[int], Optional[str], Optional[int]] ] = [
  387 + # Format : (DO_IT, "self cmd_name cmd_args", validity, "expected_result", expected_status),
385 388  
386   - # Format : (DO_IT, "self cmd_name cmd_args", timeout, "expected_result", expected_status),
387 389 #("self do_stop now", 200, '15.5', None),
388 390 (True, "self do_specific1 1 2 3.5 titi (3,'titi',5) [1,3,5,7,9]", 200, '15.5', None),
389 391  
... ... @@ -1127,7 +1129,7 @@ class Agent:
1127 1129 ###cmd = self.__start_next_received_cmd_if_possible_and_exists()
1128 1130 # Better :
1129 1131 cmd = self.__get_next_received_cmd_if_possible_and_exists()
1130   - if cmd:
  1132 + if cmd:
1131 1133 self.__CC_prev = self.__CC
1132 1134 # This way, started commands will be able to access the current command via self.__CC
1133 1135 self.__CC = cmd
... ... @@ -1169,7 +1171,9 @@ class Agent:
1169 1171  
1170 1172  
1171 1173 def __get_next_received_cmd_if_possible_and_exists(self)->Optional[AgentCmd]:
  1174 + self.__set_and_log_status(self.AGT_STATUS.IN_MAIN_LOOP_GET_NEXT_CMD)
1172 1175 cmd = None
  1176 +
1173 1177 # GET NEXT COMMAND if exists and if current cmd is finished
1174 1178 #if self.__CC is None or self.__CC.is_finished() or self.__priority_cmd_received():
1175 1179 CMD_PRIO = self.__get_received_priority_cmd_if_exists()
... ... @@ -1189,10 +1193,12 @@ class Agent:
1189 1193 log.info("RUNNING PRIORITY COMMAND:")
1190 1194 log.info(str(CMD_PRIO))
1191 1195 cmd = CMD_PRIO if CMD_PRIO else self.__get_next_received_cmd_if_exists()
  1196 +
1192 1197 return cmd
1193 1198  
1194 1199  
1195 1200 def __start_next_received_cmd(self, cmd:AgentCmd)->None:
  1201 + cmd.set_read_time()
1196 1202 print()
1197 1203 print()
1198 1204 log.info("*"*10 + " NEXT CMD RECEIVED PROCESSING (START) " + "*"*10 + '\n')
... ... @@ -1427,7 +1433,7 @@ class Agent:
1427 1433 def __start_cmd(self, cmd:AgentCmd)->None:
1428 1434 ''' Processing the next pending command (self.__CC) '''
1429 1435 #cmd = self.__CC
1430   - self.__set_and_log_status(self.AGT_STATUS.IN_MAIN_LOOP_GET_NEXT_CMD)
  1436 + #self.__set_and_log_status(self.AGT_STATUS.IN_MAIN_LOOP_GET_NEXT_CMD)
1431 1437  
1432 1438 #print()
1433 1439 #print()
... ... @@ -1640,11 +1646,11 @@ class Agent:
1640 1646 old_commands.delete()
1641 1647 """
1642 1648  
1643   - def sleep(self, nbsec):
  1649 + def sleep(self, nbsec:float):
1644 1650 log.info(f"Now, waiting (sleeping) for {nbsec} second(s)...")
1645 1651 time.sleep(nbsec)
1646 1652 # alias to sleep
1647   - def waitfor(self, nbsec:float=2.0):
  1653 + def waitfor(self, nbsec:float):
1648 1654 '''
1649 1655 # thread
1650 1656 if self._current_device_cmd_thread and self.RUN_IN_THREAD:
... ... @@ -1661,7 +1667,7 @@ class Agent:
1661 1667 # MODE & STATUS
1662 1668 #
1663 1669  
1664   - def get_state(self):
  1670 + def get_state(self)->str:
1665 1671 return f"MODE is {self.__get_mode()} ; STATUS is {self.get_status()} ; ITERATION #{self.__iter_num}"
1666 1672  
1667 1673 def show_state(self):
... ... @@ -1674,20 +1680,21 @@ class Agent:
1674 1680 #
1675 1681  
1676 1682 # - GET
1677   - def get_status(self):
  1683 + def get_status(self)->AgentSurvey.STATUS_CHOICES:
1678 1684 return self.__status
1679 1685  
1680 1686 # - SET
1681 1687  
1682 1688 # Private because automatic, must not be changed, depends only on the step the Agent is currently running, used only by Agent (superclass)
1683   - def __set_status(self, status:str):
  1689 + def __set_status(self, status:AgentSurvey.STATUS_CHOICES):
1684 1690 #self.printd(f"[{status}] (switching from status {self.__status})")
1685 1691 log.debug(f"[{status}]")
1686 1692 self.__status = status
1687 1693 return False
1688 1694  
1689 1695 # Private because automatic, must not be changed, used only by Agent (superclass)
1690   - def __set_and_log_status(self, status:str):
  1696 + ##def __set_and_log_status(self, status:str):
  1697 + def __set_and_log_status(self, status:AgentSurvey.STATUS_CHOICES):
1691 1698 self.__set_status(status)
1692 1699 self.__log_agent_state()
1693 1700  
... ... @@ -1697,12 +1704,12 @@ class Agent:
1697 1704 #
1698 1705  
1699 1706 # - GET
1700   - def __get_mode(self):
  1707 + def __get_mode(self)->AgentSurvey.MODE_CHOICES:
1701 1708 return self.__mode
1702 1709 # Test mode
1703   - def IS_MODE_IDLE(self): return self.__get_mode() == AgentSurvey.MODE_CHOICES.IDLE
1704   - def IS_MODE_ROUTINE(self): return self.__get_mode() == AgentSurvey.MODE_CHOICES.ROUTINE
1705   - def IS_MODE_ATTENTIVE(self): return self.__get_mode() == AgentSurvey.MODE_CHOICES.ATTENTIVE
  1710 + def IS_MODE_IDLE(self)->bool: return self.__get_mode() == AgentSurvey.MODE_CHOICES.IDLE
  1711 + def IS_MODE_ROUTINE(self)->bool: return self.__get_mode() == AgentSurvey.MODE_CHOICES.ROUTINE
  1712 + def IS_MODE_ATTENTIVE(self)->bool: return self.__get_mode() == AgentSurvey.MODE_CHOICES.ATTENTIVE
1706 1713 # @deprecated
1707 1714 def IS_IDLE(self): return self.IS_MODE_IDLE()
1708 1715 # @deprecated
... ... @@ -1743,7 +1750,7 @@ class Agent:
1743 1750 """
1744 1751 def suspend(self):
1745 1752 """
1746   - TODO:
  1753 + FIXME:
1747 1754 Mode IDLE (doit rester ร  l'รฉcoute d'un resume,
1748 1755 et doit continuer ร  alimenter les tables pour informer de son รฉtat via tables agents_logs,
1749 1756 et lire table agents_command pour reprendre via resume,
... ... @@ -1754,6 +1761,7 @@ class Agent:
1754 1761  
1755 1762 def resume(self):
1756 1763 """
  1764 + FIXME:
1757 1765 Quit suspend() mode
1758 1766 """
1759 1767 self._set_active()
... ... @@ -2105,7 +2113,7 @@ class Agent:
2105 2113 # so return it for execution
2106 2114 #self.printd(f"Got command {cmd.name} sent by agent {cmd.sender} at {cmd.sender_deposit_time}")
2107 2115 #self.printd(f"Starting processing of this command")
2108   - next_cmd.set_read_time()
  2116 + ##next_cmd.set_read_time()
2109 2117 return next_cmd
2110 2118  
2111 2119  
... ... @@ -2785,7 +2793,7 @@ class Agent:
2785 2793  
2786 2794 # To be overriden by subclass (AgentDevice...)
2787 2795 # @abstract
2788   - def is_device_level_cmd(self, cmd):
  2796 + def is_device_level_cmd(self, cmd)->bool:
2789 2797 return False
2790 2798  
2791 2799 '''
... ... @@ -2802,7 +2810,7 @@ class Agent:
2802 2810 '''
2803 2811  
2804 2812  
2805   - def __cmd_was_sent_before_my_start(self, cmd:AgentCmd):
  2813 + def __cmd_was_sent_before_my_start(self, cmd:AgentCmd)->bool:
2806 2814 return cmd.s_deposit_time < self.__UP_SINCE
2807 2815  
2808 2816  
... ...
src/core/pyros_django/agent/AgentBasic.py
... ... @@ -46,22 +46,22 @@ class AgentBasic(Agent):
46 46  
47 47 # Normal Commands
48 48 #("set_specific2", 5, 0),
49   - ("do_specific10", 10, Agent.EXEC_MODE.SEQUENTIAL),
50   - ("do_specific30", 3, Agent.EXEC_MODE.SEQUENTIAL),
51   - ("do_cmd_with_long_exec_time", 3, Agent.EXEC_MODE.SEQUENTIAL),
  49 + ("do_specific10", 1, Agent.EXEC_MODE.SEQUENTIAL),
  50 + ("do_specific30", 0, Agent.EXEC_MODE.SEQUENTIAL),
  51 + ("do_cmd_with_long_exec_time", 50, Agent.EXEC_MODE.THREAD),
52 52  
53 53 ]
54 54  
55 55 # Deactivate some tests, so that test scenario runs faster during DEV
56 56 # on DEV
57   - #COMMIT_ONLY = False
  57 + COMMIT_ONLY = False
58 58 # on commit only
59   - COMMIT_ONLY = True
  59 + #COMMIT_ONLY = True
60 60  
61 61 # @override
62 62 _TEST_COMMANDS_LIST = [
63 63  
64   - # Format : (DO_IT, "self cmd_name cmd_args", timeout, "expected_result", expected_status),
  64 + # Format : (DO_IT, "self cmd_name cmd_args", validity, "expected_result", expected_status),
65 65  
66 66 #("self do_stop now", 200, '', Agent.CMD_STATUS.CMD_EXECUTED),
67 67  
... ... @@ -71,14 +71,16 @@ class AgentBasic(Agent):
71 71 #("self do_stop asap", 200, 'STOPPING asap', Agent.CMD_STATUS.CMD_EXECUTED),
72 72  
73 73 # get_specific_cmds
74   - (True, " self get_specific_cmds ", 200,
  74 + (True, " self get_all_cmds ", 100,
75 75 None,
76 76 #'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)',
77 77 Agent.CMD_STATUS.CMD_EXECUTED
78 78 ),
79 79  
80   - # do_restart
81 80 (COMMIT_ONLY, "self do_restart asap", 200, 'RESTARTING asap', Agent.CMD_STATUS.CMD_EXECUTED),
  81 +
  82 + # do_restart
  83 + (True, "self do_cmd_with_long_exec_time", 200, None, Agent.CMD_STATUS.CMD_EXECUTED),
82 84 #("self do_stop", 200, 'STOPPING asap', Agent.CMD_STATUS.CMD_EXECUTED),
83 85  
84 86 # ----------------------------------------------------
... ... @@ -371,8 +373,39 @@ class AgentBasic(Agent):
371 373  
372 374 # Long time execution command
373 375 def do_cmd_with_long_exec_time(self):
374   - self.waitfor(100)
375   - return "took 100s to execute"
  376 + nbsec=8
  377 +
  378 + res = f"1 - now sleeping {nbsec} sec"
  379 + self.CC.set_result(res, True)
  380 + #self.CC.get_updated_result()
  381 + while self.CC.get_updated_result() != res: self.sleep(1)
  382 + self.sleep(nbsec)
  383 +
  384 + res = f"2 - now sleeping {nbsec} sec"
  385 + self.CC.set_result(res, True)
  386 + #self.CC.get_updated_result()
  387 + while self.CC.get_updated_result() != res: self.sleep(1)
  388 + self.sleep(nbsec)
  389 +
  390 + res = f"3 - now sleeping {nbsec} sec"
  391 + self.CC.set_result(res, True)
  392 + #self.CC.get_updated_result()
  393 + while self.CC.get_updated_result() != res: self.sleep(1)
  394 + self.sleep(nbsec)
  395 +
  396 + res = f"4 - now sleeping {nbsec} sec"
  397 + self.CC.set_result(res, True)
  398 + #self.CC.get_updated_result()
  399 + while self.CC.get_updated_result() != res: self.sleep(1)
  400 + self.sleep(nbsec)
  401 +
  402 + res = f"5 - now sleeping {nbsec} sec"
  403 + self.CC.set_result(res, True)
  404 + #self.CC.get_updated_result()
  405 + while self.CC.get_updated_result() != res: self.sleep(1)
  406 + self.sleep(nbsec)
  407 +
  408 + return f"should have taken {5*nbsec}s to execute"
376 409  
377 410 def do_specific10(self,
378 411 arg1:int,
... ...