[Multi-User Part 5]: Add a production Docker file and use a gunicorn configuration with it (#514)

- Add a productionized setup for the Khoj server using `gunicorn` with multiple workers for handling requests
- Add a new Dockerfile meant for production config at `ghcr.io/khoj-ai/khoj:prod`; the existing Docker config should remain the same
This commit is contained in:
sabaimran
2023-10-26 13:15:31 -07:00
committed by GitHub
parent 9acc722f7f
commit 5f3f6b7c61
12 changed files with 117 additions and 11 deletions

View File

@@ -103,6 +103,9 @@ def configure_server(
user: KhojUser = None,
):
# Update Config
if config == None:
logger.info(f"🚨 Khoj is not configured.\nInitializing it with a default config.")
config = FullConfig()
state.config = config
# Initialize Search Models from Config and initialize content

View File

@@ -65,7 +65,7 @@ logging.basicConfig(handlers=[rich_handler])
logger = logging.getLogger("khoj")
def run():
def run(should_start_server=True):
# Turn Tokenizers Parallelism Off. App does not support it.
os.environ["TOKENIZERS_PARALLELISM"] = "false"
@@ -107,7 +107,10 @@ def run():
configure_middleware(app)
initialize_server(args.config)
start_server(app, host=args.host, port=args.port, socket=args.socket)
# If the server is started through gunicorn (external to the script), don't start the server
if should_start_server:
start_server(app, host=args.host, port=args.port, socket=args.socket)
def set_state(args):
@@ -139,3 +142,5 @@ def poll_task_scheduler():
if __name__ == "__main__":
run()
else:
run(should_start_server=False)

View File

@@ -36,16 +36,17 @@ else:
@auth_router.get("/login")
async def login_get(request: Request):
redirect_uri = request.url_for("auth")
redirect_uri = str(request.app.url_path_for("auth"))
return await oauth.google.authorize_redirect(request, redirect_uri)
@auth_router.post("/login")
async def login(request: Request):
redirect_uri = request.url_for("auth")
redirect_uri = str(request.app.url_path_for("auth"))
return await oauth.google.authorize_redirect(request, redirect_uri)
@auth_router.post("/redirect")
@auth_router.post("/token")
@requires(["authenticated"], redirect="login_page")
async def generate_token(request: Request, token_name: Optional[str] = None) -> str:

View File

@@ -3,6 +3,9 @@ import argparse
import pathlib
from importlib.metadata import version
import os
import logging
logger = logging.getLogger(__name__)
# Internal Packages
from khoj.utils.helpers import resolve_absolute_path
@@ -17,7 +20,7 @@ def cli(args=None):
# Setup Argument Parser for the Commandline Interface
parser = argparse.ArgumentParser(description="Start Khoj; An AI personal assistant for your Digital Brain")
parser.add_argument(
"--config-file", "-c", default="~/.khoj/khoj.yml", type=pathlib.Path, help="YAML file to configure Khoj"
"--config-file", default="~/.khoj/khoj.yml", type=pathlib.Path, help="YAML file to configure Khoj"
)
parser.add_argument(
"--regenerate",
@@ -42,7 +45,9 @@ def cli(args=None):
help="Run Khoj in anonymous mode. This does not require any login for connecting users.",
)
args = parser.parse_args(args)
args, remaining_args = parser.parse_known_args(args)
logger.debug(f"Ignoring unknown commandline args: {remaining_args}")
args.version_no = version("khoj-assistant")
if args.version: