Production Deployment
Guide to deploying MMPS to production.
Pre-Deployment Checklist
- ✅ All tests pass:
npm test - ✅ Code lints:
npm run lint - ✅ Build succeeds:
npm run build - ✅ No secrets in code
- ✅ Environment variables configured
- ✅ Database backup in place
- ✅ Monitoring set up
Building for Production
bash
# Build TypeScript
npm run build
# Verify build
npm start
# Check logs for errorsEnvironment Variables
Set all required variables in production:
bash
# Application
IS_PROD=true
# MongoDB
MONGO_URI=mongodb+srv://user:pass@cluster.mongodb.net/
# AI APIs
OPENAI_API_KEY=sk-...
ANTHROPIC_API_KEY=sk-ant-...
# Bot Tokens
CHATBOT_TELEGRAM_BOT_TOKEN=...
COACH_TELEGRAM_BOT_TOKEN=...
# ... all bot tokens
# Google Sheets Logging
SHEETS_CLIENT_EMAIL=...
SHEETS_PRIVATE_KEY=...
SHEETS_LOGS_SPREADSHEET_ID=...Running in Production
Using Node
bash
npm startUsing PM2
bash
# Install globally
npm install -g pm2
# Start with PM2
pm2 start dist/index.js --name mmps
# Monitor
pm2 monit
# View logs
pm2 logs mmpsUsing Docker
bash
# Build image
docker build -t mmps .
# Run container
docker run -e MONGO_URI=... -e OPENAI_API_KEY=... mmpsMonitoring & Logging
Google Sheets Logging
Logs are automatically sent to Google Sheets in production:
typescript
// Configure service account
SHEETS_CLIENT_EMAIL=...@iam.gserviceaccount.com
SHEETS_PRIVATE_KEY=-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n
SHEETS_LOGS_SPREADSHEET_ID=your-spreadsheet-idLog Levels
log()- General infowarn()- Warningserror()- Errorsdebug()- Debug info (dev only)
Monitoring Checklist
- Monitor error rates
- Check API rate limits
- Monitor MongoDB connections
- Track bot response times
- Monitor memory usage
Scaling
For high traffic:
- Load Balancing - Run multiple instances behind a load balancer
- Database Replication - Use MongoDB Atlas for managed replication
- Caching - Implement Redis for frequently accessed data
- Scheduled Jobs - Consider separate process for schedulers
Backup & Recovery
MongoDB Backups
bash
# Manual backup
mongodump --uri "mongodb+srv://..." --out ./backup
# Restore from backup
mongorestore ./backupUsing MongoDB Atlas
- Automated backups enabled by default
- Point-in-time restore available
- Scheduled backup snapshots
Troubleshooting
Bot Not Responding
- Check bot token:
curl -X GET https://api.telegram.org/bot{TOKEN}/getMe - Check MongoDB:
mongosh "mongodb+srv://..." - Check logs:
pm2 logs mmps
Memory Leak
bash
# Monitor memory usage
pm2 monit
# Restart on memory threshold
pm2 set mmps restart_delay 3600000Rate Limiting
- Implement exponential backoff
- Cache API responses
- Use bulk operations