core/homeassistant/helpers/signal.py

72 lines
2.2 KiB
Python
Raw Normal View History

"""Signal handling related helpers."""
import logging
import signal
import sys
from types import FrameType
from homeassistant.core import callback, HomeAssistant
from homeassistant.const import RESTART_EXIT_CODE
from homeassistant.loader import bind_hass
_LOGGER = logging.getLogger(__name__)
@callback
@bind_hass
def async_register_signal_handling(hass: HomeAssistant) -> None:
"""Register system signal handler for core."""
2019-07-31 19:25:30 +00:00
if sys.platform != "win32":
@callback
def async_signal_handle(exit_code: int) -> None:
"""Wrap signal handling.
* queue call to shutdown task
* re-instate default handler
"""
hass.loop.remove_signal_handler(signal.SIGTERM)
hass.loop.remove_signal_handler(signal.SIGINT)
hass.async_create_task(hass.async_stop(exit_code))
try:
2019-07-31 19:25:30 +00:00
hass.loop.add_signal_handler(signal.SIGTERM, async_signal_handle, 0)
except ValueError:
_LOGGER.warning("Could not bind to SIGTERM")
try:
2019-07-31 19:25:30 +00:00
hass.loop.add_signal_handler(signal.SIGINT, async_signal_handle, 0)
except ValueError:
_LOGGER.warning("Could not bind to SIGINT")
try:
hass.loop.add_signal_handler(
2019-07-31 19:25:30 +00:00
signal.SIGHUP, async_signal_handle, RESTART_EXIT_CODE
)
except ValueError:
_LOGGER.warning("Could not bind to SIGHUP")
else:
old_sigterm = None
old_sigint = None
@callback
def async_signal_handle(exit_code: int, frame: FrameType) -> None:
"""Wrap signal handling.
* queue call to shutdown task
* re-instate default handler
"""
signal.signal(signal.SIGTERM, old_sigterm)
signal.signal(signal.SIGINT, old_sigint)
hass.async_create_task(hass.async_stop(exit_code))
try:
old_sigterm = signal.signal(signal.SIGTERM, async_signal_handle)
except ValueError:
_LOGGER.warning("Could not bind to SIGTERM")
try:
old_sigint = signal.signal(signal.SIGINT, async_signal_handle)
except ValueError:
_LOGGER.warning("Could not bind to SIGINT")