Commit cbbd0c655a398cd45c75cc119342f5492397e8c3

Authored by Etienne Pallier
1 parent c655f27d
Exists in dev

Updated models.AgentCmd plus complet et precis -> faire un PYROS_DOCKER_UPDATE p…

…our mettre BD a jour - si question, repondre y a champ end_time replaced with r_end_time
src/core/pyros_django/agent/Agent.py
@@ -408,7 +408,7 @@ class Agent: @@ -408,7 +408,7 @@ class Agent:
408 # ------------------------------- 408 # -------------------------------
409 409
410 # This command has a validity of 0s and thus should be tagged as "expired" 410 # This command has a validity of 0s and thus should be tagged as "expired"
411 - ("self set_mode ATTENTIVE", 0, "MODE = ATTENTIVE", CMD_STATUS.CMD_OUTOFDATE), 411 + ("self set_mode ATTENTIVE", 0, "MODE = ATTENTIVE", CMD_STATUS.CMD_EXPIRED),
412 412
413 # Agent general command 413 # Agent general command
414 ("self set_mode ATTENTIVE", 200, "MODE = ATTENTIVE", CMD_STATUS.CMD_EXECUTED), 414 ("self set_mode ATTENTIVE", 200, "MODE = ATTENTIVE", CMD_STATUS.CMD_EXECUTED),
@@ -1042,6 +1042,8 @@ class Agent: @@ -1042,6 +1042,8 @@ class Agent:
1042 log.info("*"*10 + " NEXT COMMAND PROCESSING (START) " + "*"*10 + '\n') 1042 log.info("*"*10 + " NEXT COMMAND PROCESSING (START) " + "*"*10 + '\n')
1043 try : 1043 try :
1044 cmd = self._process_next_command_if_exists() 1044 cmd = self._process_next_command_if_exists()
  1045 + print("after get")
  1046 + print(cmd)
1045 except (AgentCmdUnimplementedException, AgentCmdBadArgsException, UnknownCmdException) as e : 1047 except (AgentCmdUnimplementedException, AgentCmdBadArgsException, UnknownCmdException) as e :
1046 print(e) 1048 print(e)
1047 log.error(f"EXCEPTION on Agent command '{e.cmd.name}'") 1049 log.error(f"EXCEPTION on Agent command '{e.cmd.name}'")
@@ -1706,7 +1708,7 @@ class Agent: @@ -1706,7 +1708,7 @@ class Agent:
1706 if not commands.exists(): 1708 if not commands.exists():
1707 log.info("<None>") 1709 log.info("<None>")
1708 return None 1710 return None
1709 - "Current pending (or running) commands are (time ordered):" 1711 + "Current pending (or running) commands (time ordered):"
1710 AgentCmd.show_commands(commands) 1712 AgentCmd.show_commands(commands)
1711 1713
1712 # 2) Check if a PRIORITY command is in the list (even at the very end), 1714 # 2) Check if a PRIORITY command is in the list (even at the very end),
@@ -1719,7 +1721,7 @@ class Agent: @@ -1719,7 +1721,7 @@ class Agent:
1719 if cmd.is_running(): 1721 if cmd.is_running():
1720 return None 1722 return None
1721 if cmd.is_expired(): 1723 if cmd.is_expired():
1722 - cmd.set_as_outofdate() 1724 + cmd.set_as_expired()
1723 return None 1725 return None
1724 return cmd 1726 return cmd
1725 ''' 1727 '''
@@ -1749,7 +1751,7 @@ class Agent: @@ -1749,7 +1751,7 @@ class Agent:
1749 cmd = commands.first() 1751 cmd = commands.first()
1750 1752
1751 if cmd.is_expired(): 1753 if cmd.is_expired():
1752 - cmd.set_as_outofdate() 1754 + cmd.set_as_expired()
1753 return None 1755 return None
1754 1756
1755 if cmd.is_running(): 1757 if cmd.is_running():
@@ -2405,6 +2407,7 @@ class Agent: @@ -2405,6 +2407,7 @@ class Agent:
2405 #self._cmdts.set_as_pending() 2407 #self._cmdts.set_as_pending()
2406 # SEND 2408 # SEND
2407 self._cmdts.send() 2409 self._cmdts.send()
  2410 + log.info(f"*** NEW COMMAND SENT")
2408 #cmdts_is_processed = False 2411 #cmdts_is_processed = False
2409 #cmdts_res = None 2412 #cmdts_res = None
2410 2413
src/core/pyros_django/common/models.py
@@ -441,27 +441,49 @@ class AgentCmd(models.Model): @@ -441,27 +441,49 @@ class AgentCmd(models.Model):
441 441
442 # Command status codes 442 # Command status codes
443 CMD_STATUS_CODES = Choices( 443 CMD_STATUS_CODES = Choices(
  444 +
444 # Cmd is pending, waiting for execution 445 # Cmd is pending, waiting for execution
445 "CMD_PENDING", 446 "CMD_PENDING",
446 447
447 - # Cmd is running (after having been "read")  
448 - "CMD_RUNNING",  
449 -  
450 - # Cmd was skipped (not run, because the agent is in a mode which does not allow execution of this command) 448 +
  449 + ### 1 - ERRORS BEFORE EVEN BEING RUN ###
  450 +
  451 + # Cmd is invalid (unknown or bad args)
  452 + "CMD_INVALID",
  453 +
  454 + # Cmd is not yet implemented (by recipient agent)
  455 + "CMD_UNIMPLEMENTED",
  456 +
  457 + # Cmd was skipped (not run, because the agent is IDLE (or in a mode which does not allow execution of this command)
451 "CMD_SKIPPED", 458 "CMD_SKIPPED",
452 -  
453 - # Cmd was aborted, killed  
454 - # - either because of exec time too long and timeout reached  
455 - # - or because an agent sent an "do_abort_current_command" command  
456 - "CMD_KILLED",  
457 459
458 # Cmd is out of date (expired) 460 # Cmd is out of date (expired)
459 # because it has been pending for too long 461 # because it has been pending for too long
460 - "CMD_OUTOFDATE", 462 + #"CMD_OUTOFDATE",
  463 + "CMD_EXPIRED",
  464 +
461 465
462 - # Cmd was run and finished ok 466 + ### 2 - RUNNING ###
  467 + # Cmd is running (after having been "read")
  468 + "CMD_RUNNING",
  469 +
  470 +
  471 + ### 3 - ERRORS WHILE RUNNING ###
  472 +
  473 + # - Cmd was run and finished ok
463 # ( simulé par un sleep(3) dans AgentX.core_process() ) 474 # ( simulé par un sleep(3) dans AgentX.core_process() )
464 "CMD_EXECUTED", 475 "CMD_EXECUTED",
  476 +
  477 + # - Cmd was running, but then an error occured
  478 + "CMD_EXEC_ERROR",
  479 +
  480 + # - Cmd was running, but took too much time and timeout was reached
  481 + "CMD_EXEC_TIMEOUT",
  482 +
  483 + # - Cmd was aborted, killed by another agent
  484 + #"CMD_KILLED",
  485 + "CMD_EXEC_KILLED",
  486 +
465 ) 487 )
466 488
467 # Command default validity duration (from sending time) BEFORE execution, before being perempted (in sec) 489 # Command default validity duration (from sending time) BEFORE execution, before being perempted (in sec)
@@ -493,6 +515,7 @@ class AgentCmd(models.Model): @@ -493,6 +515,7 @@ class AgentCmd(models.Model):
493 #state = models.CharField(choices = CMD_STATUS_CODES, default=CMD_STATUS_CODES.CMD_PENDING, max_length=20) 515 #state = models.CharField(choices = CMD_STATUS_CODES, default=CMD_STATUS_CODES.CMD_PENDING, max_length=20)
494 state = models.CharField(choices=CMD_STATUS_CODES, default=CMD_STATUS_CODES.CMD_PENDING, max_length=20) 516 state = models.CharField(choices=CMD_STATUS_CODES, default=CMD_STATUS_CODES.CMD_PENDING, max_length=20)
495 #state = models.IntegerField(choices=CMD_STATUS_CODES, default=RSCODE_PENDING) 517 #state = models.IntegerField(choices=CMD_STATUS_CODES, default=RSCODE_PENDING)
  518 +
496 # TODO: maybe à mettre au format json (key:value) 519 # TODO: maybe à mettre au format json (key:value)
497 result = models.CharField(max_length=400, blank=True) 520 result = models.CharField(max_length=400, blank=True)
498 521
@@ -502,8 +525,9 @@ class AgentCmd(models.Model): @@ -502,8 +525,9 @@ class AgentCmd(models.Model):
502 r_read_time = models.DateTimeField(null=True) 525 r_read_time = models.DateTimeField(null=True)
503 # - on launching: 526 # - on launching:
504 r_start_time = models.DateTimeField(null=True) 527 r_start_time = models.DateTimeField(null=True)
505 - # - on skipping (because idle), expiration (because too old), kill, or end of execution  
506 - end_time = models.DateTimeField(null=True) 528 + # - on end (after execution started or not)
  529 + #end_time = models.DateTimeField(null=True)
  530 + r_end_time = models.DateTimeField(null=True)
507 531
508 # Other agent (than sender or recipient) that set this command to KILLED or EXPIRED 532 # Other agent (than sender or recipient) that set this command to KILLED or EXPIRED
509 killer_agent_name = models.CharField(max_length=50, help_text='sender agent name', null=True) 533 killer_agent_name = models.CharField(max_length=50, help_text='sender agent name', null=True)
@@ -900,9 +924,12 @@ class AgentCmd(models.Model): @@ -900,9 +924,12 @@ class AgentCmd(models.Model):
900 return self.state == self.CMD_STATUS_CODES.CMD_SKIPPED 924 return self.state == self.CMD_STATUS_CODES.CMD_SKIPPED
901 925
902 def is_killed(self): 926 def is_killed(self):
903 - return self.state == self.CMD_STATUS_CODES.CMD_KILLED 927 + return self.state == self.CMD_STATUS_CODES.CMD_EXEC_KILLED
904 928
905 def is_expired(self): 929 def is_expired(self):
  930 + if self.state == self.CMD_STATUS_CODES.CMD_EXPIRED: return True
  931 + # Must NOT be running
  932 + if self.is_running(): return False
906 # return (datetime.utcnow() - self.s_deposit_time) > timedelta(seconds = self.validity_duration) 933 # return (datetime.utcnow() - self.s_deposit_time) > timedelta(seconds = self.validity_duration)
907 elapsed_time = (datetime.utcnow().astimezone() - self.s_deposit_time) 934 elapsed_time = (datetime.utcnow().astimezone() - self.s_deposit_time)
908 printd("elapsed_time", elapsed_time) 935 printd("elapsed_time", elapsed_time)
@@ -915,6 +942,7 @@ class AgentCmd(models.Model): @@ -915,6 +942,7 @@ class AgentCmd(models.Model):
915 """ 942 """
916 943
917 def is_timeout(self): 944 def is_timeout(self):
  945 + if self.state == self.CMD_STATUS_CODES.CMD_EXEC_TIMEOUT: return True
918 # Must be running 946 # Must be running
919 if not self.is_running(): return False 947 if not self.is_running(): return False
920 # return (datetime.utcnow() - self.s_deposit_time) > timedelta(seconds = self.validity_duration) 948 # return (datetime.utcnow() - self.s_deposit_time) > timedelta(seconds = self.validity_duration)
@@ -952,10 +980,11 @@ class AgentCmd(models.Model): @@ -952,10 +980,11 @@ class AgentCmd(models.Model):
952 # Optimization: update the related fields, but does not work, why ? 980 # Optimization: update the related fields, but does not work, why ?
953 ##self.save(update_fields=["state", "r_processed_time"]) 981 ##self.save(update_fields=["state", "r_processed_time"])
954 982
955 - def set_as_outofdate(self): 983 + #def set_as_outofdate(self):
  984 + def set_as_expired(self):
956 print( 985 print(
957 f"- Set this command as expired (older than its validity duration of {self.validity_duration}s): {self}") 986 f"- Set this command as expired (older than its validity duration of {self.validity_duration}s): {self}")
958 - self.set_state_to(self.CMD_STATUS_CODES.CMD_OUTOFDATE) 987 + self.set_state_to(self.CMD_STATUS_CODES.CMD_EXPIRED)
959 988
960 def set_as_pending(self): 989 def set_as_pending(self):
961 self.set_state_to(self.CMD_STATUS_CODES.CMD_PENDING) 990 self.set_state_to(self.CMD_STATUS_CODES.CMD_PENDING)
@@ -966,7 +995,7 @@ class AgentCmd(models.Model): @@ -966,7 +995,7 @@ class AgentCmd(models.Model):
966 def set_as_killed_by(self, author_agent: str): 995 def set_as_killed_by(self, author_agent: str):
967 printd(f"- Set command {self.name} as killed") 996 printd(f"- Set command {self.name} as killed")
968 #print(f"- Set this command as killed: {self}") 997 #print(f"- Set this command as killed: {self}")
969 - self.set_state_to(self.CMD_STATUS_CODES.CMD_KILLED, author_agent) 998 + self.set_state_to(self.CMD_STATUS_CODES.CMD_EXEC_KILLED, author_agent)
970 999
971 def set_as_running(self): 1000 def set_as_running(self):
972 printd(f"- Set command {self.name} as running") 1001 printd(f"- Set command {self.name} as running")
@@ -983,20 +1012,20 @@ class AgentCmd(models.Model): @@ -983,20 +1012,20 @@ class AgentCmd(models.Model):
983 now_time = datetime.utcnow().astimezone() 1012 now_time = datetime.utcnow().astimezone()
984 if result: 1013 if result:
985 self.set_result(result, False) 1014 self.set_result(result, False)
986 - if status in (self.CMD_STATUS_CODES.CMD_RUNNING, self.CMD_STATUS_CODES.CMD_SKIPPED, self.CMD_STATUS_CODES.CMD_OUTOFDATE): 1015 + if status in (self.CMD_STATUS_CODES.CMD_RUNNING, self.CMD_STATUS_CODES.CMD_SKIPPED, self.CMD_STATUS_CODES.CMD_EXPIRED):
987 assert self.is_pending() 1016 assert self.is_pending()
988 if status == self.CMD_STATUS_CODES.CMD_RUNNING: 1017 if status == self.CMD_STATUS_CODES.CMD_RUNNING:
989 self.r_start_time = now_time 1018 self.r_start_time = now_time
990 elif status in (self.CMD_STATUS_CODES.CMD_SKIPPED): 1019 elif status in (self.CMD_STATUS_CODES.CMD_SKIPPED):
991 - self.end_time = now_time 1020 + self.r_end_time = now_time
992 # EXPIRED 1021 # EXPIRED
993 - elif status == self.CMD_STATUS_CODES.CMD_OUTOFDATE:  
994 - self.end_time = now_time 1022 + elif status == self.CMD_STATUS_CODES.CMD_EXPIRED:
  1023 + self.r_end_time = now_time
995 self.killer_agent_name = author_agent_name 1024 self.killer_agent_name = author_agent_name
996 - elif status in (self.CMD_STATUS_CODES.CMD_EXECUTED, self.CMD_STATUS_CODES.CMD_KILLED): 1025 + elif status in (self.CMD_STATUS_CODES.CMD_EXECUTED, self.CMD_STATUS_CODES.CMD_EXEC_KILLED):
997 assert self.is_running() 1026 assert self.is_running()
998 - self.end_time = now_time  
999 - if status == self.CMD_STATUS_CODES.CMD_KILLED: 1027 + self.r_end_time = now_time
  1028 + if status == self.CMD_STATUS_CODES.CMD_EXEC_KILLED:
1000 self.killer_agent_name = author_agent_name 1029 self.killer_agent_name = author_agent_name
1001 # Update command status 1030 # Update command status
1002 self.state = status 1031 self.state = status