From 3df5e1de38463dc747916b253a79174d3354f2b7 Mon Sep 17 00:00:00 2001 From: Etienne Pallier Date: Tue, 26 Mar 2019 18:39:10 +0100 Subject: [PATCH] Nouvel algo pour routine_process() (simulateur) (on ne bloque plus à attendre la réponse) --- README.md | 4 ++-- src/agent/Agent.py | 87 ++++++++++++++++++++++++++++++++++++++++++++++----------------------------------------- 2 files changed, 48 insertions(+), 43 deletions(-) diff --git a/README.md b/README.md index ef227f9..afad191 100644 --- a/README.md +++ b/README.md @@ -67,11 +67,11 @@ This software has been tested and validated with the following configurations : -------------------------------------------------------------------------------------------- ## LAST VERSION -Date: 22/03/2019 +Date: 26/03/2019 Author: E. Pallier -VERSION: 0.20.30 +VERSION: 0.20.31 Comment: mode "test" (-t) géré (= mode simulateur) diff --git a/src/agent/Agent.py b/src/agent/Agent.py index a26fa0e..f62951c 100755 --- a/src/agent/Agent.py +++ b/src/agent/Agent.py @@ -478,12 +478,11 @@ class Agent: self.print(f"Waiting {random_waiting_sec} sec (random) before starting new iteration...") time.sleep(random_waiting_sec) - self.load_config() + self.load_config() # only if changed + # Update my current mode and status in DB self.update_survey() - #if self.SIMULATOR_MODE: self.simulator_send_next_command() - self.printd("------START COMMMAND PROCESSING------") # Purge commandes (every N iterations, delete old commands) @@ -501,7 +500,8 @@ class Agent: if cmd: cmd = self.command_process(cmd) # ROUTINE process - self.routine_process() + if self.is_active(): self.routine_process() + #self.printd("I am IDLE, so I bypass the routine_process (do not send any new command)") self.printd("------END COMMMAND PROCESSING------") @@ -539,56 +539,61 @@ class Agent: at each iteration """ self.set_status(self.STATUS_ROUTINE_PROCESS) - if not self.is_active(): - self.printd("I am IDLE, so I bypass the routine_process (do not send any new command)") - return - # TODO: normal code pour routine - if not self.SIMULATOR_MODE: - return + if self.SIMULATOR_MODE: self.simulator_routine_process() + + + def simulator_routine_process(self): """ SIMULATOR MODE ONLY """ + + SEND_A_NEW_COMMAND = False + #cmdts_is_processed = False + + # No currently running command => send a new command if self.cmdts is None: self.cmdts = self.simulator_get_next_command_to_send() + # No more command to send (from simulator) => return + if self.cmdts is None: return + SEND_A_NEW_COMMAND = True else: - # Previous cmd was not completed, so, make a copy of it and send it again - # For this, it is enough to set primary key to None, - # then the send() command below will save a NEW command - #self.cmdts = copy.copy(self.cmdts) - self.cmdts.id = None - - # No more command to send (from simulator), return - if self.cmdts is None: return - - # 1) Send cmd (= set as pending and save) - self.print(f"Send command", self.cmdts) - #self.cmdts.set_as_pending() - self.cmdts.send() - cmdts_is_processed = False - cmdts_res = None - - # 2) Wait for end of cmd execution - #self.wait_for_execution_of_cmd(self.cmdts) - while not cmdts_is_processed: self.print(f"Waiting for end of cmd '{self.cmdts.name}' execution...") + # Update cmdts fields from DB self.cmdts.refresh_from_db() - # timeout ? - if self.cmdts.is_expired(): break - # skipped or killed ? - if self.cmdts.is_skipped() or self.cmdts.is_killed(): break - if self.cmdts.is_executed(): + # Execution was not completed => set cmd to None + if self.cmdts.is_expired() or self.cmdts.is_skipped() or self.cmdts.is_killed(): + self.printd("Command was not completed, so I send it again") + # The command was not completed, so, make a copy of it and send it again + # For this, it is enough to set primary key to None, + # then the send() command below will save a NEW command + #self.cmdts = copy.copy(self.cmdts) + self.cmdts.id = None + SEND_A_NEW_COMMAND = True + # cmd is executed ? => get result + elif self.cmdts.is_executed(): cmdts_res = self.cmdts.get_result() + self.print(f"Cmd executed. Result is '{cmdts_res}'") + #cmdts_is_processed = True self.cmdts = None - cmdts_is_processed = True - time.sleep(1) + ''' Optimisation possible pour gagner une iteration: + self.cmdts = self.simulator_get_next_command_to_send() + # No more command to send (from simulator) => return + if self.cmdts is None: return + SEND_A_NEW_COMMAND = True + ''' + + if SEND_A_NEW_COMMAND: + # Send cmd (= set as pending and save) + self.print(f"Send command", self.cmdts) + #self.cmdts.set_as_pending() + self.cmdts.send() + #cmdts_is_processed = False + #cmdts_res = None + + - # 3) Get cmd result - if cmdts_is_processed: - self.print(f"Cmd executed. Result is '{cmdts_res}'") - else: - self.printd("Command was not completed") """ -- libgit2 0.21.2