Commit 997d4b8e33907fdaf5ba8c6f81c20c6936ea7846

Authored by Etienne Pallier
1 parent 24aaf714
Exists in dev

AgentA et AgentX peuvent s'executer en meme temps sans conflit

(les démarrer dans 2 terminaux différents)
Showing 3 changed files with 172 additions and 2 deletions   Show diff stats
pyros.py
... ... @@ -25,6 +25,7 @@ INIT_FIXTURE = "initial_fixture.json"
25 25 AGENTS = {
26 26 #"agentX" : "majordome",
27 27 "agentX" : "agent",
  28 + "agentA" : "agent",
28 29 "webserver" : "webserver",
29 30 "monitoring" : "monitoring",
30 31 "majordome" : "majordome",
... ...
src/agent/AgentA.py 0 → 100644
... ... @@ -0,0 +1,161 @@
  1 +
  2 +import utils.Logger as L
  3 +import threading #, multiprocessing, os
  4 +import time
  5 +
  6 +from django.db import transaction
  7 +from common.models import Command
  8 +
  9 +from .Agent import Agent
  10 +
  11 +
  12 +
  13 +log = L.setupLogger("AgentXTaskLogger", "AgentX")
  14 +
  15 +
  16 +
  17 +class AgentA(Agent):
  18 +
  19 + """
  20 + How to run this agent thread_exec_specific_cmd() method ?
  21 + - True = inside a Thread (cannot be killed, must be asked to stop, and inadequate for computation)
  22 + - False = inside a Process
  23 + If thread, displays :
  24 + >>>>> Thread: starting execution of command specific1
  25 + >>>>> Thread: PID: 2695, Process Name: MainProcess, Thread Name: Thread-1
  26 + ...
  27 + >>>>> Thread: starting execution of command specific2
  28 + >>>>> Thread: PID: 2695, Process Name: MainProcess, Thread Name: Thread-2
  29 + ...
  30 + >>>>> Thread: starting execution of command specific3
  31 + >>>>> Thread: PID: 2695, Process Name: MainProcess, Thread Name: Thread-3
  32 + If process, displays :
  33 + >>>>> Thread: starting execution of command specific1
  34 + >>>>> Thread: PID: 2687, Process Name: Process-1, Thread Name: MainThread
  35 + ...
  36 + >>>>> Thread: starting execution of command specific2
  37 + >>>>> Thread: PID: 2689, Process Name: Process-2, Thread Name: MainThread
  38 + ...
  39 + >>>>> Thread: starting execution of command specific3
  40 + >>>>> Thread: PID: 2690, Process Name: Process-3, Thread Name: MainThread
  41 + """
  42 + RUN_IN_THREAD = True
  43 + #RUN_IN_THREAD = False
  44 +
  45 +
  46 + """
  47 + =================================================================
  48 + FUNCTIONS RUN INSIDE MAIN THREAD
  49 + =================================================================
  50 + """
  51 +
  52 + # @override
  53 + def __init__(self, name:str=None, config_filename=None):
  54 + if name is None: name = self.__class__.__name__
  55 + super().__init__(name, config_filename, self.RUN_IN_THREAD)
  56 +
  57 + # @override
  58 + def init(self):
  59 + super().init()
  60 + # --- Set the mode according the startmode value
  61 + ##agent_alias = self.__class__.__name__
  62 + ##self.set_mode_from_config(agent_alias)
  63 +
  64 + '''
  65 + # @override
  66 + def load_config(self):
  67 + super().load_config()
  68 + '''
  69 +
  70 + '''
  71 + # @override
  72 + def update_survey(self):
  73 + super().update_survey()
  74 + '''
  75 +
  76 + '''
  77 + # @override
  78 + def get_next_command(self):
  79 + return super().get_next_command()
  80 + '''
  81 +
  82 + # @override
  83 + def do_log(self):
  84 + super().do_log()
  85 +
  86 +
  87 +
  88 + """
  89 + =================================================================
  90 + FUNCTIONS RUN INSIDE A SUB-THREAD (OR A PROCESS) (thread_*())
  91 + =================================================================
  92 + """
  93 +
  94 + # Define your own command step(s) here
  95 + def cmd_step1(self, step:int):
  96 + cmd = self._current_specific_cmd
  97 + cmd.result = f"in step #{step}/{self._thread_total_steps_number}"
  98 + cmd.save()
  99 + """
  100 + if self.RUN_IN_THREAD:
  101 + print("(save from thread)")
  102 + cmd.save()
  103 + else:
  104 + #@transaction.atomic
  105 + print("(save from process)")
  106 + with transaction.atomic():
  107 + cmd.save()
  108 + #Command.objects.select_for_update()
  109 + """
  110 +
  111 + def cmd_step2(self, step:int):
  112 + self.cmd_step1(step)
  113 + def cmd_step3(self, step:int):
  114 + self.cmd_step1(step)
  115 + def cmd_step4(self, step:int):
  116 + self.cmd_step1(step)
  117 +
  118 + """
  119 + # @override
  120 + def thread_exec_specific_cmd_step(self, step:int, sleep_time:float=1.0):
  121 + self.thread_stop_if_asked()
  122 + cmd = self._current_specific_cmd
  123 + print(f">>>>> Thread (cmd {cmd.name}): step #{step}/5")
  124 + self.sleep(sleep_time)
  125 + """
  126 +
  127 + '''
  128 + # @override
  129 + def exec_specific_cmd_start(self, cmd:Command, from_thread=True):
  130 + super().exec_specific_cmd_start(cmd, from_thread)
  131 + '''
  132 +
  133 +
  134 + # @override
  135 + def thread_exec_specific_cmd_main(self):
  136 + # This is optional
  137 + self.thread_set_total_steps_number(5)
  138 +
  139 + # HERE, write your own scenario
  140 +
  141 + # scenario OK
  142 + self.thread_exec_specific_cmd_step(1, self.cmd_step1, 1)
  143 + self.thread_exec_specific_cmd_step(2, self.cmd_step2, 3)
  144 + self.thread_exec_specific_cmd_step(3, self.cmd_step1, 5)
  145 + self.thread_exec_specific_cmd_step(4, self.cmd_step3, 10)
  146 + self.thread_exec_specific_cmd_step(5, self.cmd_step1, 4)
  147 + # ... as many as you need
  148 +
  149 + """ autre scenario
  150 + self.thread_exec_specific_cmd_step(1, self.cmd_step1, 1)
  151 + self.thread_exec_specific_cmd_step(2, self.cmd_step2, 2)
  152 + self.thread_exec_specific_cmd_step(3, self.cmd_step1, 2)
  153 + self.thread_exec_specific_cmd_step(4, self.cmd_step3, 2)
  154 + self.thread_exec_specific_cmd_step(5, self.cmd_step1, 3)
  155 + """
  156 +
  157 + '''
  158 + # @override
  159 + def exec_specific_cmd_end(self, cmd:Command, from_thread=True):
  160 + super().exec_specific_cmd_end(cmd, from_thread)
  161 + '''
... ...
start_agent.py
... ... @@ -7,6 +7,7 @@ from django.conf import settings as djangosettings
7 7  
8 8 AGENTS = {
9 9 "agentX" : "AgentX",
  10 + "agentA" : "AgentA",
10 11 "webserver" : "webserver",
11 12 "monitoring" : "monitoring",
12 13 "majordome" : "majordome",
... ... @@ -92,11 +93,18 @@ if agent_name == "monitoring":
92 93 Monitoring().run()
93 94 sys.exit(0)
94 95  
  96 +if agent_name == "agentA":
  97 + from src.agent.AgentA import AgentA
  98 + agentA = AgentA(name="agentA", config_filename=configfile)
  99 + # Run agent without actual commands sent to devices (FOR_REAL=False)
  100 + agentA.run(FOR_REAL=True)
  101 + sys.exit(0)
  102 +
95 103 # Default agent is AgentX
96 104  
97 105 from src.agent.AgentX import AgentX
98 106 # AgentX().run(FOR_REAL=False)
99   -agentx = AgentX(name="agentX", config_filename=configfile)
  107 +agentX = AgentX(name="agentX", config_filename=configfile)
100 108  
101 109 # Run agent without actual commands sent to devices (FOR_REAL=False)
102   -agentx.run(FOR_REAL=True)
  110 +agentX.run(FOR_REAL=True)
... ...