File size: 2,244 Bytes
2eef9ea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
"""Entry point: starts Redis (if possible), serves the FastAPI backend + static frontend."""
import subprocess
import time
import socket

import uvicorn
from fastapi.staticfiles import StaticFiles

from backend.api.main import app
from backend.core.config import get_settings

# Mount at module level so the reloader worker picks it up when importing run:app
app.mount("/", StaticFiles(directory="frontend", html=True), name="frontend")


def _redis_already_up(host: str = "127.0.0.1", port: int = 6379) -> bool:
    try:
        with socket.create_connection((host, port), timeout=1):
            return True
    except OSError:
        return False


def _start_redis() -> subprocess.Popen | None:
    if _redis_already_up():
        print("[run] Redis already running.")
        return None

    # Try Docker first
    try:
        proc = subprocess.Popen(
            ["docker", "run", "--rm", "-p", "6379:6379", "--name", "mas-redis", "redis:7-alpine"],
            stdout=subprocess.DEVNULL,
            stderr=subprocess.DEVNULL,
        )
        for _ in range(10):
            time.sleep(0.5)
            if _redis_already_up():
                print("[run] Redis started via Docker.")
                return proc
        proc.terminate()
    except FileNotFoundError:
        pass

    # Try local redis-server
    try:
        proc = subprocess.Popen(
            ["redis-server", "--daemonize", "no"],
            stdout=subprocess.DEVNULL,
            stderr=subprocess.DEVNULL,
        )
        for _ in range(6):
            time.sleep(0.5)
            if _redis_already_up():
                print("[run] Redis started via redis-server.")
                return proc
        proc.terminate()
    except FileNotFoundError:
        pass

    print("[run] Redis unavailable — short-term memory disabled (app still works).")
    return None


if __name__ == "__main__":
    settings = get_settings()
    redis_proc = _start_redis()
    try:
        uvicorn.run(
            "run:app",
            host=settings.app_host,
            port=settings.app_port,
            reload=settings.is_dev,
        )
    finally:
        if redis_proc is not None:
            redis_proc.terminate()
            print("[run] Redis stopped.")