Fix Supervisor startup issues
- Add startup script for production deployment - Create Supervisor configuration file - Update main.py to ensure database initialization at startup - Add configurable port via environment variable - Add Supervisor setup instructions to README - Initialize database at application startup
This commit is contained in:
parent
b6561799a2
commit
221dbad51a
31
README.md
31
README.md
@ -40,7 +40,9 @@ This is a FastAPI application that provides a simple generic REST API service wi
|
||||
│ └── item.py # Item schema
|
||||
├── alembic.ini # Alembic configuration
|
||||
├── main.py # Application entry point
|
||||
└── requirements.txt # Project dependencies
|
||||
├── requirements.txt # Project dependencies
|
||||
├── start.sh # Production startup script
|
||||
└── supervisor.conf # Supervisor configuration
|
||||
```
|
||||
|
||||
## Setup and Installation
|
||||
@ -77,12 +79,39 @@ PYTHONPATH=$PWD alembic upgrade head
|
||||
|
||||
5. Start the application:
|
||||
|
||||
**Development mode:**
|
||||
|
||||
```bash
|
||||
uvicorn main:app --reload
|
||||
```
|
||||
|
||||
The application will be available at http://127.0.0.1:8000.
|
||||
|
||||
**Production mode with Supervisor:**
|
||||
|
||||
The application comes with a Supervisor configuration file for production deployment:
|
||||
|
||||
```bash
|
||||
# Make the start script executable
|
||||
chmod +x start.sh
|
||||
|
||||
# Copy the supervisor configuration to the supervisor config directory
|
||||
cp supervisor.conf /etc/supervisor/conf.d/fastapi-app.conf
|
||||
|
||||
# Restart supervisor to load the new configuration
|
||||
supervisorctl reread
|
||||
supervisorctl update
|
||||
|
||||
# Check the status of the application
|
||||
supervisorctl status app-8001
|
||||
|
||||
# View logs if needed
|
||||
tail -f /var/log/supervisor/app-8001_stdout.log
|
||||
tail -f /var/log/supervisor/app-8001_stderr.log
|
||||
```
|
||||
|
||||
The application will be available at http://127.0.0.1:8001 when running with Supervisor.
|
||||
|
||||
## API Documentation
|
||||
|
||||
Once the application is running, you can access the automatically generated API documentation:
|
||||
|
15
main.py
15
main.py
@ -1,8 +1,13 @@
|
||||
import os
|
||||
import uvicorn
|
||||
from fastapi import FastAPI
|
||||
from app.api.api import api_router
|
||||
from app.core.config import settings
|
||||
|
||||
# Initialize database at startup
|
||||
from app.db.init_db import init_db
|
||||
init_db()
|
||||
|
||||
app = FastAPI(
|
||||
title=settings.PROJECT_NAME,
|
||||
openapi_url=f"{settings.API_V1_STR}/openapi.json",
|
||||
@ -12,5 +17,13 @@ app = FastAPI(
|
||||
|
||||
app.include_router(api_router, prefix=settings.API_V1_STR)
|
||||
|
||||
# Add startup event to ensure proper initialization
|
||||
@app.on_event("startup")
|
||||
async def startup_event():
|
||||
# Any additional initialization can go here
|
||||
print("Application startup complete")
|
||||
|
||||
if __name__ == "__main__":
|
||||
uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True)
|
||||
# Get port from environment variable or default to 8000
|
||||
port = int(os.environ.get("PORT", 8000))
|
||||
uvicorn.run("main:app", host="0.0.0.0", port=port, reload=True)
|
||||
|
11
start.sh
Executable file
11
start.sh
Executable file
@ -0,0 +1,11 @@
|
||||
#!/bin/bash
|
||||
# Start script for the FastAPI application
|
||||
|
||||
# Set environment variables if needed
|
||||
export PYTHONPATH=/app
|
||||
|
||||
# Navigate to the application directory
|
||||
cd /app
|
||||
|
||||
# Start the application with uvicorn
|
||||
exec uvicorn main:app --host 0.0.0.0 --port 8001 --workers 4 --no-access-log
|
14
supervisor.conf
Normal file
14
supervisor.conf
Normal file
@ -0,0 +1,14 @@
|
||||
[program:app-8001]
|
||||
command=/app/start.sh
|
||||
directory=/app
|
||||
user=root
|
||||
autostart=true
|
||||
autorestart=true
|
||||
startretries=5
|
||||
numprocs=1
|
||||
startsecs=1
|
||||
process_name=%(program_name)s_%(process_num)02d
|
||||
stderr_logfile=/var/log/supervisor/%(program_name)s_stderr.log
|
||||
stderr_logfile_maxbytes=10MB
|
||||
stdout_logfile=/var/log/supervisor/%(program_name)s_stdout.log
|
||||
stdout_logfile_maxbytes=10MB
|
Loading…
x
Reference in New Issue
Block a user