mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-25 17:36:18 -05:00
Checking that process is running during process_runner.
This commit is contained in:
parent
f124fd3e5e
commit
b16b8ce33a
2 changed files with 17 additions and 2 deletions
|
|
@ -42,6 +42,15 @@ def which(*possible_paths: str, warn: bool = False) -> Optional[Path]:
|
|||
return found.absolute()
|
||||
|
||||
|
||||
def pid_is_running(pid: int) -> bool:
|
||||
"""Return True if a process with PID exists."""
|
||||
try:
|
||||
os.kill(pid, 0)
|
||||
return True
|
||||
except OSError:
|
||||
return False
|
||||
|
||||
|
||||
# Admin
|
||||
SUDO_BIN = which('sudo', '/usr/bin/sudo')
|
||||
NMCLI_BIN = which('nmcli', '/usr/bin/nmcli')
|
||||
|
|
|
|||
|
|
@ -25,6 +25,7 @@ from sqlalchemy.sql import Delete
|
|||
|
||||
from wrolpi import flags
|
||||
from wrolpi.api_utils import api_app
|
||||
from wrolpi.cmd import pid_is_running
|
||||
from wrolpi.common import Base, ModelHelper, logger, wrol_mode_check, zig_zag, ConfigFile, WROLPI_CONFIG, \
|
||||
limit_concurrent, wrol_mode_enabled
|
||||
from wrolpi.dates import TZDateTime, now, Seconds
|
||||
|
|
@ -245,7 +246,8 @@ class Downloader:
|
|||
|
||||
Global timeout takes precedence over the timeout argument, unless it is 0. (Smaller global timeout wins)
|
||||
"""
|
||||
logger.debug(f'{self} launching download process with args: {" ".join(list(map(str, cmd)))}')
|
||||
str_cmd = " ".join([str(i) for i in cmd])
|
||||
logger.debug(f'{self} launching download process with args: {str_cmd}')
|
||||
start = now()
|
||||
proc = await asyncio.create_subprocess_exec(*cmd,
|
||||
stdout=asyncio.subprocess.PIPE,
|
||||
|
|
@ -276,13 +278,17 @@ class Downloader:
|
|||
stdout, stderr = await proc.communicate()
|
||||
break
|
||||
|
||||
if not pid_is_running(pid):
|
||||
logger.debug(f'Process is no longer running! {str_cmd}')
|
||||
break
|
||||
|
||||
elapsed = (now() - start).total_seconds()
|
||||
if timeout and elapsed > timeout:
|
||||
logger.warning(f'Download has exceeded its timeout {elapsed=}')
|
||||
download_manager.kill_download(download_id)
|
||||
|
||||
if download_manager.download_is_killed(download_id):
|
||||
logger.warning(f'Killing download {pid=}, {elapsed} seconds elapsed (timeout was not exceeded).')
|
||||
logger.warning(f'Killing download {pid=}, {elapsed} seconds elapsed.')
|
||||
proc.kill()
|
||||
break
|
||||
except Exception as e:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue