feat(ocr): Add memory management and auto-restart for OCR stability

OCR Service improvements:
- Single worker ThreadPoolExecutor to prevent parallel OCR memory accumulation
- Semaphore to ensure only one OCR operation at a time
- Explicit numpy array cleanup after each OCR step
- Forced garbage collection after every OCR request
- Memory threshold check (2500MB) with pre-processing GC
- Memory usage logging before/after processing

Backend auto-restart:
- Add run-with-restart.sh wrapper script for uvicorn
- Auto-restart on crash (OOM, etc.) with max 10 restarts
- Reset restart counter if process runs >60s (stable)
- Graceful exit on SIGINT/SIGTERM
- Update start-prod.sh and start-test.sh to use wrapper

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-12-30 22:03:51 +02:00
parent c5682ead42
commit 51d736addf
4 changed files with 159 additions and 7 deletions

View File

@@ -145,9 +145,9 @@ else
source .env
set +a
# Start backend with reload
print_message "Starting unified backend (includes Reports, Data Entry, and Telegram bot)..."
nohup uvicorn main:app --host 0.0.0.0 --port 8000 --reload > /tmp/unified_backend_prod.log 2>&1 &
# Start backend with auto-restart on crash (OOM protection)
print_message "Starting unified backend with auto-restart (includes Reports, Data Entry, and Telegram bot)..."
nohup ./run-with-restart.sh 8000 /tmp/unified_backend_prod.log > /dev/null 2>&1 &
cd - > /dev/null