immortal-bnb/docs/RUNBOOKS.md
caelum0x bea907f76e feat: Complete production readiness implementation
- Integrated all production features into main application flow
- Added metrics, tracing, caching, and health checks to API server
- Enhanced trade execution with circuit breakers and retry policies
- Integrated database persistence for trades and metrics
- Added structured logging and observability
- Enhanced middleware with missing validators and rate limiters
- Added comprehensive error handling and resilience patterns
2025-11-20 00:35:39 +03:00

4.3 KiB

Operational Runbooks

Common operational procedures for the Immortal AI Trading Bot.

Table of Contents

  1. Starting the Bot
  2. Stopping the Bot
  3. Checking Bot Status
  4. Viewing Logs
  5. Restarting Services
  6. Database Maintenance
  7. Clearing Cache
  8. Rotating API Keys
  9. Emergency Stop

Starting the Bot

Via API

curl -X POST http://localhost:3001/api/start-bot \
  -H "Content-Type: application/json" \
  -d '{
    "tokens": [],
    "risk": 5
  }'

Via Docker

docker-compose exec backend bun run src/index.ts

Stopping the Bot

Via API

curl -X POST http://localhost:3001/api/stop-bot

Via Docker

docker-compose stop backend

Checking Bot Status

# Basic status
curl http://localhost:3001/api/bot-status

# Health check
curl http://localhost:3001/health

# Detailed health
curl http://localhost:3001/api/health

Viewing Logs

Docker Logs

# All services
docker-compose logs -f

# Backend only
docker-compose logs -f backend

# Last 100 lines
docker-compose logs --tail=100 backend

Log Files

# Error logs
tail -f logs/error.log

# Combined logs
tail -f logs/combined.log

# Structured JSON logs
tail -f logs/structured.json | jq

Restarting Services

# Restart all services
docker-compose restart

# Restart specific service
docker-compose restart backend

# Full restart (stop and start)
docker-compose down
docker-compose up -d

Database Maintenance

Run Migrations

bunx prisma migrate deploy

Check Database Size

psql $DATABASE_URL -c "SELECT pg_size_pretty(pg_database_size(current_database()));"

Vacuum Database

psql $DATABASE_URL -c "VACUUM ANALYZE;"

Check Table Sizes

psql $DATABASE_URL -c "
SELECT 
  schemaname,
  tablename,
  pg_size_pretty(pg_total_relation_size(schemaname||'.'||tablename)) AS size
FROM pg_tables
WHERE schemaname = 'public'
ORDER BY pg_total_relation_size(schemaname||'.'||tablename) DESC;
"

Clearing Cache

Redis Cache

# Connect to Redis
redis-cli

# Clear all cache
FLUSHALL

# Clear specific pattern
KEYS "immortal-bot:token:*" | xargs redis-cli DEL

Application Cache

# Via API (if endpoint exists)
curl -X POST http://localhost:3001/api/cache/clear

Rotating API Keys

Generate New API Key

# Via script
bun run scripts/generate-api-key.ts

# Or manually
node -e "console.log(require('crypto').randomBytes(32).toString('hex'))"

Update Environment

  1. Add new key to .env.production
  2. Update API clients
  3. Revoke old key after migration period

Emergency Stop

Immediate Stop

# Stop bot
curl -X POST http://localhost:3001/api/stop-bot

# Pause wallet
# (Requires wallet manager integration)

Complete Shutdown

# Stop all services
docker-compose down

# Stop database (if local)
docker-compose stop postgres

Performance Tuning

Increase Memory Limits

Edit docker-compose.prod.yml:

services:
  backend:
    deploy:
      resources:
        limits:
          memory: 2G

Optimize Database

# Analyze query performance
psql $DATABASE_URL -c "EXPLAIN ANALYZE SELECT * FROM \"Trade\" WHERE \"tokenAddress\" = '0x...';"

# Create indexes if needed
bunx prisma migrate dev --create-only

Monitoring

Check Metrics

# Prometheus metrics
curl http://localhost:3001/metrics

# Specific metric
curl http://localhost:3001/metrics | grep trades_total

View Grafana Dashboards

  1. Open Grafana: http://localhost:3000
  2. Navigate to dashboards
  3. Select relevant dashboard

Troubleshooting Common Issues

Bot Not Starting

  1. Check logs: docker-compose logs backend
  2. Verify environment variables
  3. Check database connection
  4. Verify wallet balance

High Error Rate

  1. Check error logs: tail -f logs/error.log
  2. Review recent errors
  3. Check external service status
  4. Review circuit breaker states

Slow Performance

  1. Check database query performance
  2. Review cache hit rates
  3. Check API response times
  4. Review system resources