def sighup_handler(signum, frame): # On SIGHUP, check if the configuration is valid and reload in # this process, and if it is valid, signal SIGHUP to all # child processes. if get_configuration().reload(): parent = psutil.Process(os.getpid()) children = parent.children(recursive=True) for process in children: process.send_signal(signal.SIGHUP) if children: logging.info('Main process received SIGHUP with valid config, sent SIGHUP to ' f'child processes {[c.pid for c in children]}')
def sighup_handler(signum, frame): # On SIGHUP, check if the configuration is valid and reload in # this process, and if it is valid, signal our three long-running # child processes. All other processes are short-lived and forked # from those or this process, so any new ones will pick up # the new config automatically. if get_configuration().reload(): pids = [whois_process.pid, http_process.pid, preload_manager.pid] logging.info( f'Main process received SIGHUP with valid config, sending SIGHUP to ' 'child processes {pids}') for pid in pids: os.kill(pid, signal.SIGHUP)
def run_http_server(config_path: str): setproctitle('irrd-http-server-manager') configuration = get_configuration() assert configuration os.environ[ENV_UVICORN_WORKER_CONFIG_PATH] = config_path uvicorn.run( app="irrd.server.http.app:app", host=get_setting('server.http.interface'), port=get_setting('server.http.port'), workers=get_setting('server.http.workers'), forwarded_allow_ips=get_setting('server.http.forwarded_allowed_ips'), headers=[['Server', f'IRRd {__version__}']], log_config=configuration.logging_config, )
def main(): description = """IRRd main process""" parser = argparse.ArgumentParser(description=description) parser.add_argument('--config', dest='config_file_path', type=str, help=f'use a different IRRd config file (default: {CONFIG_PATH_DEFAULT})') parser.add_argument('--foreground', dest='foreground', action='store_true', help=f"run IRRd in the foreground, don't detach") args = parser.parse_args() mirror_frequency = int(os.environ.get('IRRD_SCHEDULER_TIMER_OVERRIDE', 15)) daemon_kwargs = { 'umask': 0o022, } if args.foreground: daemon_kwargs['detach_process'] = False daemon_kwargs['stdout'] = sys.stdout daemon_kwargs['stderr'] = sys.stderr # Since Python 3.8, the default method is spawn for MacOS, # which creates several issues. For consistency, we force to fork. multiprocessing.set_start_method('fork') # config_init with commit may only be called within DaemonContext, # but this call here causes fast failure for most misconfigurations config_init(args.config_file_path, commit=False) if not any([ get_configuration().user_config_staging.get('log.logfile_path'), get_configuration().user_config_staging.get('log.logging_config_path'), args.foreground, ]): logging.critical('Unable to start: when not running in the foreground, you must set ' 'either log.logfile_path or log.logging_config_path in the settings') return with daemon.DaemonContext(**daemon_kwargs): config_init(args.config_file_path) uid, gid = get_configured_owner() # Running as root is permitted on CI if not os.environ.get('CI') and not uid and os.geteuid() == 0: logging.critical('Unable to start: user and group must be defined in settings ' 'when starting IRRd as root') return piddir = get_setting('piddir') logger.info('IRRd attempting to secure PID') try: with PidFile(pidname='irrd', piddir=piddir): logger.info(f'IRRd {__version__} starting, PID {os.getpid()}, PID file in {piddir}') run_irrd(mirror_frequency=mirror_frequency, config_file_path=args.config_file_path if args.config_file_path else CONFIG_PATH_DEFAULT, uid=uid, gid=gid, ) except PidFileError as pfe: logger.error(f'Failed to start IRRd, unable to lock PID file irrd.pid in {piddir}: {pfe}') except Exception as e: logger.error(f'Error occurred in main process, terminating. Error follows:') logger.exception(e) os.kill(os.getpid(), signal.SIGTERM)
def sighup(server): get_configuration().reload() server.sighup_workers()