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
This commit is contained in:
Debanjum Singh Solanky 2022-08-11 00:13:14 +03:00
parent 34018c7d4b
commit f7fdf8d8ce
2 changed files with 27 additions and 29 deletions

View file

@ -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

View file

@ -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__()