Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:1Panel dev MaxKB main py

From Leeroopedia
Knowledge Sources
Domains Deployment, CLI
Last Updated 2026-10-10 00:00 GMT

Overview

Concrete CLI entrypoint that orchestrates configuration, migration, static collection, and multi-service startup for MaxKB.

Description

The main.py module is the single entrypoint for all MaxKB services. It sets up `sys.path` (inserting `apps/`), configures `DJANGO_SETTINGS_MODULE`, sets `SERVER_NAME` based on the target service, and dispatches to the appropriate action:

  • dev — Runs a single service in dev mode (web: runserver 0.0.0.0:8080, celery: celery worker, local_model: runserver 127.0.0.1:11636). Always calls collect_static + perform_db_migrate first.
  • start — Production mode: spawns gunicorn + celery + local_model as watched daemons via Django management commands.
  • upgrade_db — Runs `migrate` with PG crash-recovery retry logic (10 retries, 5s interval).
  • collect_static — Runs `collectstatic --no-input -c`.

Usage

Import this module when you need to:

  • Start any MaxKB service (web, celery, local_model)
  • Run database migrations with crash recovery
  • Collect static files for production

Code Reference

Source Location

  • Repository: MaxKB
  • File: main.py

Signature

def collect_static() -> None:
    """Run Django collectstatic --no-input -c (silently catches errors)."""

def perform_db_migrate() -> None:
    """
    Run Django migrate with PostgreSQL crash-recovery retry.
    Retries up to 10 times with 5s interval if DB is starting up.
    Exits with code 11 on failure.
    """

def dev() -> None:
    """
    Dev mode: run one service directly.
    web:       runserver 0.0.0.0:8080
    celery:    celery worker (queue: celery)
    local_model: runserver 127.0.0.1:11636
    """

def start_services() -> None:
    """Production mode: spawn services as daemons via management commands."""

# CLI dispatch:
#   python main.py dev [web|celery|local_model]
#   python main.py start [all|web|task] -d -w <workers> -f
#   python main.py upgrade_db
#   python main.py collect_static

Import

# main.py is executed as a script, not imported:
# python main.py dev web

I/O Contract

Inputs

Name Type Required Description
action str Yes One of: start, dev, upgrade_db, collect_static
services str Yes Service to run: web, celery, local_model (dev) or all, web, task (start)
-d/--daemon flag No Run as daemon (start mode)
-w/--worker int No Worker count (start mode)
-f/--force flag No Force restart (start mode)

Outputs

Name Type Description
stdout Server logs and migration status
exit code int 0 on success, 11 on migration failure

Usage Examples

Starting Services

# Dev mode (one service per terminal)
python main.py dev web          # Django runserver on 0.0.0.0:8080
python main.py dev celery       # Celery worker
python main.py dev local_model  # Local embedding model server

# Production mode
python main.py start all -d            # All services as daemons
python main.py start web -w 3           # Gunicorn with 3 workers
python main.py start task              # Celery workers

# Database and static
python main.py upgrade_db      # Run migrations (with PG retry)
python main.py collect_static  # Collect static files

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment