From f7fdf8d8ce2092bbbf45229e7b3f4f13f2ee6bf7 Mon Sep 17 00:00:00 2001 From: Debanjum Singh Solanky Date: Thu, 11 Aug 2022 00:13:14 +0300 Subject: [PATCH] Refactor app start to start server even if backend not configured - Decouple configuring backend from starting server. Backend search and processors can be configured after the backend server has started - Set global state in main instead of in configure_server method. This allows the app to start even if configure_server exits early in the first run scenario, where no config available to configure server - Now start server, even if no config, before GUI started in main - This refactor of app startup flow will allow users to configure backend using the configure screen after server start --- src/configure.py | 25 +++++++++++++------------ src/main.py | 31 ++++++++++++++----------------- 2 files changed, 27 insertions(+), 29 deletions(-) diff --git a/src/configure.py b/src/configure.py index 34051f8b..2981dc9d 100644 --- a/src/configure.py +++ b/src/configure.py @@ -1,3 +1,6 @@ +# System Packages +import sys + # External Packages import torch import json @@ -13,24 +16,22 @@ from src.utils.helpers import get_absolute_path from src.utils.rawconfig import FullConfig, ProcessorConfig -def configure_server(args): - # Stores the file path to the config file. - state.config_file = args.config_file - - # Store the raw config data. - state.config = args.config - - # Store the verbose flag - state.verbose = args.verbose +def configure_server(args, required=False): + if args.config is None: + if required: + print('Exiting as Khoj is not configured. Configure the application to use it.') + sys.exit(1) + else: + return + else: + state.config = args.config # Initialize the search model from Config - state.model = configure_search(state.model, args.config, args.regenerate, device=state.device, verbose=state.verbose) + state.model = configure_search(state.model, state.config, args.regenerate, device=state.device, verbose=state.verbose) # Initialize Processor from Config state.processor_config = configure_processor(args.config.processor, verbose=state.verbose) - return args.host, args.port, args.socket - def configure_search(model: SearchModels, config: FullConfig, regenerate: bool, t: SearchType = None, device=torch.device("cpu"), verbose: int = 0): # Initialize Org Notes Search diff --git a/src/main.py b/src/main.py index 4e75537d..ecef329e 100644 --- a/src/main.py +++ b/src/main.py @@ -27,38 +27,35 @@ def run(): # Load config from CLI state.cli_args = sys.argv[1:] args = cli(state.cli_args) + set_state(args) - # Setup Base GUI + # Setup GUI gui = QtWidgets.QApplication([]) gui.setQuitOnLastWindowClosed(False) configure_screen = ConfigureScreen(args.config_file) tray = create_system_tray(gui, configure_screen) tray.show() + # Setup Server + configure_server(args, required=False) + server = ServerThread(app, args.host, args.port, args.socket) + # Trigger First Run Experience, if required if args.config is None: - configure_screen.show() - gui.exec() + configure_screen.show() - # Reload config after first run - args = cli(sys.argv[1:]) - # Quit if app still not configured - if args.config is None: - print('Exiting as Khoj is not configured. Configure the application to use it.') - sys.exit(1) - - # Setup Application Server - host, port, socket = configure_server(args) - - # Start Application Server - server = ServerThread(app, host, port, socket) + # Start Application server.start() gui.aboutToQuit.connect(server.terminate) - - # Start the GUI gui.exec() +def set_state(args): + state.config_file = args.config_file + state.config = args.config + state.verbose = args.verbose + + class ServerThread(QThread): def __init__(self, app, host=None, port=None, socket=None): super(ServerThread, self).__init__()