-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathrun_fastapi.py
More file actions
153 lines (129 loc) · 4.28 KB
/
run_fastapi.py
File metadata and controls
153 lines (129 loc) · 4.28 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
"""
FastAPI Application Startup Script
© 2025 Murat Unsal — Skyulf Project
This script starts the FastAPI application with proper configuration.
It replaces the Flask run.py with modern async server capabilities.
"""
import logging
import os
import subprocess
import sys
from backend.config import get_settings
from backend.utils.logging_utils import setup_universal_logging
from backend.main import app
def setup_logging():
"""Setup logging configuration."""
settings = get_settings()
# Use the unified logging setup which supports time-based rotation
setup_universal_logging(
log_file=settings.LOG_FILE,
log_level=settings.LOG_LEVEL,
rotation_type=settings.LOG_ROTATION_TYPE,
rotation_when=settings.LOG_ROTATION_WHEN,
rotation_interval=settings.LOG_ROTATION_INTERVAL,
max_bytes=settings.LOG_MAX_SIZE,
backup_count=settings.LOG_BACKUP_COUNT,
)
def start_celery_worker():
"""Start the Celery worker in a separate process."""
settings = get_settings()
if not settings.USE_CELERY:
print("🚫 Celery worker disabled by configuration.")
return None
if sys.platform == "win32":
pool_arg = "--pool=solo"
else:
pool_arg = "--pool=prefork"
cmd = [
sys.executable,
"-m",
"celery",
"-A",
"celery_worker.celery_app",
"worker",
pool_arg,
"--loglevel=info",
"--queues",
"mlops-training",
]
print(f"👷 Starting Celery worker: {' '.join(cmd)}")
return subprocess.Popen(cmd, cwd=os.getcwd())
def check_redis_availability():
"""Check if Redis is running and accessible."""
settings = get_settings()
try:
import redis
# Use a short timeout to avoid hanging
client = redis.from_url(settings.CELERY_BROKER_URL, socket_connect_timeout=1)
client.ping()
return True
except Exception:
return False
def main():
"""Main entry point for the FastAPI application."""
# Setup logging
setup_logging()
logger = logging.getLogger(__name__)
# Get settings
settings = get_settings()
# Log startup information
logger.info(f"[START] Starting {settings.APP_NAME}")
logger.info(
f"[ENV] Environment: {'Development' if settings.DEBUG else 'Production'}"
)
logger.info(f"[HOST] Host: {settings.HOST}:{settings.PORT}")
# Import uvicorn here to avoid import errors if not installed
try:
import uvicorn
except ImportError:
logger.error(
"[ERROR] uvicorn not installed. Please run: pip install uvicorn[standard]"
)
sys.exit(1)
celery_process = None
# Development vs Production server configuration
if settings.DEBUG:
# Start Celery worker in development mode if Redis is available
if check_redis_availability():
try:
celery_process = start_celery_worker()
except Exception as e:
logger.error(f"[ERROR] Failed to start Celery worker: {e}")
else:
logger.warning("[WARN] Redis not found. Celery worker will NOT be started.")
logger.warning(
" To enable background tasks, start Redis: docker-compose up -d redis"
)
# Development server with auto-reload
logger.info("[DEV] Running in development mode with auto-reload")
try:
uvicorn.run(
"core.main:app",
host=settings.HOST,
port=settings.PORT,
reload=True,
reload_dirs=["."],
log_level="info",
access_log=True,
use_colors=True,
)
finally:
if celery_process:
logger.info("🛑 Stopping Celery worker...")
celery_process.terminate()
celery_process.wait()
else:
# Production server
logger.info("🏭 Running in production mode")
uvicorn.run(
app,
host=settings.HOST,
port=settings.PORT,
workers=settings.WORKERS,
log_level="warning",
access_log=False,
server_header=False,
date_header=False,
)
if __name__ == "__main__":
main()