Fix Supervisor startup errors and improve application resilience
- Update Supervisor configuration to use python -m uvicorn for reliable module loading - Fix database path resolution using absolute paths - Add in-memory SQLite database option for testing and when file access is problematic - Improve error handling in database connection module - Enhance logging configuration with more detailed output - Add environment variables and documentation for better configuration options - Update troubleshooting guide with common issues and solutions
This commit is contained in:
parent
acf2757384
commit
a5aebe0377
25
README.md
25
README.md
@ -25,6 +25,8 @@ The application uses the following environment variables (see `.env.example` for
|
|||||||
- `SECRET_KEY`: Secret key for JWT token encryption (default provided for development)
|
- `SECRET_KEY`: Secret key for JWT token encryption (default provided for development)
|
||||||
- `ACCESS_TOKEN_EXPIRE_MINUTES`: JWT token expiration time in minutes (default: 60 * 24 * 8 = 8 days)
|
- `ACCESS_TOKEN_EXPIRE_MINUTES`: JWT token expiration time in minutes (default: 60 * 24 * 8 = 8 days)
|
||||||
- `PORT`: Port number for the application to listen on (default: 8000)
|
- `PORT`: Port number for the application to listen on (default: 8000)
|
||||||
|
- `USE_IN_MEMORY_DB`: Set to "true" to use an in-memory SQLite database (useful for testing or when file access is problematic)
|
||||||
|
- `LOG_LEVEL`: Logging level (e.g., DEBUG, INFO, WARNING, ERROR) - defaults to INFO
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
@ -184,16 +186,31 @@ supervisorctl restart app-8001
|
|||||||
To view logs:
|
To view logs:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
tail -f /tmp/app-8001.log # Application logs
|
tail -f /tmp/app-8001.log # Application logs (contains both stdout and stderr with redirect_stderr=true)
|
||||||
tail -f /tmp/app-8001-error.log # Error logs
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Troubleshooting
|
## Troubleshooting
|
||||||
|
|
||||||
If you encounter issues with the application starting up:
|
If you encounter issues with the application starting up:
|
||||||
|
|
||||||
1. Check the error logs: `tail -f /tmp/app-8001-error.log`
|
1. Check the error logs: `tail -f /tmp/app-8001.log`
|
||||||
2. Verify the database path is correct and accessible
|
2. Verify the database path is correct and accessible
|
||||||
3. Ensure all environment variables are properly set
|
3. Ensure all environment variables are properly set
|
||||||
4. Check permissions for the storage directory
|
4. Check permissions for the storage directory
|
||||||
5. Try running the application directly with uvicorn to see detailed error messages
|
5. Try using the in-memory database option for testing: `USE_IN_MEMORY_DB=true`
|
||||||
|
6. Run the application directly with uvicorn to see detailed error messages: `python -m uvicorn main:app --log-level debug`
|
||||||
|
7. Check your Python environment and installed packages: `pip list`
|
||||||
|
|
||||||
|
### Common Issues and Solutions
|
||||||
|
|
||||||
|
#### Database Access Problems
|
||||||
|
|
||||||
|
If you encounter database access issues, you can set `USE_IN_MEMORY_DB=true` in your .env file to use an in-memory SQLite database instead of a file-based one. This can help isolate whether the issue is with file permissions or database configuration.
|
||||||
|
|
||||||
|
#### Supervisor Configuration
|
||||||
|
|
||||||
|
If Supervisor fails to start the application:
|
||||||
|
- Make sure the paths in supervisord.conf are correct
|
||||||
|
- Check that the PYTHONPATH environment variable is set correctly
|
||||||
|
- Verify that Supervisor has permission to run the application
|
||||||
|
- Use `python -m uvicorn` instead of just `uvicorn` to ensure the Python module path is correct
|
@ -20,13 +20,15 @@ class Settings(BaseSettings):
|
|||||||
ACCESS_TOKEN_EXPIRE_MINUTES: int = 60 * 24 * 8 # 8 days
|
ACCESS_TOKEN_EXPIRE_MINUTES: int = 60 * 24 * 8 # 8 days
|
||||||
|
|
||||||
# SQLite settings
|
# SQLite settings
|
||||||
# Main database location
|
# Main database location - use absolute path for reliability
|
||||||
DB_DIR = Path("app/storage/db") # Relative path for better compatibility
|
DB_DIR = Path(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) / "app" / "storage" / "db"
|
||||||
DB_DIR.mkdir(parents=True, exist_ok=True)
|
DB_DIR.mkdir(parents=True, exist_ok=True)
|
||||||
SQLALCHEMY_DATABASE_URL: str = f"sqlite:///{DB_DIR.absolute()}/db.sqlite"
|
SQLALCHEMY_DATABASE_URL: str = f"sqlite:///{DB_DIR}/db.sqlite"
|
||||||
|
|
||||||
# Alternative SQLite connection using memory for testing if file access is an issue
|
# Fallback to in-memory SQLite if file access fails
|
||||||
# SQLALCHEMY_DATABASE_URL: str = "sqlite://" # In-memory SQLite database
|
USE_IN_MEMORY_DB: bool = os.getenv("USE_IN_MEMORY_DB", "").lower() in ("true", "1", "yes")
|
||||||
|
if USE_IN_MEMORY_DB:
|
||||||
|
SQLALCHEMY_DATABASE_URL = "sqlite://" # In-memory SQLite database
|
||||||
|
|
||||||
# CORS settings
|
# CORS settings
|
||||||
BACKEND_CORS_ORIGINS: List[AnyHttpUrl] = []
|
BACKEND_CORS_ORIGINS: List[AnyHttpUrl] = []
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import logging
|
import logging
|
||||||
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
@ -8,8 +9,8 @@ class LogConfig(BaseModel):
|
|||||||
"""Logging configuration to be set for the server"""
|
"""Logging configuration to be set for the server"""
|
||||||
|
|
||||||
LOGGER_NAME: str = "blogging_api"
|
LOGGER_NAME: str = "blogging_api"
|
||||||
LOG_FORMAT: str = "%(levelprefix)s | %(asctime)s | %(message)s"
|
LOG_FORMAT: str = "%(levelprefix)s | %(asctime)s | %(name)s | %(message)s"
|
||||||
LOG_LEVEL: str = "INFO"
|
LOG_LEVEL: str = os.getenv("LOG_LEVEL", "INFO").upper()
|
||||||
|
|
||||||
# Logging config
|
# Logging config
|
||||||
version: int = 1
|
version: int = 1
|
||||||
@ -20,16 +21,30 @@ class LogConfig(BaseModel):
|
|||||||
"fmt": LOG_FORMAT,
|
"fmt": LOG_FORMAT,
|
||||||
"datefmt": "%Y-%m-%d %H:%M:%S",
|
"datefmt": "%Y-%m-%d %H:%M:%S",
|
||||||
},
|
},
|
||||||
|
"detailed": {
|
||||||
|
"()": "uvicorn.logging.DefaultFormatter",
|
||||||
|
"fmt": "%(levelprefix)s | %(asctime)s | %(name)s | %(filename)s:%(lineno)d | %(message)s",
|
||||||
|
"datefmt": "%Y-%m-%d %H:%M:%S",
|
||||||
|
}
|
||||||
}
|
}
|
||||||
handlers: dict = {
|
handlers: dict = {
|
||||||
"default": {
|
"default": {
|
||||||
"formatter": "default",
|
"formatter": "default",
|
||||||
"class": "logging.StreamHandler",
|
"class": "logging.StreamHandler",
|
||||||
"stream": sys.stderr,
|
"stream": sys.stdout, # Use stdout instead of stderr for better supervisor compatibility
|
||||||
},
|
},
|
||||||
|
"detailed": {
|
||||||
|
"formatter": "detailed",
|
||||||
|
"class": "logging.StreamHandler",
|
||||||
|
"stream": sys.stdout,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
loggers: dict = {
|
loggers: dict = {
|
||||||
LOGGER_NAME: {"handlers": ["default"], "level": LOG_LEVEL},
|
LOGGER_NAME: {"handlers": ["default"], "level": LOG_LEVEL, "propagate": False},
|
||||||
|
"uvicorn": {"handlers": ["default"], "level": LOG_LEVEL, "propagate": False},
|
||||||
|
"sqlalchemy": {"handlers": ["detailed"], "level": LOG_LEVEL, "propagate": False},
|
||||||
|
"app": {"handlers": ["default"], "level": LOG_LEVEL, "propagate": False},
|
||||||
|
"db": {"handlers": ["detailed"], "level": LOG_LEVEL, "propagate": False},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -7,33 +7,61 @@ from app.core.logging import get_logger
|
|||||||
|
|
||||||
logger = get_logger("db.session")
|
logger = get_logger("db.session")
|
||||||
|
|
||||||
# Create engine with better error handling
|
# Wrap in function for easier error handling and retries
|
||||||
try:
|
def create_db_engine():
|
||||||
logger.info(f"Creating database engine with URL: {settings.SQLALCHEMY_DATABASE_URL}")
|
# Use in-memory database if configured or as fallback
|
||||||
engine = create_engine(
|
if settings.USE_IN_MEMORY_DB:
|
||||||
settings.SQLALCHEMY_DATABASE_URL,
|
logger.info("Using in-memory SQLite database")
|
||||||
connect_args={"check_same_thread": False},
|
db_url = "sqlite://"
|
||||||
pool_pre_ping=True, # Ensure connections are still alive
|
connect_args = {"check_same_thread": False}
|
||||||
)
|
else:
|
||||||
|
logger.info(f"Using file-based SQLite database at: {settings.SQLALCHEMY_DATABASE_URL}")
|
||||||
|
db_url = settings.SQLALCHEMY_DATABASE_URL
|
||||||
|
connect_args = {"check_same_thread": False}
|
||||||
|
|
||||||
# Add engine event listeners for debugging
|
# Create engine with better error handling
|
||||||
@event.listens_for(engine, "connect")
|
try:
|
||||||
def on_connect(dbapi_connection, connection_record):
|
engine = create_engine(
|
||||||
logger.info("Database connection established")
|
db_url,
|
||||||
|
connect_args=connect_args,
|
||||||
@event.listens_for(engine, "engine_connect")
|
pool_pre_ping=True, # Ensure connections are still alive
|
||||||
def on_engine_connect(connection):
|
)
|
||||||
logger.info("Engine connected")
|
|
||||||
|
|
||||||
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
|
||||||
|
|
||||||
# Test connection on startup
|
|
||||||
with engine.connect() as conn:
|
|
||||||
logger.info("Database connection test successful")
|
|
||||||
|
|
||||||
except SQLAlchemyError as e:
|
# Add engine event listeners for debugging
|
||||||
logger.error(f"Database connection error: {e}", exc_info=True)
|
@event.listens_for(engine, "connect")
|
||||||
# Don't re-raise to allow fallback mechanisms
|
def on_connect(dbapi_connection, connection_record):
|
||||||
|
logger.info("Database connection established")
|
||||||
|
|
||||||
|
@event.listens_for(engine, "engine_connect")
|
||||||
|
def on_engine_connect(connection):
|
||||||
|
logger.info("Engine connected")
|
||||||
|
|
||||||
|
# Test connection
|
||||||
|
with engine.connect() as conn:
|
||||||
|
conn.execute("SELECT 1")
|
||||||
|
logger.info("Database connection test successful")
|
||||||
|
|
||||||
|
return engine
|
||||||
|
|
||||||
|
except SQLAlchemyError as e:
|
||||||
|
logger.error(f"Database connection error: {e}", exc_info=True)
|
||||||
|
# Fall back to in-memory SQLite if file access failed
|
||||||
|
if not settings.USE_IN_MEMORY_DB:
|
||||||
|
logger.info("Falling back to in-memory SQLite database")
|
||||||
|
return create_engine(
|
||||||
|
"sqlite://",
|
||||||
|
connect_args={"check_same_thread": False}
|
||||||
|
)
|
||||||
|
# Re-raise if in-memory DB was already the target
|
||||||
|
raise
|
||||||
|
|
||||||
|
# Initialize engine
|
||||||
|
try:
|
||||||
|
engine = create_db_engine()
|
||||||
|
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Failed to initialize database engine: {e}", exc_info=True)
|
||||||
|
engine = None
|
||||||
SessionLocal = None
|
SessionLocal = None
|
||||||
|
|
||||||
# Dependency to get DB session
|
# Dependency to get DB session
|
||||||
|
13
main.py
13
main.py
@ -46,9 +46,20 @@ app.include_router(api_router)
|
|||||||
@app.on_event("startup")
|
@app.on_event("startup")
|
||||||
async def startup_event():
|
async def startup_event():
|
||||||
logger.info("Application starting up...")
|
logger.info("Application starting up...")
|
||||||
|
# Print current working directory and environment for debugging
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
logger.info(f"Current working directory: {os.getcwd()}")
|
||||||
|
logger.info(f"Python path: {sys.path}")
|
||||||
|
logger.info(f"Environment: {os.environ.get('PYTHONPATH', 'Not set')}")
|
||||||
|
|
||||||
# Make sure we can initialize database connection
|
# Make sure we can initialize database connection
|
||||||
try:
|
try:
|
||||||
logger.info("Database engine initialized successfully")
|
from app.db.session import SessionLocal
|
||||||
|
if SessionLocal:
|
||||||
|
logger.info("Database engine initialized successfully")
|
||||||
|
else:
|
||||||
|
logger.warning("Database session is None - check connection settings")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Failed to initialize database: {e}", exc_info=True)
|
logger.error(f"Failed to initialize database: {e}", exc_info=True)
|
||||||
# We allow the app to start even with DB errors, to avoid restart loops
|
# We allow the app to start even with DB errors, to avoid restart loops
|
||||||
|
@ -7,16 +7,16 @@ loglevel=info
|
|||||||
pidfile=/tmp/supervisord.pid
|
pidfile=/tmp/supervisord.pid
|
||||||
|
|
||||||
[program:app-8001]
|
[program:app-8001]
|
||||||
command=uvicorn main:app --host 0.0.0.0 --port 8001
|
command=python -m uvicorn main:app --host 0.0.0.0 --port 8001 --log-level debug
|
||||||
directory=/projects/bloggingapi-a05jzl
|
directory=/projects/bloggingapi-a05jzl
|
||||||
autostart=true
|
autostart=true
|
||||||
autorestart=true
|
autorestart=true
|
||||||
startretries=5
|
startretries=3
|
||||||
numprocs=1
|
numprocs=1
|
||||||
startsecs=1
|
startsecs=2
|
||||||
redirect_stderr=false
|
# Use either redirect_stderr or stderr_logfile, not both
|
||||||
|
redirect_stderr=true
|
||||||
stdout_logfile=/tmp/app-8001.log
|
stdout_logfile=/tmp/app-8001.log
|
||||||
stderr_logfile=/tmp/app-8001-error.log
|
|
||||||
stdout_logfile_maxbytes=50MB
|
stdout_logfile_maxbytes=50MB
|
||||||
stdout_logfile_backups=10
|
stdout_logfile_backups=10
|
||||||
environment=PORT=8001,PYTHONUNBUFFERED=1
|
environment=PORT=8001,PYTHONUNBUFFERED=1,PYTHONPATH=/projects/bloggingapi-a05jzl
|
Loading…
x
Reference in New Issue
Block a user