diff options
Diffstat (limited to 'tools')
-rw-r--r-- | tools/tasks.py | 33 | ||||
-rw-r--r-- | tools/teststepstools.py | 17 |
2 files changed, 47 insertions, 3 deletions
diff --git a/tools/tasks.py b/tools/tasks.py index 4179291f..18f4d712 100644 --- a/tools/tasks.py +++ b/tools/tasks.py @@ -114,6 +114,16 @@ def run_task(cmd, logger, msg=None, check_error=False): return ('\n'.join(sout.decode(my_encoding).strip() for sout in stdout), ('\n'.join(sout.decode(my_encoding).strip() for sout in stderr))) +def update_pids(pid): + """update list of running pids, so they can be terminated at the end + """ + try: + pids = settings.getValue('_EXECUTED_PIDS') + pids.append(pid) + except AttributeError: + pids = [pid] + settings.setValue('_EXECUTED_PIDS', pids) + def run_background_task(cmd, logger, msg): """Run task in background and log when started. @@ -132,6 +142,8 @@ def run_background_task(cmd, logger, msg): proc = subprocess.Popen(map(os.path.expanduser, cmd), stdout=_get_stdout(), bufsize=0) + update_pids(proc.pid) + return proc.pid @@ -174,14 +186,13 @@ def terminate_task_subtree(pid, signal='-15', sleep=10, logger=None): :param logger: Logger to write details to """ try: - output = subprocess.check_output("pgrep -P " + str(pid), shell=True).decode().rstrip('\n') + children = subprocess.check_output("pgrep -P " + str(pid), shell=True).decode().rstrip('\n').split() except subprocess.CalledProcessError: - output = "" + children = [] terminate_task(pid, signal, sleep, logger) # just for case children were kept alive - children = output.split('\n') for child in children: terminate_task(child, signal, sleep, logger) @@ -208,6 +219,22 @@ def terminate_task(pid, signal='-15', sleep=10, logger=None): if signal.lstrip('-').upper() not in ('9', 'KILL', 'SIGKILL') and systeminfo.pid_isalive(pid): terminate_task(pid, '-9', sleep, logger) + pids = settings.getValue('_EXECUTED_PIDS') + if pid in pids: + pids.remove(pid) + settings.setValue('_EXECUTED_PIDS', pids) + +def terminate_all_tasks(logger): + """Terminate all processes executed by vsperf, just for case they were not + terminated by standard means. + """ + pids = settings.getValue('_EXECUTED_PIDS') + if pids: + logger.debug('Following processes will be terminated: %s', pids) + for pid in pids: + terminate_task_subtree(pid, logger=logger) + settings.setValue('_EXECUTED_PIDS', []) + class Process(object): """Control an instance of a long-running process. diff --git a/tools/teststepstools.py b/tools/teststepstools.py index 639e3437..33db8f79 100644 --- a/tools/teststepstools.py +++ b/tools/teststepstools.py @@ -19,6 +19,7 @@ import logging import subprocess import locale from tools.functions import filter_output +from tools.tasks import run_background_task _LOGGER = logging.getLogger(__name__) @@ -102,3 +103,19 @@ class TestStepsTools(object): """ validate result of shell `command' execution """ return result is not None + + @staticmethod + def Exec_Shell_Background(command): + """ Execute a shell `command' at the background and return its PID id + """ + try: + pid = run_background_task(command.split(), _LOGGER, "Background task: {}".format(command)) + return pid + except OSError: + return None + + @staticmethod + def validate_Exec_Shell_Background(result, dummy_command, dummy_regex=None): + """ validate result of shell `command' execution on the background + """ + return result is not None |