improve migrations and fix logging problem with mqtt

This commit is contained in:
Joel Krauska
2025-11-04 16:27:18 -08:00
parent 0a0ec5c45f
commit b86af326af
4 changed files with 30 additions and 33 deletions
-28
View File
@@ -1,28 +0,0 @@
FROM python:3.12-slim
# Set work directory
WORKDIR /app
# Install system dependencies (graphviz required, git for cloning)
RUN apt-get update && \
apt-get install -y --no-install-recommends git graphviz && \
rm -rf /var/lib/apt/lists/*
# Clone the repo with submodules
RUN git clone --recurse-submodules https://github.com/pablorevilla-meshtastic/meshview.git /app
# Create virtual environment
RUN python -m venv /app/env
# Upgrade pip and install requirements in venv
RUN /app/env/bin/pip install --no-cache-dir --upgrade pip && \
/app/env/bin/pip install --no-cache-dir -r /app/requirements.txt
# Copy sample config
RUN cp /app/sample.config.ini /app/config.ini
# Expose port
EXPOSE 8081
# Run the app via venv
CMD ["/app/env/bin/python", "/app/mvrun.py"]
+6
View File
@@ -110,13 +110,19 @@ def run_migrations(database_url: str) -> None:
database_url: Database connection string
"""
logger.info("Running database migrations...")
import sys
sys.stdout.flush()
config = get_alembic_config(database_url)
try:
# Run migrations to head
logger.info("Calling alembic upgrade command...")
sys.stdout.flush()
command.upgrade(config, "head")
logger.info("Database migrations completed successfully")
sys.stdout.flush()
except Exception as e:
logger.error(f"Error running migrations: {e}")
raise
+1 -1
View File
@@ -63,7 +63,7 @@ def run_script(python_executable, script_name, pid_file, *args):
process = None
try:
# Combine the script name and arguments
command = [python_executable, script_name] + list(args)
command = [python_executable, '-u', script_name] + list(args)
# Run the subprocess (output goes directly to console for real-time viewing)
process = subprocess.Popen(command)
+23 -4
View File
@@ -8,6 +8,15 @@ from sqlalchemy import delete
from meshview import migrations, models, mqtt_database, mqtt_reader, mqtt_store
from meshview.config import CONFIG
# -------------------------
# Basic logging configuration
# -------------------------
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s %(filename)s:%(lineno)d [pid:%(process)d] %(levelname)s - %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
# -------------------------
# Logging for cleanup
# -------------------------
@@ -149,16 +158,23 @@ async def main():
logger.info("Migration status set to 'in progress'")
try:
# Run any pending migrations (synchronous operation)
# Check if migrations are needed before running them
logger.info("Checking for pending database migrations...")
migrations.run_migrations(database_url)
logger.info("Database migrations check complete")
if await migrations.is_database_up_to_date(mqtt_database.engine, database_url):
logger.info("Database schema is already up to date, skipping migrations")
else:
logger.info("Database schema needs updating, running migrations...")
migrations.run_migrations(database_url)
logger.info("Database migrations completed")
# Create tables if needed (for backwards compatibility)
logger.info("Creating database tables...")
await mqtt_database.create_tables()
logger.info("Database tables created")
finally:
# Clear migration in progress flag
logger.info("Clearing migration status...")
await migrations.set_migration_in_progress(mqtt_database.engine, False)
logger.info("Migration status cleared - database ready")
@@ -172,7 +188,10 @@ async def main():
cleanup_hour = get_int(CONFIG, "cleanup", "hour", 2)
cleanup_minute = get_int(CONFIG, "cleanup", "minute", 0)
logger.info("Starting MQTT ingestion and cleanup tasks...")
logger.info(f"Starting MQTT ingestion from {CONFIG['mqtt']['server']}:{CONFIG['mqtt']['port']}")
if cleanup_enabled:
logger.info(f"Daily cleanup enabled: keeping {cleanup_days} days of data")
async with asyncio.TaskGroup() as tg:
tg.create_task(
load_database_from_mqtt(