From cbbd0c655a398cd45c75cc119342f5492397e8c3 Mon Sep 17 00:00:00 2001 From: Etienne Pallier Date: Tue, 13 Sep 2022 02:44:24 +0200 Subject: [PATCH] Updated models.AgentCmd plus complet et precis -> faire un PYROS_DOCKER_UPDATE pour mettre BD a jour - si question, repondre y a champ end_time replaced with r_end_time --- src/core/pyros_django/agent/Agent.py | 11 +++++++---- src/core/pyros_django/common/models.py | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------ 2 files changed, 60 insertions(+), 28 deletions(-) diff --git a/src/core/pyros_django/agent/Agent.py b/src/core/pyros_django/agent/Agent.py index 8d3755c..5fa42e5 100755 --- a/src/core/pyros_django/agent/Agent.py +++ b/src/core/pyros_django/agent/Agent.py @@ -408,7 +408,7 @@ class Agent: # ------------------------------- # This command has a validity of 0s and thus should be tagged as "expired" - ("self set_mode ATTENTIVE", 0, "MODE = ATTENTIVE", CMD_STATUS.CMD_OUTOFDATE), + ("self set_mode ATTENTIVE", 0, "MODE = ATTENTIVE", CMD_STATUS.CMD_EXPIRED), # Agent general command ("self set_mode ATTENTIVE", 200, "MODE = ATTENTIVE", CMD_STATUS.CMD_EXECUTED), @@ -1042,6 +1042,8 @@ class Agent: log.info("*"*10 + " NEXT COMMAND PROCESSING (START) " + "*"*10 + '\n') try : cmd = self._process_next_command_if_exists() + print("after get") + print(cmd) except (AgentCmdUnimplementedException, AgentCmdBadArgsException, UnknownCmdException) as e : print(e) log.error(f"EXCEPTION on Agent command '{e.cmd.name}'") @@ -1706,7 +1708,7 @@ class Agent: if not commands.exists(): log.info("") return None - "Current pending (or running) commands are (time ordered):" + "Current pending (or running) commands (time ordered):" AgentCmd.show_commands(commands) # 2) Check if a PRIORITY command is in the list (even at the very end), @@ -1719,7 +1721,7 @@ class Agent: if cmd.is_running(): return None if cmd.is_expired(): - cmd.set_as_outofdate() + cmd.set_as_expired() return None return cmd ''' @@ -1749,7 +1751,7 @@ class Agent: cmd = commands.first() if cmd.is_expired(): - cmd.set_as_outofdate() + cmd.set_as_expired() return None if cmd.is_running(): @@ -2405,6 +2407,7 @@ class Agent: #self._cmdts.set_as_pending() # SEND self._cmdts.send() + log.info(f"*** NEW COMMAND SENT") #cmdts_is_processed = False #cmdts_res = None diff --git a/src/core/pyros_django/common/models.py b/src/core/pyros_django/common/models.py index 6679a7b..4409731 100644 --- a/src/core/pyros_django/common/models.py +++ b/src/core/pyros_django/common/models.py @@ -441,27 +441,49 @@ class AgentCmd(models.Model): # Command status codes CMD_STATUS_CODES = Choices( + # Cmd is pending, waiting for execution "CMD_PENDING", - # Cmd is running (after having been "read") - "CMD_RUNNING", - - # Cmd was skipped (not run, because the agent is in a mode which does not allow execution of this command) + + ### 1 - ERRORS BEFORE EVEN BEING RUN ### + + # Cmd is invalid (unknown or bad args) + "CMD_INVALID", + + # Cmd is not yet implemented (by recipient agent) + "CMD_UNIMPLEMENTED", + + # Cmd was skipped (not run, because the agent is IDLE (or in a mode which does not allow execution of this command) "CMD_SKIPPED", - - # Cmd was aborted, killed - # - either because of exec time too long and timeout reached - # - or because an agent sent an "do_abort_current_command" command - "CMD_KILLED", # Cmd is out of date (expired) # because it has been pending for too long - "CMD_OUTOFDATE", + #"CMD_OUTOFDATE", + "CMD_EXPIRED", + - # Cmd was run and finished ok + ### 2 - RUNNING ### + # Cmd is running (after having been "read") + "CMD_RUNNING", + + + ### 3 - ERRORS WHILE RUNNING ### + + # - Cmd was run and finished ok # ( simulé par un sleep(3) dans AgentX.core_process() ) "CMD_EXECUTED", + + # - Cmd was running, but then an error occured + "CMD_EXEC_ERROR", + + # - Cmd was running, but took too much time and timeout was reached + "CMD_EXEC_TIMEOUT", + + # - Cmd was aborted, killed by another agent + #"CMD_KILLED", + "CMD_EXEC_KILLED", + ) # Command default validity duration (from sending time) BEFORE execution, before being perempted (in sec) @@ -493,6 +515,7 @@ class AgentCmd(models.Model): #state = models.CharField(choices = CMD_STATUS_CODES, default=CMD_STATUS_CODES.CMD_PENDING, max_length=20) state = models.CharField(choices=CMD_STATUS_CODES, default=CMD_STATUS_CODES.CMD_PENDING, max_length=20) #state = models.IntegerField(choices=CMD_STATUS_CODES, default=RSCODE_PENDING) + # TODO: maybe à mettre au format json (key:value) result = models.CharField(max_length=400, blank=True) @@ -502,8 +525,9 @@ class AgentCmd(models.Model): r_read_time = models.DateTimeField(null=True) # - on launching: r_start_time = models.DateTimeField(null=True) - # - on skipping (because idle), expiration (because too old), kill, or end of execution - end_time = models.DateTimeField(null=True) + # - on end (after execution started or not) + #end_time = models.DateTimeField(null=True) + r_end_time = models.DateTimeField(null=True) # Other agent (than sender or recipient) that set this command to KILLED or EXPIRED killer_agent_name = models.CharField(max_length=50, help_text='sender agent name', null=True) @@ -900,9 +924,12 @@ class AgentCmd(models.Model): return self.state == self.CMD_STATUS_CODES.CMD_SKIPPED def is_killed(self): - return self.state == self.CMD_STATUS_CODES.CMD_KILLED + return self.state == self.CMD_STATUS_CODES.CMD_EXEC_KILLED def is_expired(self): + if self.state == self.CMD_STATUS_CODES.CMD_EXPIRED: return True + # Must NOT be running + if self.is_running(): return False # return (datetime.utcnow() - self.s_deposit_time) > timedelta(seconds = self.validity_duration) elapsed_time = (datetime.utcnow().astimezone() - self.s_deposit_time) printd("elapsed_time", elapsed_time) @@ -915,6 +942,7 @@ class AgentCmd(models.Model): """ def is_timeout(self): + if self.state == self.CMD_STATUS_CODES.CMD_EXEC_TIMEOUT: return True # Must be running if not self.is_running(): return False # return (datetime.utcnow() - self.s_deposit_time) > timedelta(seconds = self.validity_duration) @@ -952,10 +980,11 @@ class AgentCmd(models.Model): # Optimization: update the related fields, but does not work, why ? ##self.save(update_fields=["state", "r_processed_time"]) - def set_as_outofdate(self): + #def set_as_outofdate(self): + def set_as_expired(self): print( f"- Set this command as expired (older than its validity duration of {self.validity_duration}s): {self}") - self.set_state_to(self.CMD_STATUS_CODES.CMD_OUTOFDATE) + self.set_state_to(self.CMD_STATUS_CODES.CMD_EXPIRED) def set_as_pending(self): self.set_state_to(self.CMD_STATUS_CODES.CMD_PENDING) @@ -966,7 +995,7 @@ class AgentCmd(models.Model): def set_as_killed_by(self, author_agent: str): printd(f"- Set command {self.name} as killed") #print(f"- Set this command as killed: {self}") - self.set_state_to(self.CMD_STATUS_CODES.CMD_KILLED, author_agent) + self.set_state_to(self.CMD_STATUS_CODES.CMD_EXEC_KILLED, author_agent) def set_as_running(self): printd(f"- Set command {self.name} as running") @@ -983,20 +1012,20 @@ class AgentCmd(models.Model): now_time = datetime.utcnow().astimezone() if result: self.set_result(result, False) - if status in (self.CMD_STATUS_CODES.CMD_RUNNING, self.CMD_STATUS_CODES.CMD_SKIPPED, self.CMD_STATUS_CODES.CMD_OUTOFDATE): + if status in (self.CMD_STATUS_CODES.CMD_RUNNING, self.CMD_STATUS_CODES.CMD_SKIPPED, self.CMD_STATUS_CODES.CMD_EXPIRED): assert self.is_pending() if status == self.CMD_STATUS_CODES.CMD_RUNNING: self.r_start_time = now_time elif status in (self.CMD_STATUS_CODES.CMD_SKIPPED): - self.end_time = now_time + self.r_end_time = now_time # EXPIRED - elif status == self.CMD_STATUS_CODES.CMD_OUTOFDATE: - self.end_time = now_time + elif status == self.CMD_STATUS_CODES.CMD_EXPIRED: + self.r_end_time = now_time self.killer_agent_name = author_agent_name - elif status in (self.CMD_STATUS_CODES.CMD_EXECUTED, self.CMD_STATUS_CODES.CMD_KILLED): + elif status in (self.CMD_STATUS_CODES.CMD_EXECUTED, self.CMD_STATUS_CODES.CMD_EXEC_KILLED): assert self.is_running() - self.end_time = now_time - if status == self.CMD_STATUS_CODES.CMD_KILLED: + self.r_end_time = now_time + if status == self.CMD_STATUS_CODES.CMD_EXEC_KILLED: self.killer_agent_name = author_agent_name # Update command status self.state = status -- libgit2 0.21.2