diff --git a/DEPLOYMENT.md b/DEPLOYMENT.md index fc05c91..127c5dd 100644 --- a/DEPLOYMENT.md +++ b/DEPLOYMENT.md @@ -133,6 +133,44 @@ wrangler pages deploy dist --project-name=aicodebattle ## Monitoring +### Health Endpoints + +The Worker API provides two health endpoints for monitoring: + +- **Liveness**: `GET /health` or `GET /api/health` + - Returns 200 if the worker process is running + - Use for Kubernetes liveness probes + +- **Readiness**: `GET /ready` or `GET /api/ready` + - Returns 200 if database is connected and ready + - Returns 503 if database is unavailable + - Use for Kubernetes readiness probes + +Example responses: + +```json +// GET /health +{ + "success": true, + "data": { + "status": "healthy", + "timestamp": "2024-01-15T10:30:00.000Z" + } +} + +// GET /ready (when healthy) +{ + "success": true, + "data": { + "status": "ready", + "database": "connected", + "timestamp": "2024-01-15T10:30:00.000Z" + } +} +``` + +### Cloudflare Monitoring + - Cloudflare Analytics: Available in Cloudflare dashboard - Worker Logs: `wrangler tail` - Container Logs: `docker-compose logs -f` diff --git a/PROGRESS.md b/PROGRESS.md index ba80d8b..9ab790a 100644 --- a/PROGRESS.md +++ b/PROGRESS.md @@ -25,20 +25,23 @@ - Cloudflare setup instructions - Container deployment commands - Troubleshooting guide - -### Remaining Phase 6 Work - -- [ ] Cloudflare Pages project creation and deployment - [x] D1 database schema and migrations - Complete schema.sql with all tables from plan - Added: predictions, predictor_stats, map_votes, replay_feedback, series, series_games, seasons - Added evolution fields to bots table (evolved, island, generation, parent_ids) - Created migrations/0001_initial.sql for D1 migrations - Updated wrangler.toml with migrations_dir config +- [x] Monitoring endpoints + - `/health` - Liveness probe (always returns 200) + - `/ready` - Readiness probe (checks database connectivity, returns 503 if unavailable) + - Documented in DEPLOYMENT.md + +### Remaining Phase 6 Work (requires Cloudflare account access) + +- [ ] Cloudflare Pages project creation and deployment - [ ] R2 bucket creation and custom domain -- [ ] Worker API deployment via Wrangler +- [ ] Worker API deployment via Wrangler (`wrangler deploy`) - [ ] DNS configuration -- [ ] Monitoring setup ### Phase 5 Completed ✅ diff --git a/worker-api/src/index.ts b/worker-api/src/index.ts index 6d7c14b..8b23a69 100644 --- a/worker-api/src/index.ts +++ b/worker-api/src/index.ts @@ -66,9 +66,54 @@ export default { }; try { - // Health check + // Health check (liveness probe - always returns 200 if process is running) if (path === '/health' || path === '/api/health') { - return json({ success: true, data: { status: 'healthy' } }); + return json({ + success: true, + data: { + status: 'healthy', + timestamp: new Date().toISOString(), + }, + }); + } + + // Readiness check (checks if service can handle requests) + if (path === '/ready' || path === '/api/ready') { + try { + // Test database connectivity + const dbResult = await env.DB.prepare('SELECT 1 as ok').first(); + const dbHealthy = dbResult?.ok === 1; + + if (!dbHealthy) { + return json({ + success: false, + data: { + status: 'not_ready', + database: 'error', + timestamp: new Date().toISOString(), + }, + }, 503); + } + + return json({ + success: true, + data: { + status: 'ready', + database: 'connected', + timestamp: new Date().toISOString(), + }, + }); + } catch (error) { + return json({ + success: false, + data: { + status: 'not_ready', + database: 'error', + error: String(error), + timestamp: new Date().toISOString(), + }, + }, 503); + } } // ============ Job Endpoints (require API key) ============