1
mirror of https://github.com/home-assistant/core synced 2024-10-01 05:30:36 +02:00

Single process restart fixes (#2118)

* Ignore permission errors on setpgid.

When launched in a docker container we got a permission denied error
from setpgid.

* Don't fail if we find our own pidfile.

When we restart using exec we are running a new instance of home-assistant with
the same process id so we shouldn't be surprised to find an existing pidfile in
that case.

* Allow restart to work when started as python -m homeassistant.

When we are started with `python -m homeassistant`, the restart command line
becomes `python /path/to/hass/homeassistant/__main__.py`. But in that case the
python path includes `/path/to/hass/homeassistant` instead of `/path/to/hass`
and we fail on the first import.

Fix this by recognizing `/__main__.py` as part of the first argument and
injecting the proper path as PYTHONPATH environment before we start the new
home-assistant instance.
This commit is contained in:
Jan Harkes 2016-05-20 14:45:16 -04:00 committed by Robbie Trencheny
parent 7eeb623b8f
commit 53d51a467d

View File

@ -174,6 +174,10 @@ def check_pid(pid_file):
# PID File does not exist
return
# If we just restarted, we just found our own pidfile.
if pid == os.getpid():
return
try:
os.kill(pid, 0)
except OSError:
@ -253,6 +257,9 @@ def closefds_osx(min_fd, max_fd):
def cmdline():
"""Collect path and arguments to re-execute the current hass instance."""
if sys.argv[0].endswith('/__main__.py'):
modulepath = os.path.dirname(sys.argv[0])
os.environ['PYTHONPATH'] = os.path.dirname(modulepath)
return [sys.executable] + [arg for arg in sys.argv if arg != '--daemon']
@ -395,7 +402,10 @@ def main():
# Create new process group if we can
if hasattr(os, 'setpgid'):
os.setpgid(0, 0)
try:
os.setpgid(0, 0)
except PermissionError:
pass
exit_code = setup_and_run_hass(config_dir, args)
if exit_code == RESTART_EXIT_CODE and not args.runner: