Commit 3df5e1de38463dc747916b253a79174d3354f2b7

Authored by Etienne Pallier
1 parent 149ba653
Exists in dev

Nouvel algo pour routine_process() (simulateur)

(on ne bloque plus à attendre la réponse)
Showing 2 changed files with 48 additions and 43 deletions   Show diff stats
README.md
... ... @@ -67,11 +67,11 @@ This software has been tested and validated with the following configurations :
67 67 --------------------------------------------------------------------------------------------
68 68 ## LAST VERSION
69 69  
70   -Date: 22/03/2019
  70 +Date: 26/03/2019
71 71  
72 72 Author: E. Pallier
73 73  
74   -VERSION: 0.20.30
  74 +VERSION: 0.20.31
75 75  
76 76 Comment: mode "test" (-t) géré (= mode simulateur)
77 77  
... ...
src/agent/Agent.py
... ... @@ -478,12 +478,11 @@ class Agent:
478 478 self.print(f"Waiting {random_waiting_sec} sec (random) before starting new iteration...")
479 479 time.sleep(random_waiting_sec)
480 480  
481   - self.load_config()
  481 + self.load_config() # only if changed
482 482  
  483 + # Update my current mode and status in DB
483 484 self.update_survey()
484 485  
485   - #if self.SIMULATOR_MODE: self.simulator_send_next_command()
486   -
487 486 self.printd("------START COMMMAND PROCESSING------")
488 487  
489 488 # Purge commandes (every N iterations, delete old commands)
... ... @@ -501,7 +500,8 @@ class Agent:
501 500 if cmd: cmd = self.command_process(cmd)
502 501  
503 502 # ROUTINE process
504   - self.routine_process()
  503 + if self.is_active(): self.routine_process()
  504 + #self.printd("I am IDLE, so I bypass the routine_process (do not send any new command)")
505 505  
506 506 self.printd("------END COMMMAND PROCESSING------")
507 507  
... ... @@ -539,56 +539,61 @@ class Agent:
539 539 at each iteration
540 540 """
541 541 self.set_status(self.STATUS_ROUTINE_PROCESS)
542   - if not self.is_active():
543   - self.printd("I am IDLE, so I bypass the routine_process (do not send any new command)")
544   - return
545 542  
546   - # TODO: normal code pour routine
547   - if not self.SIMULATOR_MODE:
548   - return
  543 + if self.SIMULATOR_MODE: self.simulator_routine_process()
549 544  
  545 +
  546 +
  547 + def simulator_routine_process(self):
550 548 """
551 549 SIMULATOR MODE ONLY
552 550 """
  551 +
  552 + SEND_A_NEW_COMMAND = False
  553 + #cmdts_is_processed = False
  554 +
  555 + # No currently running command => send a new command
553 556 if self.cmdts is None:
554 557 self.cmdts = self.simulator_get_next_command_to_send()
  558 + # No more command to send (from simulator) => return
  559 + if self.cmdts is None: return
  560 + SEND_A_NEW_COMMAND = True
555 561 else:
556   - # Previous cmd was not completed, so, make a copy of it and send it again
557   - # For this, it is enough to set primary key to None,
558   - # then the send() command below will save a NEW command
559   - #self.cmdts = copy.copy(self.cmdts)
560   - self.cmdts.id = None
561   -
562   - # No more command to send (from simulator), return
563   - if self.cmdts is None: return
564   -
565   - # 1) Send cmd (= set as pending and save)
566   - self.print(f"Send command", self.cmdts)
567   - #self.cmdts.set_as_pending()
568   - self.cmdts.send()
569   - cmdts_is_processed = False
570   - cmdts_res = None
571   -
572   - # 2) Wait for end of cmd execution
573   - #self.wait_for_execution_of_cmd(self.cmdts)
574   - while not cmdts_is_processed:
575 562 self.print(f"Waiting for end of cmd '{self.cmdts.name}' execution...")
  563 + # Update cmdts fields from DB
576 564 self.cmdts.refresh_from_db()
577   - # timeout ?
578   - if self.cmdts.is_expired(): break
579   - # skipped or killed ?
580   - if self.cmdts.is_skipped() or self.cmdts.is_killed(): break
581   - if self.cmdts.is_executed():
  565 + # Execution was not completed => set cmd to None
  566 + if self.cmdts.is_expired() or self.cmdts.is_skipped() or self.cmdts.is_killed():
  567 + self.printd("Command was not completed, so I send it again")
  568 + # The command was not completed, so, make a copy of it and send it again
  569 + # For this, it is enough to set primary key to None,
  570 + # then the send() command below will save a NEW command
  571 + #self.cmdts = copy.copy(self.cmdts)
  572 + self.cmdts.id = None
  573 + SEND_A_NEW_COMMAND = True
  574 + # cmd is executed ? => get result
  575 + elif self.cmdts.is_executed():
582 576 cmdts_res = self.cmdts.get_result()
  577 + self.print(f"Cmd executed. Result is '{cmdts_res}'")
  578 + #cmdts_is_processed = True
583 579 self.cmdts = None
584   - cmdts_is_processed = True
585   - time.sleep(1)
  580 + ''' Optimisation possible pour gagner une iteration:
  581 + self.cmdts = self.simulator_get_next_command_to_send()
  582 + # No more command to send (from simulator) => return
  583 + if self.cmdts is None: return
  584 + SEND_A_NEW_COMMAND = True
  585 + '''
  586 +
  587 + if SEND_A_NEW_COMMAND:
  588 + # Send cmd (= set as pending and save)
  589 + self.print(f"Send command", self.cmdts)
  590 + #self.cmdts.set_as_pending()
  591 + self.cmdts.send()
  592 + #cmdts_is_processed = False
  593 + #cmdts_res = None
  594 +
  595 +
586 596  
587   - # 3) Get cmd result
588   - if cmdts_is_processed:
589   - self.print(f"Cmd executed. Result is '{cmdts_res}'")
590   - else:
591   - self.printd("Command was not completed")
592 597  
593 598  
594 599 """
... ...