mirror of
https://github.com/khoaliber/khoj.git
synced 2026-03-05 21:29:11 +00:00
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:
@@ -1,3 +1,6 @@
|
|||||||
|
# System Packages
|
||||||
|
import sys
|
||||||
|
|
||||||
# External Packages
|
# External Packages
|
||||||
import torch
|
import torch
|
||||||
import json
|
import json
|
||||||
@@ -13,24 +16,22 @@ from src.utils.helpers import get_absolute_path
|
|||||||
from src.utils.rawconfig import FullConfig, ProcessorConfig
|
from src.utils.rawconfig import FullConfig, ProcessorConfig
|
||||||
|
|
||||||
|
|
||||||
def configure_server(args):
|
def configure_server(args, required=False):
|
||||||
# Stores the file path to the config file.
|
if args.config is None:
|
||||||
state.config_file = args.config_file
|
if required:
|
||||||
|
print('Exiting as Khoj is not configured. Configure the application to use it.')
|
||||||
# Store the raw config data.
|
sys.exit(1)
|
||||||
state.config = args.config
|
else:
|
||||||
|
return
|
||||||
# Store the verbose flag
|
else:
|
||||||
state.verbose = args.verbose
|
state.config = args.config
|
||||||
|
|
||||||
# Initialize the search model from 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
|
# Initialize Processor from Config
|
||||||
state.processor_config = configure_processor(args.config.processor, verbose=state.verbose)
|
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):
|
def configure_search(model: SearchModels, config: FullConfig, regenerate: bool, t: SearchType = None, device=torch.device("cpu"), verbose: int = 0):
|
||||||
# Initialize Org Notes Search
|
# Initialize Org Notes Search
|
||||||
|
|||||||
31
src/main.py
31
src/main.py
@@ -27,38 +27,35 @@ def run():
|
|||||||
# Load config from CLI
|
# Load config from CLI
|
||||||
state.cli_args = sys.argv[1:]
|
state.cli_args = sys.argv[1:]
|
||||||
args = cli(state.cli_args)
|
args = cli(state.cli_args)
|
||||||
|
set_state(args)
|
||||||
|
|
||||||
# Setup Base GUI
|
# Setup GUI
|
||||||
gui = QtWidgets.QApplication([])
|
gui = QtWidgets.QApplication([])
|
||||||
gui.setQuitOnLastWindowClosed(False)
|
gui.setQuitOnLastWindowClosed(False)
|
||||||
configure_screen = ConfigureScreen(args.config_file)
|
configure_screen = ConfigureScreen(args.config_file)
|
||||||
tray = create_system_tray(gui, configure_screen)
|
tray = create_system_tray(gui, configure_screen)
|
||||||
tray.show()
|
tray.show()
|
||||||
|
|
||||||
|
# Setup Server
|
||||||
|
configure_server(args, required=False)
|
||||||
|
server = ServerThread(app, args.host, args.port, args.socket)
|
||||||
|
|
||||||
# Trigger First Run Experience, if required
|
# Trigger First Run Experience, if required
|
||||||
if args.config is None:
|
if args.config is None:
|
||||||
configure_screen.show()
|
configure_screen.show()
|
||||||
gui.exec()
|
|
||||||
|
|
||||||
# Reload config after first run
|
# Start Application
|
||||||
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)
|
|
||||||
server.start()
|
server.start()
|
||||||
gui.aboutToQuit.connect(server.terminate)
|
gui.aboutToQuit.connect(server.terminate)
|
||||||
|
|
||||||
# Start the GUI
|
|
||||||
gui.exec()
|
gui.exec()
|
||||||
|
|
||||||
|
|
||||||
|
def set_state(args):
|
||||||
|
state.config_file = args.config_file
|
||||||
|
state.config = args.config
|
||||||
|
state.verbose = args.verbose
|
||||||
|
|
||||||
|
|
||||||
class ServerThread(QThread):
|
class ServerThread(QThread):
|
||||||
def __init__(self, app, host=None, port=None, socket=None):
|
def __init__(self, app, host=None, port=None, socket=None):
|
||||||
super(ServerThread, self).__init__()
|
super(ServerThread, self).__init__()
|
||||||
|
|||||||
Reference in New Issue
Block a user