# Bot-Spawner Railway Deployment Guide

## Prerequisites

- Railway CLI installed and authenticated (`railway login`)
- Access to `buzztrail-voice-agent` Railway project
- Environment variables from slide-system service

## Deployment Steps

### Step 1: Create New Service in Railway Dashboard

1. Go to https://railway.app/dashboard
2. Select project: **buzztrail-voice-agent**
3. Click **"+ New Service"**
4. Select **"GitHub Repo"** → Choose `BuzzTrail/video-extension`
5. Service name: **bot-spawner**
6. Root directory: `apps/bot-spawner`

### Step 2: Configure Build Settings

Railway will automatically detect the `railway.json` configuration:
- Builder: DOCKERFILE
- Dockerfile path: `apps/bot-spawner/Dockerfile`
- Start command: `node dist/apps/bot-spawner/main.js`
- Health check: `/health`

### Step 3: Set Environment Variables

Copy these from the **slide-system** service:

**Required:**
```bash
SUPABASE_URL=https://dwdytxxqcvlrmyemonxz.supabase.co
SUPABASE_KEY={from slide-system SUPABASE_SERVICE_KEY}
LIVEKIT_URL={from slide-system}
LIVEKIT_API_KEY={from slide-system}
LIVEKIT_API_SECRET={from slide-system}
```

**Optional:**
```bash
PORT=3001  # Default, Railway overrides automatically
PYTHON_VERSION=3.11  # For health check display
```

### Step 4: Deploy

1. Railway will auto-deploy on commit to `main` branch
2. Or manually trigger deployment:
   - Go to service → **Deployments** tab
   - Click **"Deploy"** button
   - Select branch: `main`

### Step 5: Verify Deployment

**Check Build Logs:**
```bash
# Via CLI (if linked)
railway logs --service bot-spawner

# Via Dashboard
Project → bot-spawner → Deployments → Latest → Logs
```

**Test Health Endpoint:**
```bash
# Get the service URL from Railway dashboard
curl https://bot-spawner-production-XXXX.up.railway.app/health
```

Expected response:
```json
{
  "status": "healthy",
  "timestamp": "2025-10-21T09:00:00.000Z",
  "bots_active": 0,
  "python_version": "3.11",
  "service": "bot-spawner",
  "node_version": "v20.x.x",
  "supabase_configured": true
}
```

### Step 6: Validate Internal Networking

From the **slide-system** service:
```bash
# Via Railway CLI
railway run --service slide-system \
  curl http://bot-spawner.railway.internal:3001/health

# Expected: Same JSON response as public endpoint
```

## Troubleshooting

### Build Failures

**Issue:** Dockerfile fails to build
- Check logs for specific error
- Verify base image (ubuntu:24.04) is accessible
- Ensure pnpm lockfile is committed

**Issue:** Python 3.11 not found
- Verify deadsnakes PPA is added correctly
- Check `apt-get update` succeeded after PPA add

### Runtime Failures

**Issue:** Health check returns 503
- Check Supabase credentials are correct
- Verify `SUPABASE_URL` and `SUPABASE_KEY` env vars set
- Check `active_bots` table exists in Supabase

**Issue:** Service won't start
- Check logs for startup errors
- Verify `dist/apps/bot-spawner/main.js` exists after build
- Ensure PORT is accessible (Railway provides PORT env var)

### Network Issues

**Issue:** Internal networking doesn't work
- Verify service name is exactly `bot-spawner` (DNS name)
- Check both services are in same Railway project
- Try `bot-spawner.railway.internal` or `bot-spawner.railway.internal:3001`

## Manual Deployment via CLI

If you need to deploy manually:

```bash
# 1. Link to Railway project
cd /path/to/epic-bot-spawner-extraction
railway link

# Select:
# - Workspace: Buzztrail
# - Project: buzztrail-voice-agent
# - Service: bot-spawner (or create new)

# 2. Set environment variables
railway variables set SUPABASE_URL="https://..."
railway variables set SUPABASE_KEY="..."
railway variables set LIVEKIT_URL="wss://..."
railway variables set LIVEKIT_API_KEY="..."
railway variables set LIVEKIT_API_SECRET="..."

# 3. Deploy
railway up

# 4. Monitor deployment
railway logs
```

## Environment Variable Reference

| Variable | Required | Source | Purpose |
|----------|----------|--------|---------|
| `SUPABASE_URL` | Yes | slide-system | Supabase project URL |
| `SUPABASE_KEY` | Yes | slide-system | Supabase service role key |
| `LIVEKIT_URL` | Yes | slide-system | LiveKit server URL |
| `LIVEKIT_API_KEY` | Yes | slide-system | LiveKit API key |
| `LIVEKIT_API_SECRET` | Yes | slide-system | LiveKit API secret |
| `PORT` | No | Railway auto | Service port (default: 3001) |
| `PYTHON_VERSION` | No | Manual | Python version for display |

## Next Steps

After successful deployment:

1. **Update slide-system** to call bot-spawner API
2. **Test bot spawning** via new HTTP endpoint
3. **Monitor logs** for asyncio hang debugging
4. **Iterate on instrumentation** based on findings

## Reference

- Railway Dashboard: https://railway.app/dashboard
- Project: buzztrail-voice-agent (ID: 655e15d5-19b0-40a0-b6a4-d556806f3746)
- GitHub Repo: https://github.com/BuzzTrail/video-extension
- Epic: #164 Bot Spawner Extraction
- Task: #162 Create Bot-Spawner NX App & Railway Service
