daemon/grpc: shifted kill signal handling to grpc server who will handle complete shutdown, no longer done by CoreEmu directly

This commit is contained in:
Blake Harnden 2022-10-12 14:34:24 -07:00
parent b5b7b8cdf9
commit 03775c2c3c
2 changed files with 14 additions and 29 deletions

View file

@ -1,7 +1,8 @@
import atexit
import logging import logging
import os import os
import re import re
import signal
import sys
import tempfile import tempfile
import time import time
from concurrent import futures from concurrent import futures
@ -120,11 +121,20 @@ class CoreGrpcServer(core_pb2_grpc.CoreApiServicer):
self.coreemu: CoreEmu = coreemu self.coreemu: CoreEmu = coreemu
self.running: bool = True self.running: bool = True
self.server: Optional[grpc.Server] = None self.server: Optional[grpc.Server] = None
atexit.register(self._exit_handler) # catch signals
signal.signal(signal.SIGHUP, self._signal_handler)
signal.signal(signal.SIGINT, self._signal_handler)
signal.signal(signal.SIGTERM, self._signal_handler)
signal.signal(signal.SIGUSR1, self._signal_handler)
signal.signal(signal.SIGUSR2, self._signal_handler)
def _exit_handler(self) -> None: def _signal_handler(self, signal_number: int, _) -> None:
logger.debug("catching exit, stop running") logger.info("caught signal: %s", signal_number)
self.coreemu.shutdown()
self.running = False self.running = False
if self.server:
self.server.stop(None)
sys.exit(signal_number)
def _is_running(self, context) -> bool: def _is_running(self, context) -> bool:
return self.running and context.is_active() return self.running and context.is_active()

View file

@ -1,8 +1,5 @@
import atexit
import logging import logging
import os import os
import signal
import sys
from pathlib import Path from pathlib import Path
from typing import Dict, List, Type from typing import Dict, List, Type
@ -18,25 +15,6 @@ logger = logging.getLogger(__name__)
DEFAULT_EMANE_PREFIX: str = "/usr" DEFAULT_EMANE_PREFIX: str = "/usr"
def signal_handler(signal_number: int, _) -> None:
"""
Handle signals and force an exit with cleanup.
:param signal_number: signal number
:param _: ignored
:return: nothing
"""
logger.info("caught signal: %s", signal_number)
sys.exit(signal_number)
signal.signal(signal.SIGHUP, signal_handler)
signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)
signal.signal(signal.SIGUSR1, signal_handler)
signal.signal(signal.SIGUSR2, signal_handler)
class CoreEmu: class CoreEmu:
""" """
Provides logic for creating and configuring CORE sessions and the nodes within them. Provides logic for creating and configuring CORE sessions and the nodes within them.
@ -70,9 +48,6 @@ class CoreEmu:
# check executables exist on path # check executables exist on path
self._validate_env() self._validate_env()
# catch exit event
atexit.register(self.shutdown)
def _validate_env(self) -> None: def _validate_env(self) -> None:
""" """
Validates executables CORE depends on exist on path. Validates executables CORE depends on exist on path.