def main(cmd_line_args: argparse.Namespace) -> None: cfg_file = cmd_line_args.configfile app_args = {'config_file': cfg_file} # Setup Logging version = utils.get_software_version() if cmd_line_args.nologfile: app_args['log_file'] = "" else: app_args['log_file'] = os.path.normpath( os.path.expanduser(cmd_line_args.logfile)) app_args['software_version'] = version app_args['python_version'] = sys.version.replace("\n", " ") ql, file_logger, warning = utils.setup_logging(app_args) if warning is not None: app_args['log_warning'] = warning # Start asyncio event loop and server event_loop = EventLoop() alt_config_loaded = False estatus = 0 while True: try: server = Server(app_args, file_logger, event_loop) server.load_components() except confighelper.ConfigError as e: backup_cfg = confighelper.find_config_backup(cfg_file) logging.exception("Server Config Error") if alt_config_loaded or backup_cfg is None: estatus = 1 break app_args['config_file'] = backup_cfg app_args['config_warning'] = ( f"Server configuration error: {e}\n" f"Loaded server from most recent working configuration:" f" '{app_args['config_file']}'\n" f"Please fix the issue in moonraker.conf and restart " f"the server.") alt_config_loaded = True continue except Exception: logging.exception("Moonraker Error") estatus = 1 break try: event_loop.register_callback(server.server_init) event_loop.start() except Exception: logging.exception("Server Running Error") estatus = 1 break if server.exit_reason == "terminate": break # Restore the original config and clear the warning # before the server restarts if alt_config_loaded: app_args['config_file'] = cfg_file app_args.pop('config_warning', None) alt_config_loaded = False event_loop.close() # Since we are running outside of the the server # it is ok to use a blocking sleep here time.sleep(.5) logging.info("Attempting Server Restart...") event_loop.reset() event_loop.close() logging.info("Server Shutdown") ql.stop() exit(estatus)
def main() -> None: # Parse start arguments parser = argparse.ArgumentParser( description="Moonraker - Klipper API Server") parser.add_argument("-c", "--configfile", default="~/moonraker.conf", metavar='<configfile>', help="Location of moonraker configuration file") parser.add_argument("-l", "--logfile", default="/tmp/moonraker.log", metavar='<logfile>', help="log file name and location") parser.add_argument("-n", "--nologfile", action='store_true', help="disable logging to a file") cmd_line_args = parser.parse_args() cfg_file = cmd_line_args.configfile app_args = {'config_file': cfg_file} # Setup Logging version = utils.get_software_version() if cmd_line_args.nologfile: app_args['log_file'] = "" else: app_args['log_file'] = os.path.normpath( os.path.expanduser(cmd_line_args.logfile)) app_args['software_version'] = version ql, file_logger, warning = utils.setup_logging(app_args) if warning is not None: app_args['log_warning'] = warning if sys.version_info < (3, 7): msg = f"Moonraker requires Python 3.7 or above. " \ f"Detected Version: {sys.version}" logging.info(msg) print(msg) ql.stop() exit(1) # Start asyncio event loop and server event_loop = EventLoop() alt_config_loaded = False estatus = 0 while True: try: server = Server(app_args, file_logger, event_loop) server.load_components() except confighelper.ConfigError as e: backup_cfg = confighelper.find_config_backup(cfg_file) if alt_config_loaded or backup_cfg is None: logging.exception("Server Config Error") estatus = 1 break app_args['config_file'] = backup_cfg app_args['config_warning'] = ( f"Server configuration error: {e}\n" f"Loaded server from most recent working configuration:" f" '{app_args['config_file']}'\n" f"Please fix the issue in moonraker.conf and restart " f"the server.") alt_config_loaded = True continue except Exception: logging.exception("Moonraker Error") estatus = 1 break try: event_loop.register_callback(server.server_init) event_loop.start() except Exception: logging.exception("Server Running Error") estatus = 1 break if server.exit_reason == "terminate": break # Restore the original config and clear the warning # before the server restarts if alt_config_loaded: app_args['config_file'] = cfg_file app_args.pop('config_warning', None) alt_config_loaded = False event_loop.close() # Since we are running outside of the the server # it is ok to use a blocking sleep here time.sleep(.5) logging.info("Attempting Server Restart...") for _ in range(5): # Sometimes the new loop does not properly instantiate. # Give 5 attempts before raising an exception new_loop = asyncio.new_event_loop() if not new_loop.is_closed(): break logging.info("Failed to create open eventloop, " "retyring in .5 seconds...") time.sleep(.5) else: raise RuntimeError("Unable to create new open eventloop") asyncio.set_event_loop(new_loop) event_loop.reset() event_loop.close() logging.info("Server Shutdown") ql.stop() exit(estatus)