Commit 93b402b7ab70169e5c7ea4ac74f5c6b01447b59b

Authored by Etienne Pallier
1 parent 75cb805d
Exists in dev

Added management of CTRL-C interruption (kills thread before exiting)

Agent complet avec specific_process() exécuté au choix dans thread ou
process
(il suffit de mettre la constante RUN_IN_THREAD de AgentX à False ou
True)
(les commandes "abort" et "exit" sont correctement traitées dans les 2
cas)
(NB: dans les 2 cas, la commande "exit" fait d'abord un "abort")
Showing 3 changed files with 60 additions and 43 deletions   Show diff stats
README.md
... ... @@ -71,9 +71,10 @@ Date: 11/03/2019
71 71  
72 72 Author: E. Pallier
73 73  
74   -VERSION: 0.20.13
  74 +VERSION: 0.20.14
75 75  
76   -Comment:
  76 +Comment:
  77 + Added management of CTRL-C interruption (kills thread before exiting)
77 78 Agent complet avec specific_process() exécuté au choix dans thread ou process
78 79 (il suffit de mettre la constante RUN_IN_THREAD de AgentX à False ou True)
79 80 (les commandes "abort" et "exit" sont correctement traitées dans les 2 cas)
... ...
src/agent/Agent.py
... ... @@ -371,53 +371,61 @@ class Agent:
371 371 # Main loop
372 372 while True:
373 373  
374   - print()
375   - print()
376   - #print("-"*80)
377   - print("-"*20, f"MAIN LOOP ITERATION {self._iter_num} (START)", "-"*20)
378   - self.set_status(self.STATUS_MAIN_LOOP)
379   - self.show_mode_and_status()
  374 + try:
380 375  
381   - self.load_config()
  376 + print()
  377 + print()
  378 + #print("-"*80)
  379 + print("-"*20, f"MAIN LOOP ITERATION {self._iter_num} (START)", "-"*20)
  380 + self.set_status(self.STATUS_MAIN_LOOP)
  381 + self.show_mode_and_status()
382 382  
383   - self.update_survey()
  383 + self.load_config()
384 384  
385   - if self.SIMULATOR_MODE: self.simulator_send_command_to_myself()
  385 + self.update_survey()
386 386  
387   - # generic cmd in json format
388   - print("------START COMMMAND PROCESSING------")
389   - cmd = self.get_next_valid_command()
  387 + if self.SIMULATOR_MODE: self.simulator_send_command_to_myself()
390 388  
391   - if cmd: cmd = self.general_process(cmd)
392   - '''
393   - if self.config.majordome_state == "STOP":
394   - break
395   - if self.config.majordome_state == "RESTART":
396   - self.config.majordome_restarted = True
397   - self.closing_mode = self.config.majordome_state
398   - self.config.majordome_state = "RUNNING"
399   - self.config.save()
400   - '''
  389 + # generic cmd in json format
  390 + print("------START COMMMAND PROCESSING------")
  391 + cmd = self.get_next_valid_command()
401 392  
402   - self.routine_process()
  393 + if cmd: cmd = self.general_process(cmd)
  394 + '''
  395 + if self.config.majordome_state == "STOP":
  396 + break
  397 + if self.config.majordome_state == "RESTART":
  398 + self.config.majordome_restarted = True
  399 + self.closing_mode = self.config.majordome_state
  400 + self.config.majordome_state = "RUNNING"
  401 + self.config.save()
  402 + '''
403 403  
404   - # Sub-level loop (only if ACTIVE)
405   - if self.is_active():
406   - if cmd: self.specific_process(cmd)
407   - print("------END COMMMAND PROCESSING------")
  404 + self.routine_process()
408 405  
409   - # Every N iterations, delete old commands
410   - N=3
411   - if ((self._iter_num-1) % N) == 0: Command.purge_old_commands_for_agent(self.name)
  406 + # Sub-level loop (only if ACTIVE)
  407 + if self.is_active():
  408 + if cmd: self.specific_process(cmd)
  409 + print("------END COMMMAND PROCESSING------")
412 410  
413   - self.waitfor(self.mainloop_waittime)
  411 + # Every N iterations, delete old commands
  412 + N=3
  413 + if ((self._iter_num-1) % N) == 0: Command.purge_old_commands_for_agent(self.name)
414 414  
415   - print("-"*20, "MAIN LOOP ITERATION (END)", "-"*20)
416   - #print("-"*80)
  415 + self.waitfor(self.mainloop_waittime)
417 416  
418   - #self.do_log(LOG_DEBUG, "Ending main loop iteration")
  417 + print("-"*20, "MAIN LOOP ITERATION (END)", "-"*20)
  418 + #print("-"*80)
419 419  
420   - self._iter_num += 1
  420 + #self.do_log(LOG_DEBUG, "Ending main loop iteration")
  421 +
  422 + self._iter_num += 1
  423 +
  424 + except KeyboardInterrupt:
  425 + # In case of CTRL-C, kill the current thread (process) before dying (in error)
  426 + print("CTRL-C Interrupted, I kill the current thread (process) before exiting")
  427 + self.kill_running_specific_cmd_if_exists()
  428 + exit(1)
421 429  
422 430  
423 431  
... ... @@ -577,10 +585,14 @@ class Agent:
577 585 def update_survey(self):
578 586 print("Updating the survey database table...")
579 587 print("- fetching table line for agent", self.name)
580   - self._agent_survey = AgentSurvey.objects.get(name=self.name)
581   - self._agent_survey.mode = self.mode
582   - self._agent_survey.status = self.status
583   - self._agent_survey.save()
  588 + # only necessary when using process (not necessary with threads)
  589 + with transaction.atomic():
  590 + self._agent_survey = AgentSurvey.objects.get(name=self.name)
  591 + self._agent_survey.mode = self.mode
  592 + self._agent_survey.status = self.status
  593 + # Optimization: update only the related fields
  594 + #self._agent_survey.save()
  595 + self._agent_survey.save(update_fields=["mode", "status"])
584 596  
585 597  
586 598 def simulator_send_command_to_myself(self):
... ... @@ -809,7 +821,10 @@ class Agent:
809 821 self.exec_specific_cmd_main(cmd)
810 822 # thread execution tearing down
811 823 self.exec_specific_cmd_end(cmd)
812   -
  824 + '''
  825 + except TimeoutError:
  826 + pass
  827 + '''
813 828  
814 829 def exec_specific_cmd_start(self, cmd:Command):
815 830 """ specific command execution setting up """
... ...
src/agent/AgentX.py
... ... @@ -16,7 +16,7 @@ log = L.setupLogger("AgentXTaskLogger", "AgentX")
16 16  
17 17 class AgentX(Agent):
18 18  
19   - """
  19 + """
20 20 How to run this agent exec_specific_cmd() method ?
21 21 - True = inside a Thread (cannot be killed, must be asked to stop, and inadequate for computation)
22 22 - False = inside a Process
... ... @@ -40,6 +40,7 @@ class AgentX(Agent):
40 40 >>>>> Thread: PID: 2690, Process Name: Process-3, Thread Name: MainThread
41 41 """
42 42 RUN_IN_THREAD = True
  43 + #RUN_IN_THREAD = False
43 44  
44 45  
45 46 """
... ...