#!/bin/bash # Monitor and cleanup stuck commands in the queue # This script should be run every 5 minutes via launchd set -e DB_URL="postgres://accusys@localhost:5432/momentry" LOG_FILE="/Users/accusys/momentry_core/logs/command_monitor.log" ALERT_THRESHOLD_STUCK=5 ALERT_THRESHOLD_AGE_MINUTES=30 log() { echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" >> "$LOG_FILE" } # Check for stuck commands (processing > 1 hour) check_stuck_commands() { STUCK_COUNT=$(psql "$DB_URL" -t -c " SELECT COUNT(*) FROM command_queue WHERE status = 'processing' AND processing_started_at < NOW() - INTERVAL '1 hour' " | tr -d ' ') if [ "$STUCK_COUNT" -gt 0 ]; then log "⚠️ Found $STUCK_COUNT stuck commands (processing > 1 hour)" # Reset stuck commands RESET_COUNT=$(psql "$DB_URL" -t -c " UPDATE command_queue SET status = 'pending', consumer_id = NULL, consumer_group = NULL, processing_started_at = NULL, retry_count = retry_count + 1 WHERE status = 'processing' AND processing_started_at < NOW() - INTERVAL '1 hour' AND retry_count < max_retries RETURNING COUNT(*) " | tr -d ' ') log "✅ Reset $RESET_COUNT stuck commands" # Mark commands with max retries as failed FAILED_COUNT=$(psql "$DB_URL" -t -c " UPDATE command_queue SET status = 'failed', last_error = 'Command timed out after max retries' WHERE status = 'processing' AND processing_started_at < NOW() - INTERVAL '1 hour' AND retry_count >= max_retries RETURNING COUNT(*) " | tr -d ' ') if [ "$FAILED_COUNT" -gt 0 ]; then log "❌ Marked $FAILED_COUNT commands as failed (exceeded max retries)" fi fi } # Check for commands with non-existent files check_orphan_commands() { ORPHAN_COUNT=$(psql "$DB_URL" -t -c " SELECT COUNT(*) FROM command_queue WHERE status IN ('pending', 'processing') AND value->>'file_uuid' NOT IN (SELECT file_uuid FROM videos) " | tr -d ' ') if [ "$ORPHAN_COUNT" -gt 0 ]; then log "⚠️ Found $ORPHAN_COUNT commands with non-existent files" # Mark as failed psql "$DB_URL" -c " UPDATE command_queue SET status = 'failed', last_error = 'File not found in database' WHERE status IN ('pending', 'processing') AND value->>'file_uuid' NOT IN (SELECT file_uuid FROM videos) " >> "$LOG_FILE" 2>&1 log "❌ Marked $ORPHAN_COUNT orphan commands as failed" fi } # Check for commands with completed files check_completed_files() { COMPLETED_COUNT=$(psql "$DB_URL" -t -c " SELECT COUNT(*) FROM command_queue c JOIN videos v ON c.value->>'file_uuid' = v.file_uuid WHERE c.status IN ('pending', 'processing') AND v.status = 'completed' " | tr -d ' ') if [ "$COMPLETED_COUNT" -gt 0 ]; then log "⚠️ Found $COMPLETED_COUNT commands for completed files" # Mark as failed (pipeline not needed) psql "$DB_URL" -c " UPDATE command_queue c SET status = 'failed', last_error = 'File already completed, pipeline commands not needed' FROM videos v WHERE c.value->>'file_uuid' = v.file_uuid AND c.status IN ('pending', 'processing') AND v.status = 'completed' " >> "$LOG_FILE" 2>&1 log "❌ Marked $COMPLETED_COUNT commands for completed files as failed" fi } # Check worker process check_worker_process() { WORKER_PID=$(ps aux | grep '[p]ipeline_worker' | awk '{print $2}') if [ -z "$WORKER_PID" ]; then log "🚨 Pipeline worker process not found!" # Attempt to restart launchctl start gui/$(id -u)/com.momentry.pipeline_worker 2>&1 >> "$LOG_FILE" log "✅ Attempted to restart pipeline worker" fi } # Check queue health check_queue_health() { PENDING=$(psql "$DB_URL" -t -c "SELECT COUNT(*) FROM command_queue WHERE status = 'pending'" | tr -d ' ') PROCESSING=$(psql "$DB_URL" -t -c "SELECT COUNT(*) FROM command_queue WHERE status = 'processing'" | tr -d ' ') FAILED=$(psql "$DB_URL" -t -c "SELECT COUNT(*) FROM command_queue WHERE status = 'failed'" | tr -d ' ') log "📊 Queue status: pending=$PENDING, processing=$PROCESSING, failed=$FAILED" # Alert if too many failed if [ "$FAILED" -gt 20 ]; then log "🚨 Too many failed commands: $FAILED" fi } # Main main() { log "=== Starting command queue monitor ===" check_worker_process check_stuck_commands check_orphan_commands check_completed_files check_queue_health log "=== Monitor completed ===" } main