---
name: Create Bot-Spawner NX App & Railway Service
status: closed
created: 2025-10-20T15:30:53Z
updated: 2025-10-21T13:24:17Z
completed: 2025-10-21T09:56:42Z
github: https://github.com/BuzzTrail/video-extension/issues/162
depends_on: [155, 157]
parallel: false
conflicts_with: []
---

# Task: Create Bot-Spawner NX App & Railway Service

## Description

Create the foundational bot-spawner service infrastructure:
1. NX app skeleton with Express HTTP server
2. Dockerfile with Node.js 20 + Python 3.11 (dual runtime)
3. Basic health endpoint
4. Railway project and deployment
5. Verify service accessible via internal networking

This creates the **container** for bot spawning logic (added in Task 004).

**Prerequisites:** Tasks 001 and 002 must complete successfully (networking + Python validation).

## Acceptance Criteria

- [ ] NX app created: `apps/bot-spawner`
- [ ] Express server with `/health` endpoint returning JSON status
- [ ] Dockerfile builds successfully with both Node.js 20 and Python 3.11
- [ ] Railway project created: `bot-spawner`
- [ ] Service deploys successfully to Railway
- [ ] Health endpoint accessible via `https://bot-spawner-<id>.up.railway.app/health`
- [ ] Service accessible via Railway internal networking (verified from slide-system)
- [ ] Environment variables configured (Supabase, LiveKit credentials)
- [ ] Build time <10 minutes

## Technical Details

### Implementation Approach

**1. Create NX App:**
```bash
cd /Users/lars/mycode/buzztrail/extension
pnpm nx generate @nx/node:application bot-spawner
```

**2. Express Server (`apps/bot-spawner/src/main.ts`):**
```typescript
import express from 'express';
import { createClient } from '@supabase/supabase-js';

const app = express();
const PORT = process.env.PORT || 3001;

// Health check endpoint
app.get('/health', async (req, res) => {
  try {
    // Test Supabase connection
    const supabase = createClient(
      process.env.SUPABASE_URL!,
      process.env.SUPABASE_SERVICE_KEY!
    );
    const { count } = await supabase.from('active_bots').select('*', { count: 'exact', head: true });

    res.json({
      status: 'healthy',
      timestamp: new Date().toISOString(),
      bots_active: count || 0,
      python_version: '3.11', // TODO: Actually check Python
    });
  } catch (error) {
    res.status(503).json({
      status: 'unhealthy',
      error: error.message,
    });
  }
});

app.listen(PORT, () => {
  console.log(`Bot-spawner service listening on port ${PORT}`);
});
```

**3. Dockerfile (`apps/bot-spawner/Dockerfile`):**
```dockerfile
FROM ubuntu:24.04

# Install Node.js 20
RUN apt-get update && apt-get install -y curl
RUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash -
RUN apt-get install -y nodejs

# Install Python 3.11 via deadsnakes PPA
RUN apt-get install -y software-properties-common
RUN add-apt-repository ppa:deadsnakes/ppa
RUN apt-get update && apt-get install -y \
    python3.11 \
    python3.11-venv \
    python3.11-dev

# Install pnpm
RUN npm install -g pnpm

# Copy app code
WORKDIR /app
COPY package.json pnpm-lock.yaml ./
RUN pnpm install --frozen-lockfile

COPY . .

# Expose port
EXPOSE 3001

# Start Express server
CMD ["node", "dist/apps/bot-spawner/main.js"]
```

**4. Railway Configuration:**
- Create Railway project via CLI: `railway init`
- Link to service: `railway link`
- Set environment variables via Railway dashboard
- Deploy: `railway up`

### Key Considerations

- **Dual Runtime:** Container needs both Node.js (Express) and Python (bots)
- **Build Optimization:** Layer caching for faster rebuilds
- **Environment Variables:** Copy from slide-system (Supabase, LiveKit)
- **Internal Networking:** Service must be named consistently for `.railway.internal` DNS
- **Python Validation:** Verify Python 3.11 accessible in container

### Files Affected

**New files:**
- `apps/bot-spawner/src/main.ts` (Express server)
- `apps/bot-spawner/Dockerfile`
- `apps/bot-spawner/project.json` (NX config)
- `apps/bot-spawner/tsconfig.json`
- `apps/bot-spawner/package.json`
- `apps/bot-spawner/.dockerignore`
- `apps/bot-spawner/railway.json` (Railway config)

## Dependencies

**Depends On:**
- [ ] Task 001: Railway internal networking validated
- [ ] Task 002: Python 3.11 asyncio working on Railway

**External:**
- [ ] Railway CLI installed and authenticated
- [ ] Railway project quota available
- [ ] NX workspace functional

## Effort Estimate

- **Size:** M (Medium)
- **Hours:** 8-12 hours
  - NX app setup: 2 hours
  - Express server: 2 hours
  - Dockerfile creation: 3-4 hours (iterate on build process)
  - Railway deployment: 2-3 hours (config, env vars, troubleshooting)
  - Testing and validation: 1 hour
- **Parallel:** false (blocks Task 004)

## Definition of Done

- [ ] NX app compiles with `pnpm nx build bot-spawner`
- [ ] Docker image builds locally with `docker build`
- [ ] Health endpoint returns 200 OK when deployed
- [ ] Service accessible via both:
  - Public: `https://bot-spawner-<id>.up.railway.app/health`
  - Internal: `http://bot-spawner.railway.internal:3001/health`
- [ ] Python 3.11 accessible in container: `python3.11 --version`
- [ ] Railway logs show successful startup
- [ ] Environment variables configured correctly
- [ ] Documentation updated in README

**Validation:**
```bash
# Test public health endpoint
curl https://bot-spawner-<id>.up.railway.app/health

# Test from slide-system (internal networking)
railway run --service slide-system \
  curl http://bot-spawner.railway.internal:3001/health
```
