mirror of
https://github.com/nomadkaraoke/karaoke-gen.git
synced 2026-11-30 09:14:03 +09:00
## Summary
- Refactor monolithic `infrastructure/__main__.py` from **2,602 lines to
339 lines** (87% reduction)
- Split resources into organized, purpose-specific modules following
SOLID principles
- Extract VM startup scripts to separate `.sh` files for easier
maintenance
- Extract embedded Python encoding worker app to proper package
## Changes
### New Module Structure
```
infrastructure/
├── __main__.py # Entry point (339 lines, was 2,602)
├── config.py # Centralized configuration
├── modules/ # Core GCP resources
│ ├── database.py # Firestore + indexes
│ ├── storage.py # GCS bucket + lifecycle
│ ├── artifact_registry.py # Docker repository
│ ├── secrets.py # Secret Manager secrets
│ ├── cloud_tasks.py # Task queues
│ ├── cloud_run.py # Domain mapping, Cloud Run Jobs
│ ├── monitoring.py # Alert policies
│ └── iam/ # Service accounts by purpose
├── compute/ # VM-based resources
│ ├── flacfetch_vm.py
│ ├── encoding_worker_vm.py
│ ├── github_runners.py
│ └── startup_scripts/ # External .sh files
└── docs/
└── PHASE5-PACKER-IMAGE-PLAN.md # Future optimization plan
```
### Extracted Code
- **Startup scripts**: Moved from embedded strings to separate `.sh`
files
- **GCE encoding service**: Extracted ~600-line Python app to
`backend/services/gce_encoding/`
### Documentation
- Updated `infrastructure/README.md` with new structure
- Added `docs/PHASE5-PACKER-IMAGE-PLAN.md` for future Packer image
optimization
## Verification
```bash
pulumi preview --diff
# Result: 120 resources unchanged
# Only 10 GitHub runner replacements (expected - startup script improvements from main)
```
## Test Plan
- [x] `pulumi preview` shows no unintended infrastructure changes
- [x] All module imports resolve correctly
- [x] Startup scripts produce identical output when read from files
## Notes
- Phase 5 (Packer custom images) documented but not implemented -
reduces encoding worker startup from ~10min to ~30sec
- `deploy.sh` for gdrive_validator kept - handles source upload to GCS
which Pulumi doesn't do automatically
@coderabbitai ignore
---
🤖 Generated with [Claude Code](https://claude.com/claude-code)
---------
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
2.5 KiB
2.5 KiB
GCE Encoding Worker
HTTP API service that runs on the GCE encoding worker VM for video encoding jobs.
Overview
This service provides HTTP endpoints for submitting and monitoring video encoding jobs. It runs as a systemd service on the encoding-worker GCE VM instance.
Endpoints
| Endpoint | Method | Description |
|---|---|---|
/encode |
POST | Submit a full encoding job |
/encode-preview |
POST | Submit a preview encoding job (480x270, fast) |
/status/{job_id} |
GET | Get job status and progress |
/health |
GET | Health check |
Authentication
All endpoints (except /health) require an API key via the X-API-Key header. The key is stored in Secret Manager (encoding-worker-api-key).
Encoding Process
Full Encoding (/encode)
- Downloads input files from GCS (title screen, karaoke video, end screen, instrumental audio)
- Uses
LocalEncodingServicefrom the karaoke-gen wheel to produce:- Lossless 4K MP4
- Lossy 4K MP4
- Lossless MKV
- 720p MP4
- Uploads results to GCS
- Reports progress via status endpoint
Preview Encoding (/encode-preview)
- Downloads ASS subtitle and audio files from GCS
- Runs FFmpeg to create a quick 480x270 preview video
- Uploads result to GCS
- Used for real-time preview in the web UI
Dependencies
- FFmpeg: Static build from John Van Sickle
- Python 3.13: Built from source (required for karaoke-gen)
- karaoke-gen wheel: Downloaded from GCS at job start (enables hot updates)
Deployment
This service is deployed via the VM's startup script. The script:
- Installs system dependencies (FFmpeg, fonts, Python build deps)
- Builds Python 3.13 from source
- Creates venv and installs dependencies
- Writes
main.pyto/opt/encoding-worker/ - Creates systemd service
- Starts the service on port 8080
Local Development
This code is extracted from the infrastructure for maintainability. To test locally:
cd backend/services/gce_encoding
pip install -r requirements.txt
pip install ../../../ # Install karaoke-gen wheel
ENCODING_API_KEY=test python main.py
Architecture Notes
- Hot code updates: The karaoke-gen wheel is re-downloaded at the start of each job, allowing code updates without VM restart
- In-memory job tracking: Job state is stored in memory. Restart clears queue.
- Parallel encoding: ThreadPoolExecutor with 4 workers
- GCS integration: Direct download/upload via google-cloud-storage client