diff options
author | Christian Breunig <christian@breunig.cc> | 2024-01-06 22:06:09 +0100 |
---|---|---|
committer | Mergify <37929162+mergify[bot]@users.noreply.github.com> | 2024-01-07 06:13:12 +0000 |
commit | 368ff14787d6a021d105055f3dd5e0e0870b0dfc (patch) | |
tree | c2a04db24e0fb58a79ef9e6da9369b50bcecbe17 /python | |
parent | de5ca2100d6bd45d3e6d522510aec1e7af8f0599 (diff) | |
download | vyos-1x-368ff14787d6a021d105055f3dd5e0e0870b0dfc.tar.gz vyos-1x-368ff14787d6a021d105055f3dd5e0e0870b0dfc.zip |
T5195: add timeout argument to process_named_running()
Smoketests heavily rely on process_named_running() so in order to "relax"
system constraints during a test we will add a timeout of 10 seconds for
every testcase provided by base_interfaces_test.py
(cherry picked from commit ad9bdfc248cf47b3361bd0e5d7371d56131160a0)
Diffstat (limited to 'python')
-rw-r--r-- | python/vyos/utils/process.py | 27 |
1 files changed, 21 insertions, 6 deletions
diff --git a/python/vyos/utils/process.py b/python/vyos/utils/process.py index e09c7d86d..cd58b4be2 100644 --- a/python/vyos/utils/process.py +++ b/python/vyos/utils/process.py @@ -204,17 +204,32 @@ def process_running(pid_file): pid = f.read().strip() return pid_exists(int(pid)) -def process_named_running(name, cmdline: str=None): +def process_named_running(name, cmdline: str=None, timeout=0): """ Checks if process with given name is running and returns its PID. If Process is not running, return None """ from psutil import process_iter - for p in process_iter(['name', 'pid', 'cmdline']): - if cmdline: - if p.info['name'] == name and cmdline in p.info['cmdline']: + def check_process(name, cmdline): + for p in process_iter(['name', 'pid', 'cmdline']): + if cmdline: + if name in p.info['name'] and cmdline in p.info['cmdline']: + return p.info['pid'] + elif name in p.info['name']: return p.info['pid'] - elif p.info['name'] == name: - return p.info['pid'] + return None + if timeout: + import time + time_expire = time.time() + timeout + while True: + tmp = check_process(name, cmdline) + if not tmp: + if time.time() > time_expire: + break + time.sleep(0.100) # wait 250ms + continue + return tmp + else: + return check_process(name, cmdline) return None def is_systemd_service_active(service): |