# Deployment Guide with Redis Failover

## Problem
Deployment fails when Redis is unavailable because `php artisan cache:clear` and `php artisan optimize` try to connect to Redis.

## Solutions

### Option 1: Use Smart Deployment Script (Recommended)

Replace your hosting panel deployment script with `deploy-smart.sh`:

```bash
#!/bin/bash
# Copy deploy-smart.sh content here
```

This script:
- Checks Redis availability before cache operations
- Skips cache operations if Redis is down
- Provides clear warnings about Redis status
- Completes deployment even if Redis is unavailable

### Option 2: Use Safe Deployment Script

Use `deploy-redis-safe.sh` which:
- Uses `|| echo` to continue even if cache commands fail
- Provides clear output about what failed
- Completes deployment successfully

### Option 3: Quick Fix for Current Deployment

Update your current deployment script to handle Redis failures:

```bash
#!/bin/bash
set -e  # stop on first error

cd ~/public_html

git stash

echo "▶ Pulling latest code..."
git pull origin master

echo "▶ Setting environment file..."
if [[ "master" == "master" || "master" == "tag" ]]; then
  cp .env.prod .env
  echo "  → Using .env.prod"
elif [[ "master" == "main" ]]; then
  cp .env.dev .env
  echo "  → Using .env.dev"
else
  echo "  ⚠ No env mapping found, skipping cp"
fi

echo "▶ Installing Composer dependencies..."
composer install --prefer-dist --no-progress --no-dev --optimize-autoloader

echo "▶ Running artisan commands..."

# Clear caches that don't depend on Redis
php artisan config:clear
php artisan route:clear
php artisan view:clear
php artisan event:clear
php artisan clear-compiled

# Try to clear cache, but continue if Redis is unavailable
php artisan cache:clear || echo "  ⚠ Cache clear skipped (Redis may be down)"

# Optimize application
php artisan config:cache
php artisan route:cache
php artisan view:cache

# Try to optimize cache, but continue if Redis is unavailable
php artisan cache:optimize || echo "  ⚠ Cache optimize skipped (Redis may be down)"

echo "✅ Deploy complete."
```

## How to Update Deployment Script

### For cPanel/WHM Hosting:

1. Go to **Deployment** or **Git Version Control**
2. Edit the deployment script
3. Replace with one of the scripts above
4. Save and test

### For Manual Deployment:

```bash
# Make script executable
chmod +x deploy-smart.sh

# Run deployment
./deploy-smart.sh
```

## Why This Works

### The Problem:
- Laravel's failover cache works at **runtime**
- During deployment, artisan commands try to connect to Redis directly
- If Redis is down, deployment fails

### The Solution:
- Skip cache operations during deployment if Redis is unavailable
- Application will automatically use database cache at runtime
- Deployment completes successfully
- No manual intervention needed

## Verification

After deployment, verify the application works:

```bash
# Check application health
curl https://your-domain.com/health

# Check cache store being used
php artisan tinker
>>> config('cache.default')
// Should return: "failover"
```

## Monitoring

### Check Redis Status:

```bash
# If using Docker
docker ps | grep redis

# If using system Redis
redis-cli ping

# Check Redis logs
docker logs cbt-redis --tail 50
```

### Auto-Recovery:

If Redis is down, the monitoring script will auto-restart it:

```bash
# Check monitoring logs
tail -f /var/log/redis-monitor.log
```

## Best Practices

1. **Always use smart deployment script** - handles Redis gracefully
2. **Monitor Redis health** - use monitoring script
3. **Test deployment** - run in staging first
4. **Keep Redis running** - prevents cache degradation
5. **Have fallback ready** - database cache always available

## Troubleshooting

### Deployment Still Fails:

1. Check if Redis is running
2. Verify Redis configuration in .env
3. Check Redis logs
4. Try manual deployment with smart script

### Application Slow After Deployment:

1. Redis is probably down
2. Application is using database cache
3. Restart Redis or check monitoring
4. Cache will rebuild automatically

### Cache Not Working:

1. Clear all caches manually:
   ```bash
   php artisan cache:clear
   php artisan config:clear
   php artisan route:clear
   php artisan view:clear
   ```

2. Verify failover configuration:
   ```bash
   php artisan tinker
   >>> config('cache.default')
   ```

3. Check cache store status:
   ```bash
   php artisan tinker
   >>> Cache::store('redis')->put('test', 'value', 60)
   >>> Cache::store('database')->put('test', 'value', 60)
   ```