Commit a04e004d3e2d8941df51624bfa55f95308de1c48
1 parent
82b3357a
Exists in
dev
Fixing AgentCmd create (create_cmd_for), Fixing init of device_command to parse …
…cmd full_name into cmd name and args, Fixing agentSST methods / commands
Showing
4 changed files
with
79 additions
and
50 deletions
Show diff stats
CHANGELOG
1 | 1 | 25-05-2022 (AKo): v0.4.10.0 |
2 | 2 | - Updating Agent constructor (removing RUNNIN_IN_THREAD) |
3 | 3 | - Adding new start for pyros.py (launching agentSST) |
4 | + - Add option foreground for new-start (if set, doesn't show output of pyros start) | |
5 | + - Fixing AgentCmd create (create_cmd_for), Fixing init of device_command to parse cmd full_name into cmd name and args, Fixing agentSST methods/commands | |
4 | 6 | |
5 | 7 | 18-05-2022 (AKo): v0.4.10.0 |
6 | 8 | - Adding mandatory agents in obsconfig | ... | ... |
src/core/pyros_django/agent/Agent.py
... | ... | @@ -936,7 +936,7 @@ class Agent: |
936 | 936 | if cmd: |
937 | 937 | log.info('-'*6) |
938 | 938 | log.info('-'*6 + " RECEIVED NEW COMMAND TO PROCESS: ") |
939 | - log.info('-'*6 + cmd) | |
939 | + log.info('-'*6 + str(cmd)) | |
940 | 940 | log.info('-'*6) |
941 | 941 | |
942 | 942 | # CASE 1 - AGENT LEVEL command |
... | ... | @@ -1325,13 +1325,13 @@ class Agent: |
1325 | 1325 | real_agent_name = self._get_real_agent_name(to_agent) |
1326 | 1326 | except KeyError as e: |
1327 | 1327 | ''' |
1328 | - real_agent_name = self._get_real_agent_name(to_agent) | |
1329 | - if not real_agent_name: | |
1330 | - log.e("UNKNOWN AgentDevice ALIAS", to_agent) | |
1331 | - #self.log_e("Exception raised", e) | |
1332 | - log.e(f"=> Thus, I do not send this command '{cmd_name}'") | |
1333 | - return None | |
1334 | - return AgentCmd.create(self.name, real_agent_name, cmd_name, cmd_args) | |
1328 | + # real_agent_name = self._get_real_agent_name(to_agent) | |
1329 | + # if not real_agent_name: | |
1330 | + # log.e("UNKNOWN AgentDevice ALIAS", to_agent) | |
1331 | + # #self.log_e("Exception raised", e) | |
1332 | + # log.e(f"=> Thus, I do not send this command '{cmd_name}'") | |
1333 | + # return None | |
1334 | + return AgentCmd.create(self.name, to_agent, cmd_name, cmd_args) | |
1335 | 1335 | ''' |
1336 | 1336 | return AgentCmd( |
1337 | 1337 | sender=self.name, | ... | ... |
src/core/pyros_django/agent/AgentSST.py
... | ... | @@ -2,7 +2,8 @@ |
2 | 2 | |
3 | 3 | from pathlib import Path |
4 | 4 | import subprocess |
5 | -import sys, os, datetime | |
5 | +import sys, os | |
6 | +from datetime import datetime, timezone, timedelta | |
6 | 7 | ##import utils.Logger as L |
7 | 8 | #import threading #, multiprocessing, os |
8 | 9 | import time |
... | ... | @@ -12,7 +13,7 @@ import time |
12 | 13 | |
13 | 14 | from Agent import Agent, build_agent, log, extract_parameters |
14 | 15 | import socket |
15 | -from common.models import AgentCmd | |
16 | +from common.models import AgentCmd, AgentSurvey | |
16 | 17 | |
17 | 18 | from src.core.pyros_django.obsconfig.obsconfig_class import OBSConfig |
18 | 19 | |
... | ... | @@ -24,8 +25,8 @@ class AgentSST(Agent): |
24 | 25 | subprocess_dict = {} |
25 | 26 | |
26 | 27 | AGENT_SPECIFIC_COMMANDS = [ |
27 | - "KILL_AGENT", | |
28 | - "RESTART_AGENT" | |
28 | + "kill_agent", | |
29 | + "restart_agent" | |
29 | 30 | ] |
30 | 31 | |
31 | 32 | def __init__(self, name:str=None,sim_computer=None): |
... | ... | @@ -101,7 +102,8 @@ class AgentSST(Agent): |
101 | 102 | if os.path.exists(protocol_folder_abs_path + os.sep + protocol_script_name): |
102 | 103 | cmd = self.VENV_PYTHON +" "+ protocol_folder_abs_path + os.sep + protocol_script_name |
103 | 104 | |
104 | - process = subprocess.Popen(f"{cmd}", shell=True, stdout=subprocess.DEVNULL,stderr=subprocess.STDOUT) | |
105 | + # process = subprocess.Popen(f"{cmd}", shell=True, stdout=subprocess.DEVNULL,stderr=subprocess.STDOUT) | |
106 | + process = subprocess.Popen(f"{cmd}", shell=True) | |
105 | 107 | self.subprocess_dict[agent] = process |
106 | 108 | log.info(f"Start agent {agent} with cmd {cmd}") |
107 | 109 | |
... | ... | @@ -115,54 +117,78 @@ class AgentSST(Agent): |
115 | 117 | self.start_agents(agent_name) |
116 | 118 | |
117 | 119 | |
118 | - def cmd_kill_agent(self, cmd:AgentCmd, agent:str)->None: | |
120 | + def kill_agent(self, *args)->None: | |
121 | + agent = args[0] | |
119 | 122 | if agent in self.subprocess_dict.keys(): |
120 | - self.send_cmd_to(agent,"do_exit") | |
123 | + cmd = self.send_cmd_to(agent,"do_exit") | |
124 | + return cmd | |
121 | 125 | |
122 | - def cmd_restart_agent(self, cmd:AgentCmd ,agent:str)->None: | |
126 | + def restart_agent(self, *args)->None: | |
127 | + agent = args[0] | |
128 | + self.kill_agent(agent) | |
123 | 129 | self.start_agent(agent) |
124 | - if agent in self.subprocess_dict.keys(): | |
125 | - cmd.set_result(f"Agent {agent} restarted") | |
126 | - cmd.set_as_processed() | |
127 | - else: | |
128 | - cmd.set_result(f"Agent {agent} failed to restart") | |
129 | - log.debug(f"Agent {agent} failed to restart") | |
130 | + # if agent in self.subprocess_dict.keys(): | |
131 | + # cmd.set_result(f"Agent {agent} restarted") | |
132 | + # cmd.set_as_processed() | |
133 | + # else: | |
134 | + # cmd.set_result(f"Agent {agent} failed to restart") | |
135 | + # log.debug(f"Agent {agent} failed to restart") | |
130 | 136 | |
131 | - def force_kill_agent(self, cmd:AgentCmd, agent:str)->None: | |
137 | + def force_kill_agent(self, *args)->None: | |
132 | 138 | if agent in self.subprocess_dict.get(agent) is not None: |
133 | 139 | process = self.subprocess_dict.get(agent) |
134 | 140 | process.terminate() |
141 | + process.wait() | |
135 | 142 | if process.poll() is not None: |
136 | - cmd.set_as_processed() | |
143 | + pass | |
144 | + #cmd.set_as_processed() | |
137 | 145 | else: |
138 | 146 | return None |
139 | - | |
147 | + | |
148 | + def do_things_before_exit(self,abort_cmd_sender): | |
149 | + kill_agent_commands = {} | |
150 | + for agent in self.subprocess_dict.keys(): | |
151 | + cmd = self.kill_agent(agent) | |
152 | + kill_agent_commands[agent] = cmd | |
153 | + for agent in kill_agent_commands: | |
154 | + if self.subprocess_dict[agent].poll() is None: | |
155 | + self.force_kill_agent(kill_agent_commands[agent],agent) | |
156 | + | |
140 | 157 | def routine_process_before_body(self): |
141 | - try: | |
142 | - now_time = datetime.datetime.utcnow() | |
143 | - last_running_commands = AgentCmd.get_commands_sent_by_agent("AgentSST").filter(state="CMD_RUNNING") | |
144 | - | |
145 | - for cmd in last_running_commands: | |
146 | - last_running_cmd, agent = cmd.full_name.split(" ")[:1] | |
147 | - if last_running_cmd == "KILL_AGENT" and cmd.is_expired(): | |
148 | - self.force_killed_agent(agent) | |
149 | - next_cmd = self._get_next_valid_and_not_running_command() | |
150 | - if next_cmd is not None: | |
151 | - cmd, agent = next_cmd.full_name.split(" ")[:1] | |
152 | - if cmd == "KILL_AGENT": | |
153 | - self.cmd_kill_agent(next_cmd,agent) | |
154 | - elif cmd == "RESTART_AGENT": | |
155 | - self.cmd_restart_agent(next_cmd,agent) | |
156 | - log.info("Check status of process") | |
157 | - for agent in self.subprocess_dict: | |
158 | - proc = self.subprocess_dict.get(agent) | |
158 | + now_time = datetime.now(timezone.utc) | |
159 | + last_running_commands = AgentCmd.get_commands_sent_by_agent("AgentSST").filter(state="CMD_RUNNING") | |
160 | + | |
161 | + for cmd in last_running_commands: | |
162 | + last_running_cmd, agent = cmd.full_name.split(" ")[:1] | |
163 | + if last_running_cmd == "KILL_AGENT" and cmd.is_expired(): | |
164 | + self.force_killed_agent(agent) | |
165 | + # chekcing status of agent if they are timeout | |
166 | + | |
167 | + for agent in self.subprocess_dict.keys(): | |
168 | + agent_survey = AgentSurvey.objects.get(name=agent) | |
169 | + validity_duration = agent_survey.validity_duration | |
170 | + last_update_from_agent = agent_survey.updated | |
171 | + validity_duration_timedelta = timedelta(seconds=validity_duration) | |
172 | + timeout_datetime = last_update_from_agent + validity_duration_timedelta | |
173 | + timeout_datetime = timeout_datetime.replace(tzinfo=timezone.utc) | |
174 | + # if agent is timeout, restart it | |
175 | + if timeout_datetime < now_time: | |
176 | + # restart agent | |
177 | + self.send_cmd_to("AgentSST","restart_agent", agent) | |
178 | + | |
179 | + | |
180 | + # next_cmd = self._get_next_valid_and_not_running_command() | |
181 | + # if next_cmd is not None: | |
182 | + # cmd, agent = next_cmd.full_name.split(" ")[:1] | |
183 | + # if cmd == "KILL_AGENT": | |
184 | + # self.cmd_kill_agent(next_cmd,agent) | |
185 | + # elif cmd == "RESTART_AGENT": | |
186 | + # self.cmd_restart_agent(next_cmd,agent) | |
187 | + log.info("Check status of process") | |
188 | + for agent in self.subprocess_dict: | |
189 | + proc = self.subprocess_dict.get(agent) | |
159 | 190 | log.info(f"{agent} poll result is {proc.poll()}") |
160 | - except KeyboardInterrupt: | |
161 | - | |
162 | - for process in self.subprocess_dict.values(): | |
163 | - print(process) | |
164 | - process.kill() | |
165 | - exit(0) | |
191 | + | |
166 | 192 | |
167 | 193 | |
168 | 194 | if __name__ == "__main__": | ... | ... |
src/device_controller/abstract_component/device_controller.py
... | ... | @@ -113,7 +113,8 @@ class DeviceCmd: |
113 | 113 | |
114 | 114 | def __init__(self, cmd_full_name:str, dev_comp_type:str=None, cmd_args:str=None): |
115 | 115 | self.full_name = cmd_full_name |
116 | - self.name = cmd_full_name | |
116 | + dev_comp_type,cmd_name,cmd_args = self.get_full_name_parts() | |
117 | + self.name = cmd_name | |
117 | 118 | self.devtype = dev_comp_type |
118 | 119 | self.args = cmd_args |
119 | 120 | if self.is_generic(): | ... | ... |