mirror of
https://github.com/nomadkaraoke/karaoke-gen.git
synced 2026-12-02 01:48:08 +09:00
## Summary Investigated and fixed four production error patterns flagged by the error monitor, each with tests. ## Changes **Frontend — `TypeError: currentTime non-finite`** (`/en/app/jobs` review UI) - Guard lyrics-review/audio-editor seek sinks (`AudioPlayer.seekAndPlay`/`handleSeek`, `AudioEditor.seekTo`) and the `handlePlaySegment` dispatcher with `Number.isFinite`; harden `TimelineEditor.handleTimelineClick` against a zero-width container. Restores parity with instrumental-review, which already had these guards. **Backend — auto-correct Vertex `429 RESOURCE_EXHAUSTED` surfaced as generic 502** - Retry model calls with exponential backoff on transient (rate-limit/overload) errors via `_is_transient_model_error`; surface a distinct **retryable 429** instead of 502. Retry env knobs parsed defensively (clamp + warn, never crash at import). **Backend — `audio-download-job` `InvalidStateTransitionError: downloading -> downloading`** - Root cause: a manual `/retry` raced the Cloud Run auto-retry; the losing run's post-download transition raised and the generic `except` `fail_job`'d a job the winner had already advanced. Fix: idempotent transition — re-read status first, treat an already-advanced job as success, and use `raise_on_invalid=False` so a race never fails a live job. **Backend/infra — `audio-download-job` 600s task-timeout SIGKILL** - Internal `wait_for_download` timeout equalled the Cloud Run task timeout, so slow torrents were SIGKILLed before failing gracefully (leaving the orphaned retry that fed the race above). New `DOWNLOAD_WAIT_TIMEOUT_SECONDS=1080` kept strictly below the task timeout, raised `600s → 1200s`. **Infra already applied to prod via `pulumi up` (targeted); stack is clean.** ## Testing - [x] Backend: 122 tests across touched suites pass (auto_correct incl. 10 retry/env, audio_download_worker incl. 3 race/timeout, review API, proactive) - [x] Frontend: full jest suite 1073/1073 pass (incl. new AudioPlayer non-finite guard test); `tsc` clean for changed files - [x] Infra: `pulumi preview` clean (273 unchanged) after applying timeout + reconciling pre-existing divebar drift ## Review - [x] Local CodeRabbit review completed (1 major finding — retry env validation — fixed + tested) - [x] Version bumped 0.190.5 → 0.191.0 @coderabbitai ignore --- 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
668 lines
33 KiB
Python
668 lines
33 KiB
Python
"""
|
|
Cloud Run resources.
|
|
|
|
Manages Cloud Run domain mapping and Cloud Run Jobs.
|
|
"""
|
|
|
|
import pulumi
|
|
import pulumi_gcp as gcp
|
|
from pulumi_gcp import cloudrun, cloudrunv2
|
|
|
|
from config import PROJECT_ID, REGION
|
|
|
|
AUDIO_WORKER_GPU_REGION = "us-east4" # L4 GPU quota available here
|
|
|
|
|
|
def create_domain_mapping() -> cloudrun.DomainMapping:
|
|
"""
|
|
Create the Cloud Run domain mapping for api.nomadkaraoke.com.
|
|
|
|
Returns:
|
|
cloudrun.DomainMapping: The domain mapping resource.
|
|
"""
|
|
domain_mapping = cloudrun.DomainMapping(
|
|
"karaoke-backend-domain",
|
|
location=REGION,
|
|
name="api.nomadkaraoke.com",
|
|
metadata=cloudrun.DomainMappingMetadataArgs(
|
|
namespace=PROJECT_ID,
|
|
),
|
|
spec=cloudrun.DomainMappingSpecArgs(
|
|
route_name="karaoke-backend",
|
|
),
|
|
)
|
|
|
|
return domain_mapping
|
|
|
|
|
|
def create_lyrics_transcription_job(
|
|
bucket: gcp.storage.Bucket,
|
|
service_account: gcp.serviceaccount.Account,
|
|
) -> cloudrunv2.Job:
|
|
"""
|
|
Create the Cloud Run Job for lyrics transcription.
|
|
|
|
Cloud Run Jobs run to completion without HTTP request lifecycle concerns,
|
|
avoiding the instance termination issue where Cloud Run would shut down
|
|
instances mid-processing when using BackgroundTasks (e.g., job c94cc9d6).
|
|
|
|
Typical duration: 5-15 minutes (AudioShake + agentic AI correction)
|
|
|
|
Args:
|
|
bucket: The GCS bucket for job artifacts.
|
|
service_account: The service account to run the job.
|
|
|
|
Returns:
|
|
cloudrunv2.Job: The Cloud Run Job resource.
|
|
"""
|
|
lyrics_transcription_job = cloudrunv2.Job(
|
|
"lyrics-transcription-job",
|
|
name="lyrics-transcription-job",
|
|
location=REGION,
|
|
template=cloudrunv2.JobTemplateArgs(
|
|
template=cloudrunv2.JobTemplateTemplateArgs(
|
|
containers=[
|
|
cloudrunv2.JobTemplateTemplateContainerArgs(
|
|
image=f"{REGION}-docker.pkg.dev/{PROJECT_ID}/karaoke-repo/karaoke-backend:latest",
|
|
args=["python", "-m", "backend.workers.lyrics_worker"],
|
|
resources=cloudrunv2.JobTemplateTemplateContainerResourcesArgs(
|
|
limits={
|
|
"cpu": "2",
|
|
"memory": "4Gi",
|
|
},
|
|
),
|
|
envs=[
|
|
# Admin token for Cloud Tasks auth header
|
|
cloudrunv2.JobTemplateTemplateContainerEnvArgs(
|
|
name="ADMIN_TOKENS",
|
|
value_source=cloudrunv2.JobTemplateTemplateContainerEnvValueSourceArgs(
|
|
secret_key_ref=cloudrunv2.JobTemplateTemplateContainerEnvValueSourceSecretKeyRefArgs(
|
|
secret=f"projects/{PROJECT_ID}/secrets/admin-tokens",
|
|
version="latest",
|
|
),
|
|
),
|
|
),
|
|
# AudioShake API token (required for transcription)
|
|
cloudrunv2.JobTemplateTemplateContainerEnvArgs(
|
|
name="AUDIOSHAKE_API_TOKEN",
|
|
value_source=cloudrunv2.JobTemplateTemplateContainerEnvValueSourceArgs(
|
|
secret_key_ref=cloudrunv2.JobTemplateTemplateContainerEnvValueSourceSecretKeyRefArgs(
|
|
secret=f"projects/{PROJECT_ID}/secrets/audioshake-api-key",
|
|
version="latest",
|
|
),
|
|
),
|
|
),
|
|
# Cloud Run service URL for Cloud Tasks targeting
|
|
cloudrunv2.JobTemplateTemplateContainerEnvArgs(
|
|
name="CLOUD_RUN_SERVICE_URL",
|
|
value="https://api.nomadkaraoke.com",
|
|
),
|
|
# Enable Cloud Tasks mode (vs direct HTTP)
|
|
cloudrunv2.JobTemplateTemplateContainerEnvArgs(
|
|
name="ENABLE_CLOUD_TASKS",
|
|
value="true",
|
|
),
|
|
# Basic configuration
|
|
cloudrunv2.JobTemplateTemplateContainerEnvArgs(
|
|
name="ENVIRONMENT",
|
|
value="production",
|
|
),
|
|
# Cap the duration-scaled transcription timeout below the
|
|
# task `timeout` above so the worker raises a clean
|
|
# RuntimeError before Cloud Run hard-kills the task.
|
|
cloudrunv2.JobTemplateTemplateContainerEnvArgs(
|
|
name="TRANSCRIPTION_TIMEOUT_CAP_SECONDS",
|
|
value="2700",
|
|
),
|
|
cloudrunv2.JobTemplateTemplateContainerEnvArgs(
|
|
name="GCP_REGION",
|
|
value=REGION,
|
|
),
|
|
cloudrunv2.JobTemplateTemplateContainerEnvArgs(
|
|
name="GCS_BUCKET_NAME",
|
|
value=bucket.name,
|
|
),
|
|
# Genius API token (for lyrics fetching)
|
|
cloudrunv2.JobTemplateTemplateContainerEnvArgs(
|
|
name="GENIUS_API_TOKEN",
|
|
value_source=cloudrunv2.JobTemplateTemplateContainerEnvValueSourceArgs(
|
|
secret_key_ref=cloudrunv2.JobTemplateTemplateContainerEnvValueSourceSecretKeyRefArgs(
|
|
secret=f"projects/{PROJECT_ID}/secrets/genius-api-key",
|
|
version="latest",
|
|
),
|
|
),
|
|
),
|
|
cloudrunv2.JobTemplateTemplateContainerEnvArgs(
|
|
name="GOOGLE_CLOUD_PROJECT",
|
|
value=PROJECT_ID,
|
|
),
|
|
# RapidAPI key (for Musixmatch lyrics)
|
|
cloudrunv2.JobTemplateTemplateContainerEnvArgs(
|
|
name="RAPIDAPI_KEY",
|
|
value_source=cloudrunv2.JobTemplateTemplateContainerEnvValueSourceArgs(
|
|
secret_key_ref=cloudrunv2.JobTemplateTemplateContainerEnvValueSourceSecretKeyRefArgs(
|
|
secret=f"projects/{PROJECT_ID}/secrets/rapidapi-key",
|
|
version="latest",
|
|
),
|
|
),
|
|
),
|
|
# Spotify cookie (for Spotify lyrics)
|
|
cloudrunv2.JobTemplateTemplateContainerEnvArgs(
|
|
name="SPOTIFY_COOKIE_SP_DC",
|
|
value_source=cloudrunv2.JobTemplateTemplateContainerEnvValueSourceArgs(
|
|
secret_key_ref=cloudrunv2.JobTemplateTemplateContainerEnvValueSourceSecretKeyRefArgs(
|
|
secret=f"projects/{PROJECT_ID}/secrets/spotify-cookie",
|
|
version="latest",
|
|
),
|
|
),
|
|
),
|
|
],
|
|
)
|
|
],
|
|
service_account=service_account.email,
|
|
timeout="3000s", # 50 minutes max per task (long mashups can take 30+ min to transcribe)
|
|
max_retries=2,
|
|
),
|
|
),
|
|
)
|
|
|
|
return lyrics_transcription_job
|
|
|
|
|
|
def create_audio_separation_job(
|
|
bucket: gcp.storage.Bucket,
|
|
service_account: gcp.serviceaccount.Account,
|
|
) -> cloudrunv2.Job:
|
|
"""
|
|
Create the Cloud Run Job for audio separation with L4 GPU.
|
|
|
|
Runs the Separator class directly with GPU acceleration, eliminating
|
|
the HTTP call to the separate audio-separator service.
|
|
|
|
Deployed in us-east4 where L4 GPU quota is available.
|
|
|
|
Typical duration: 5-10 minutes (local GPU separation)
|
|
|
|
Args:
|
|
bucket: The GCS bucket for job artifacts.
|
|
service_account: The service account to run the job.
|
|
|
|
Returns:
|
|
cloudrunv2.Job: The Cloud Run Job resource.
|
|
"""
|
|
audio_separation_job = cloudrunv2.Job(
|
|
"audio-separation-job",
|
|
name="audio-separation-job",
|
|
location=AUDIO_WORKER_GPU_REGION,
|
|
deletion_protection=False, # Allow Pulumi to replace (region change requires delete+create)
|
|
template=cloudrunv2.JobTemplateArgs(
|
|
template=cloudrunv2.JobTemplateTemplateArgs(
|
|
containers=[
|
|
cloudrunv2.JobTemplateTemplateContainerArgs(
|
|
image=f"{AUDIO_WORKER_GPU_REGION}-docker.pkg.dev/{PROJECT_ID}/karaoke-backend-gpu/karaoke-backend:latest",
|
|
args=["python", "-m", "backend.workers.audio_worker"],
|
|
resources=cloudrunv2.JobTemplateTemplateContainerResourcesArgs(
|
|
limits={
|
|
"cpu": "4",
|
|
"memory": "16Gi",
|
|
"nvidia.com/gpu": "1",
|
|
},
|
|
),
|
|
envs=[
|
|
# Admin token for Cloud Tasks auth header
|
|
cloudrunv2.JobTemplateTemplateContainerEnvArgs(
|
|
name="ADMIN_TOKENS",
|
|
value_source=cloudrunv2.JobTemplateTemplateContainerEnvValueSourceArgs(
|
|
secret_key_ref=cloudrunv2.JobTemplateTemplateContainerEnvValueSourceSecretKeyRefArgs(
|
|
secret=f"projects/{PROJECT_ID}/secrets/admin-tokens",
|
|
version="latest",
|
|
),
|
|
),
|
|
),
|
|
# Model directory (baked into GPU image, triggers local GPU mode)
|
|
cloudrunv2.JobTemplateTemplateContainerEnvArgs(
|
|
name="MODEL_DIR",
|
|
value="/models",
|
|
),
|
|
# Cloud Run service URL for Cloud Tasks targeting
|
|
cloudrunv2.JobTemplateTemplateContainerEnvArgs(
|
|
name="CLOUD_RUN_SERVICE_URL",
|
|
value="https://api.nomadkaraoke.com",
|
|
),
|
|
# Enable Cloud Tasks mode (vs direct HTTP)
|
|
cloudrunv2.JobTemplateTemplateContainerEnvArgs(
|
|
name="ENABLE_CLOUD_TASKS",
|
|
value="true",
|
|
),
|
|
# Basic configuration
|
|
cloudrunv2.JobTemplateTemplateContainerEnvArgs(
|
|
name="ENVIRONMENT",
|
|
value="production",
|
|
),
|
|
cloudrunv2.JobTemplateTemplateContainerEnvArgs(
|
|
name="GCP_REGION",
|
|
value=AUDIO_WORKER_GPU_REGION,
|
|
),
|
|
cloudrunv2.JobTemplateTemplateContainerEnvArgs(
|
|
name="GCS_BUCKET_NAME",
|
|
value=bucket.name,
|
|
),
|
|
cloudrunv2.JobTemplateTemplateContainerEnvArgs(
|
|
name="GOOGLE_CLOUD_PROJECT",
|
|
value=PROJECT_ID,
|
|
),
|
|
],
|
|
)
|
|
],
|
|
node_selector=cloudrunv2.JobTemplateTemplateNodeSelectorArgs(
|
|
accelerator="nvidia-l4",
|
|
),
|
|
gpu_zonal_redundancy_disabled=True, # Required for GPU Jobs (capacity limitation)
|
|
service_account=service_account.email,
|
|
timeout="1800s", # 30 minutes (local GPU is faster than remote API)
|
|
max_retries=2,
|
|
),
|
|
),
|
|
)
|
|
|
|
return audio_separation_job
|
|
|
|
|
|
def create_audio_download_job(
|
|
bucket: gcp.storage.Bucket,
|
|
service_account: gcp.serviceaccount.Account,
|
|
vpc_connector: object = None,
|
|
) -> cloudrunv2.Job:
|
|
"""
|
|
Create the Cloud Run Job for audio downloading.
|
|
|
|
Downloads audio from Spotify/YouTube/RED/OPS via the flacfetch VM,
|
|
then triggers audio separation and lyrics workers.
|
|
|
|
Requires VPC access to reach the flacfetch VM on its internal IP.
|
|
|
|
Typical duration: 30s-5 minutes (depending on source)
|
|
|
|
Args:
|
|
bucket: The GCS bucket for job artifacts.
|
|
service_account: The service account to run the job.
|
|
vpc_connector: VPC connector for accessing flacfetch VM (optional).
|
|
|
|
Returns:
|
|
cloudrunv2.Job: The Cloud Run Job resource.
|
|
"""
|
|
# Build VPC access config if connector provided
|
|
vpc_access = None
|
|
if vpc_connector:
|
|
vpc_access = cloudrunv2.JobTemplateTemplateVpcAccessArgs(
|
|
connector=vpc_connector.id,
|
|
egress="PRIVATE_RANGES_ONLY",
|
|
)
|
|
|
|
audio_download_job = cloudrunv2.Job(
|
|
"audio-download-job",
|
|
name="audio-download-job",
|
|
location=REGION,
|
|
template=cloudrunv2.JobTemplateArgs(
|
|
template=cloudrunv2.JobTemplateTemplateArgs(
|
|
containers=[
|
|
cloudrunv2.JobTemplateTemplateContainerArgs(
|
|
image=f"{REGION}-docker.pkg.dev/{PROJECT_ID}/karaoke-repo/karaoke-backend:latest",
|
|
args=["python", "-m", "backend.workers.audio_download_worker"],
|
|
resources=cloudrunv2.JobTemplateTemplateContainerResourcesArgs(
|
|
limits={
|
|
"cpu": "1",
|
|
"memory": "1Gi",
|
|
},
|
|
),
|
|
envs=[
|
|
# Admin token for worker auth
|
|
cloudrunv2.JobTemplateTemplateContainerEnvArgs(
|
|
name="ADMIN_TOKENS",
|
|
value_source=cloudrunv2.JobTemplateTemplateContainerEnvValueSourceArgs(
|
|
secret_key_ref=cloudrunv2.JobTemplateTemplateContainerEnvValueSourceSecretKeyRefArgs(
|
|
secret=f"projects/{PROJECT_ID}/secrets/admin-tokens",
|
|
version="latest",
|
|
),
|
|
),
|
|
),
|
|
# Cloud Run service URL for triggering workers
|
|
cloudrunv2.JobTemplateTemplateContainerEnvArgs(
|
|
name="CLOUD_RUN_SERVICE_URL",
|
|
value="https://api.nomadkaraoke.com",
|
|
),
|
|
# Enable Cloud Tasks mode for worker triggers
|
|
cloudrunv2.JobTemplateTemplateContainerEnvArgs(
|
|
name="ENABLE_CLOUD_TASKS",
|
|
value="true",
|
|
),
|
|
# Basic configuration
|
|
cloudrunv2.JobTemplateTemplateContainerEnvArgs(
|
|
name="ENVIRONMENT",
|
|
value="production",
|
|
),
|
|
# Flacfetch API key
|
|
cloudrunv2.JobTemplateTemplateContainerEnvArgs(
|
|
name="FLACFETCH_API_KEY",
|
|
value_source=cloudrunv2.JobTemplateTemplateContainerEnvValueSourceArgs(
|
|
secret_key_ref=cloudrunv2.JobTemplateTemplateContainerEnvValueSourceSecretKeyRefArgs(
|
|
secret=f"projects/{PROJECT_ID}/secrets/flacfetch-api-key",
|
|
version="latest",
|
|
),
|
|
),
|
|
),
|
|
# Flacfetch API URL (internal VPC address)
|
|
cloudrunv2.JobTemplateTemplateContainerEnvArgs(
|
|
name="FLACFETCH_API_URL",
|
|
value_source=cloudrunv2.JobTemplateTemplateContainerEnvValueSourceArgs(
|
|
secret_key_ref=cloudrunv2.JobTemplateTemplateContainerEnvValueSourceSecretKeyRefArgs(
|
|
secret=f"projects/{PROJECT_ID}/secrets/flacfetch-api-url",
|
|
version="latest",
|
|
),
|
|
),
|
|
),
|
|
cloudrunv2.JobTemplateTemplateContainerEnvArgs(
|
|
name="GCP_REGION",
|
|
value=REGION,
|
|
),
|
|
cloudrunv2.JobTemplateTemplateContainerEnvArgs(
|
|
name="GCS_BUCKET_NAME",
|
|
value=bucket.name,
|
|
),
|
|
cloudrunv2.JobTemplateTemplateContainerEnvArgs(
|
|
name="GOOGLE_CLOUD_PROJECT",
|
|
value=PROJECT_ID,
|
|
),
|
|
],
|
|
)
|
|
],
|
|
service_account=service_account.email,
|
|
# 20 min max. Slow torrent (RED/OPS) downloads can exceed 10 min;
|
|
# a SIGKILL at the old 600s bypassed graceful failure handling and
|
|
# left in-flight auto-retries racing manual retries (job 1cd29294).
|
|
# The worker's DOWNLOAD_WAIT_TIMEOUT_SECONDS (1080s) is kept
|
|
# strictly below this so it fails cleanly before Cloud Run kills it.
|
|
timeout="1200s",
|
|
max_retries=2,
|
|
vpc_access=vpc_access,
|
|
),
|
|
),
|
|
)
|
|
|
|
return audio_download_job
|
|
|
|
|
|
def create_bulk_search_job(
|
|
bucket: gcp.storage.Bucket,
|
|
service_account: gcp.serviceaccount.Account,
|
|
vpc_connector: object = None,
|
|
) -> cloudrunv2.Job:
|
|
"""
|
|
Create the Cloud Run Job for Bulk Mode batch audio search.
|
|
|
|
Processes every job in a batch (state_data.batch_id): runs each audio search
|
|
via the flacfetch VM, auto-selects confident lossless matches (triggering the
|
|
audio-download-job), and parks the rest in AWAITING_AUDIO_SELECTION.
|
|
|
|
Requires VPC access to reach the flacfetch VM. Longer timeout than the download
|
|
job because a batch can contain up to 100 songs. Idempotent across retries
|
|
(only re-processes jobs still flagged bulk_pending_search), so retries=1.
|
|
"""
|
|
vpc_access = None
|
|
if vpc_connector:
|
|
vpc_access = cloudrunv2.JobTemplateTemplateVpcAccessArgs(
|
|
connector=vpc_connector.id,
|
|
egress="PRIVATE_RANGES_ONLY",
|
|
)
|
|
|
|
bulk_search_job = cloudrunv2.Job(
|
|
"bulk-search-job",
|
|
name="bulk-search-job",
|
|
location=REGION,
|
|
template=cloudrunv2.JobTemplateArgs(
|
|
template=cloudrunv2.JobTemplateTemplateArgs(
|
|
containers=[
|
|
cloudrunv2.JobTemplateTemplateContainerArgs(
|
|
image=f"{REGION}-docker.pkg.dev/{PROJECT_ID}/karaoke-repo/karaoke-backend:latest",
|
|
args=["python", "-m", "backend.workers.bulk_search_worker"],
|
|
resources=cloudrunv2.JobTemplateTemplateContainerResourcesArgs(
|
|
limits={
|
|
"cpu": "1",
|
|
"memory": "1Gi",
|
|
},
|
|
),
|
|
envs=[
|
|
cloudrunv2.JobTemplateTemplateContainerEnvArgs(
|
|
name="ADMIN_TOKENS",
|
|
value_source=cloudrunv2.JobTemplateTemplateContainerEnvValueSourceArgs(
|
|
secret_key_ref=cloudrunv2.JobTemplateTemplateContainerEnvValueSourceSecretKeyRefArgs(
|
|
secret=f"projects/{PROJECT_ID}/secrets/admin-tokens",
|
|
version="latest",
|
|
),
|
|
),
|
|
),
|
|
cloudrunv2.JobTemplateTemplateContainerEnvArgs(
|
|
name="CLOUD_RUN_SERVICE_URL",
|
|
value="https://api.nomadkaraoke.com",
|
|
),
|
|
cloudrunv2.JobTemplateTemplateContainerEnvArgs(
|
|
name="ENABLE_CLOUD_TASKS",
|
|
value="true",
|
|
),
|
|
cloudrunv2.JobTemplateTemplateContainerEnvArgs(
|
|
name="ENVIRONMENT",
|
|
value="production",
|
|
),
|
|
cloudrunv2.JobTemplateTemplateContainerEnvArgs(
|
|
name="FLACFETCH_API_KEY",
|
|
value_source=cloudrunv2.JobTemplateTemplateContainerEnvValueSourceArgs(
|
|
secret_key_ref=cloudrunv2.JobTemplateTemplateContainerEnvValueSourceSecretKeyRefArgs(
|
|
secret=f"projects/{PROJECT_ID}/secrets/flacfetch-api-key",
|
|
version="latest",
|
|
),
|
|
),
|
|
),
|
|
cloudrunv2.JobTemplateTemplateContainerEnvArgs(
|
|
name="FLACFETCH_API_URL",
|
|
value_source=cloudrunv2.JobTemplateTemplateContainerEnvValueSourceArgs(
|
|
secret_key_ref=cloudrunv2.JobTemplateTemplateContainerEnvValueSourceSecretKeyRefArgs(
|
|
secret=f"projects/{PROJECT_ID}/secrets/flacfetch-api-url",
|
|
version="latest",
|
|
),
|
|
),
|
|
),
|
|
cloudrunv2.JobTemplateTemplateContainerEnvArgs(
|
|
name="GCP_REGION",
|
|
value=REGION,
|
|
),
|
|
cloudrunv2.JobTemplateTemplateContainerEnvArgs(
|
|
name="GCS_BUCKET_NAME",
|
|
value=bucket.name,
|
|
),
|
|
cloudrunv2.JobTemplateTemplateContainerEnvArgs(
|
|
name="GOOGLE_CLOUD_PROJECT",
|
|
value=PROJECT_ID,
|
|
),
|
|
],
|
|
)
|
|
],
|
|
service_account=service_account.email,
|
|
timeout="3600s", # up to 100 songs per batch
|
|
max_retries=1,
|
|
vpc_access=vpc_access,
|
|
),
|
|
),
|
|
)
|
|
|
|
return bulk_search_job
|
|
|
|
|
|
def create_video_encoding_job(
|
|
bucket: gcp.storage.Bucket,
|
|
service_account: gcp.serviceaccount.Account,
|
|
) -> cloudrunv2.Job:
|
|
"""
|
|
Create the Cloud Run Job for video encoding.
|
|
|
|
Cloud Run Jobs run to completion (up to 1 hour), immune to Cloud Run Service
|
|
deployment rollouts. This prevents the BackgroundTask termination issue where
|
|
a deployment kills the encoding orchestrator mid-flight (see incident 2026-03-08).
|
|
|
|
The video worker orchestrates: CDG/TXT packaging, GCE encoding submission/polling,
|
|
file download from GCS, YouTube/Dropbox/GDrive upload, and Discord notification.
|
|
It needs the same env vars and secrets as the Cloud Run Service for these features.
|
|
|
|
Args:
|
|
bucket: The GCS bucket for job artifacts.
|
|
service_account: The service account to run the job.
|
|
|
|
Returns:
|
|
cloudrunv2.Job: The Cloud Run Job resource.
|
|
"""
|
|
video_encoding_job = cloudrunv2.Job(
|
|
"video-encoding-job",
|
|
name="video-encoding-job",
|
|
location=REGION,
|
|
template=cloudrunv2.JobTemplateArgs(
|
|
template=cloudrunv2.JobTemplateTemplateArgs(
|
|
containers=[
|
|
cloudrunv2.JobTemplateTemplateContainerArgs(
|
|
image=f"{REGION}-docker.pkg.dev/{PROJECT_ID}/karaoke-repo/karaoke-backend:latest",
|
|
args=["python", "-m", "backend.workers.video_worker"],
|
|
resources=cloudrunv2.JobTemplateTemplateContainerResourcesArgs(
|
|
limits={
|
|
"cpu": "4",
|
|
"memory": "8Gi",
|
|
},
|
|
),
|
|
envs=[
|
|
# Admin token for worker-to-service auth
|
|
cloudrunv2.JobTemplateTemplateContainerEnvArgs(
|
|
name="ADMIN_TOKENS",
|
|
value_source=cloudrunv2.JobTemplateTemplateContainerEnvValueSourceArgs(
|
|
secret_key_ref=cloudrunv2.JobTemplateTemplateContainerEnvValueSourceSecretKeyRefArgs(
|
|
secret=f"projects/{PROJECT_ID}/secrets/admin-tokens",
|
|
version="latest",
|
|
),
|
|
),
|
|
),
|
|
# Cloud Run service URL for triggering downstream workers
|
|
cloudrunv2.JobTemplateTemplateContainerEnvArgs(
|
|
name="CLOUD_RUN_SERVICE_URL",
|
|
value="https://api.nomadkaraoke.com",
|
|
),
|
|
# Distribution defaults
|
|
cloudrunv2.JobTemplateTemplateContainerEnvArgs(
|
|
name="DEFAULT_BRAND_PREFIX",
|
|
value="NOMAD",
|
|
),
|
|
# Discord webhook for release notifications
|
|
cloudrunv2.JobTemplateTemplateContainerEnvArgs(
|
|
name="DEFAULT_DISCORD_WEBHOOK_URL",
|
|
value_source=cloudrunv2.JobTemplateTemplateContainerEnvValueSourceArgs(
|
|
secret_key_ref=cloudrunv2.JobTemplateTemplateContainerEnvValueSourceSecretKeyRefArgs(
|
|
secret=f"projects/{PROJECT_ID}/secrets/discord-releases-webhook",
|
|
version="latest",
|
|
),
|
|
),
|
|
),
|
|
cloudrunv2.JobTemplateTemplateContainerEnvArgs(
|
|
name="DEFAULT_DROPBOX_PATH",
|
|
value="/MediaUnsynced/Karaoke/Tracks-Organized",
|
|
),
|
|
cloudrunv2.JobTemplateTemplateContainerEnvArgs(
|
|
name="DEFAULT_ENABLE_YOUTUBE_UPLOAD",
|
|
value="true",
|
|
),
|
|
cloudrunv2.JobTemplateTemplateContainerEnvArgs(
|
|
name="DEFAULT_GDRIVE_FOLDER_ID",
|
|
value="1laRKAyxo0v817SstfM5XkpbWiNKNAMSX",
|
|
),
|
|
cloudrunv2.JobTemplateTemplateContainerEnvArgs(
|
|
name="DEFAULT_PRIVATE_BRAND_PREFIX",
|
|
value="NOMADNP",
|
|
),
|
|
cloudrunv2.JobTemplateTemplateContainerEnvArgs(
|
|
name="DEFAULT_PRIVATE_DROPBOX_PATH",
|
|
value="/MediaUnsynced/Karaoke/Tracks-NonPublished",
|
|
),
|
|
cloudrunv2.JobTemplateTemplateContainerEnvArgs(
|
|
name="ENABLE_CLOUD_TASKS",
|
|
value="true",
|
|
),
|
|
# GCE encoding worker
|
|
cloudrunv2.JobTemplateTemplateContainerEnvArgs(
|
|
name="ENCODING_WORKER_API_KEY",
|
|
value_source=cloudrunv2.JobTemplateTemplateContainerEnvValueSourceArgs(
|
|
secret_key_ref=cloudrunv2.JobTemplateTemplateContainerEnvValueSourceSecretKeyRefArgs(
|
|
secret=f"projects/{PROJECT_ID}/secrets/encoding-worker-api-key",
|
|
version="latest",
|
|
),
|
|
),
|
|
),
|
|
cloudrunv2.JobTemplateTemplateContainerEnvArgs(
|
|
name="ENCODING_WORKER_URL",
|
|
value_source=cloudrunv2.JobTemplateTemplateContainerEnvValueSourceArgs(
|
|
secret_key_ref=cloudrunv2.JobTemplateTemplateContainerEnvValueSourceSecretKeyRefArgs(
|
|
secret=f"projects/{PROJECT_ID}/secrets/encoding-worker-url",
|
|
version="latest",
|
|
),
|
|
),
|
|
),
|
|
# Multi-zone capacity-fallback VMs (JSON list).
|
|
# Without this, the encoding worker manager only
|
|
# tries the primary zone — when -c is exhausted
|
|
# the job fails. Cloud Run Service has the same
|
|
# secret mounted; the Cloud Run Job needed it too.
|
|
cloudrunv2.JobTemplateTemplateContainerEnvArgs(
|
|
name="ENCODING_WORKER_FALLBACK_VMS",
|
|
value_source=cloudrunv2.JobTemplateTemplateContainerEnvValueSourceArgs(
|
|
secret_key_ref=cloudrunv2.JobTemplateTemplateContainerEnvValueSourceSecretKeyRefArgs(
|
|
secret=f"projects/{PROJECT_ID}/secrets/encoding-worker-fallback-vms",
|
|
version="latest",
|
|
),
|
|
),
|
|
),
|
|
# Core configuration
|
|
cloudrunv2.JobTemplateTemplateContainerEnvArgs(
|
|
name="ENVIRONMENT",
|
|
value="production",
|
|
),
|
|
cloudrunv2.JobTemplateTemplateContainerEnvArgs(
|
|
name="GCP_REGION",
|
|
value=REGION,
|
|
),
|
|
cloudrunv2.JobTemplateTemplateContainerEnvArgs(
|
|
name="GCS_BUCKET_NAME",
|
|
value=bucket.name,
|
|
),
|
|
cloudrunv2.JobTemplateTemplateContainerEnvArgs(
|
|
name="GOOGLE_CLOUD_PROJECT",
|
|
value=PROJECT_ID,
|
|
),
|
|
# Email provider for completion emails (Postmark)
|
|
cloudrunv2.JobTemplateTemplateContainerEnvArgs(
|
|
name="POSTMARK_SERVER_TOKEN",
|
|
value_source=cloudrunv2.JobTemplateTemplateContainerEnvValueSourceArgs(
|
|
secret_key_ref=cloudrunv2.JobTemplateTemplateContainerEnvValueSourceSecretKeyRefArgs(
|
|
secret=f"projects/{PROJECT_ID}/secrets/postmark-server-token",
|
|
version="latest",
|
|
),
|
|
),
|
|
),
|
|
cloudrunv2.JobTemplateTemplateContainerEnvArgs(
|
|
name="USE_GCE_ENCODING",
|
|
value="true",
|
|
),
|
|
],
|
|
)
|
|
],
|
|
service_account=service_account.email,
|
|
timeout="3600s", # 1 hour max per task
|
|
max_retries=2,
|
|
),
|
|
),
|
|
)
|
|
|
|
return video_encoding_job
|