# Playwright API Server A FastAPI-based server that uses Playwright to scrape websites and extract various types of data. ## Recent Migration (v3.0) ### Migration from Puppeteer to Playwright This project has been successfully migrated from Puppeteer (pyppeteer) to Playwright for better performance, reliability, and maintainability. #### Key Benefits of Playwright - **Better performance**: More efficient browser management and faster page loads - **Improved stability**: Better handling of modern web applications - **Enhanced features**: Better support for modern web standards - **Active development**: More frequent updates and better community support - **Multi-browser support**: Can easily switch between Chromium, Firefox, and WebKit #### Migration Changes - **Dependencies**: Updated from `pyppeteer` to `playwright` - **Browser management**: Improved browser pool with better resource management - **API compatibility**: All existing endpoints remain the same - **Docker image**: Updated to use Playwright browsers instead of Chrome ## Recent Fixes (v2.0) ### Issues Fixed - **Resource temporarily unavailable (Errno 11)** - Fixed by implementing proper browser cleanup and resource limits - **100% CPU usage** - Fixed by reducing browser pool size, adding operation limits, and improving cleanup - **Server hanging** - Fixed by adding timeouts, emergency cleanup, and better error handling ### Key Improvements #### 1. Resource Management - **Reduced browser pool size**: From 5 to 3 browsers maximum - **Shorter browser TTL**: From 1 hour to 30 minutes - **Concurrent operation limits**: Maximum 5 concurrent operations - **Queue size limits**: Prevent unbounded growth #### 2. Better Cleanup - **Automatic browser recycling**: Browsers are recycled every 30 minutes - **Emergency cleanup**: Force cleanup all browsers when needed - **Proper page cleanup**: Ensure pages are closed after each operation - **Signal handlers**: Graceful shutdown on SIGTERM/SIGINT #### 3. Monitoring & Debugging - **System status endpoint**: `/status` - Monitor CPU, memory, file descriptors - **Browser pool monitoring**: Track active browsers and their ages - **Emergency cleanup endpoint**: `/emergency-cleanup` - Force cleanup when needed - **Timeout-based cleanup**: `/force-cleanup-old` - Force close old browser/page instances - **Monitoring script**: `monitor.py` - Real-time system monitoring #### 4. Timeout Management - **Configurable timeout**: `BROWSER_INSTANCE_TIMEOUT_MINUTES` (default: 10 minutes) - **Automatic cleanup**: Old browser and page instances are automatically force closed - **Page tracking**: All page instances are tracked with creation timestamps - **Health check integration**: Timeout cleanup runs every 2 minutes as part of health checks #### 5. Error Handling - **Timeout handling**: 30s timeout for getting browsers from pool - **Emergency recovery**: Auto-cleanup when timeouts occur - **Better exception handling**: More detailed error logging ## Environment Variables ```bash # Required API_KEY=your-api-key-here # Optional (with defaults) MAX_BROWSERS=3 # Maximum browser instances (default: 3) BROWSER_TTL=1800 # Browser time-to-live in seconds (default: 1800 = 30min) MAX_CONCURRENT_OPERATIONS=5 # Max concurrent operations (default: 5) BROWSER_INSTANCE_TIMEOUT_MINUTES=10 # Force close old browser/page instances after N minutes (default: 10) CACHE_EXPIRY_HOURS=36 # Cache expiry in hours (default: 36) CLEANUP_CRON=0 3 * * * # Cache cleanup schedule (default: daily at 3 AM) RATE_LIMIT_MINUTE=60 # Requests per minute (default: 60) ``` ## API Endpoints ### Core Endpoints - `GET /` - Visit URL and get HTML content - `GET /seo` - Extract SEO information - `GET /meta` - Extract meta tags and Open Graph data - `GET /json` - Fetch JSON content from a website - `GET /resulting-url` - Get the final URL after navigation (handles redirects) The `/json` endpoint intelligently extracts JSON data from websites by: - Directly parsing JSON responses (when Content-Type is application/json) - Extracting JSON from `