mirror of
https://github.com/nomadkaraoke/karaoke-gen.git
synced 2026-12-04 00:56:43 +09:00
## Summary
The encoding-worker idle-shutdown Cloud Function only iterated
\`[primary_vm, secondary_vm]\` in the default zone — capacity-fallback
VMs in alternate zones (\`-a\`/\`-b\`) were never checked, so a fallback
started during a capacity event stayed RUNNING indefinitely after the
event resolved.
Observed 2026-05-07: \`encoding-worker-fallback-a\` in \`us-central1-a\`
ran for 13+ hours idle (~\$15 cost) before manual intervention.
## Changes
- **\`infrastructure/functions/encoding_worker_idle/main.py\`**:
- Read \`ENCODING_WORKER_FALLBACK_VMS\` env (same secret the backend
uses for capacity failover) and iterate fallbacks alongside
primary/secondary.
- Threaded \`zone\` through \`get_vm_status\` / \`get_vm_ip\` /
\`stop_vm\` so cross-zone shutdowns work.
- Keep-alive-on-recent-activity now follows \`active_override_vm\` (the
currently-routed VM during a capacity event), not always the primary.
Otherwise an active capacity-fallback would get killed while serving
real traffic.
- **\`infrastructure/modules/encoding_worker_manager.py\`** (Pulumi):
- Wire the \`encoding-worker-fallback-vms\` secret into the function via
\`secret_environment_variables\`.
- New \`gcp.secretmanager.SecretIamMember\` binding so the function's SA
can read that secret.
- **\`infrastructure/functions/encoding_worker_idle/deploy.sh\`** (new):
uploads source zip and triggers function rebuild. Was missing for this
function (other functions all have one); matches the pattern from
\`backup_to_aws/deploy.sh\` etc.
-
**\`infrastructure/functions/encoding_worker_idle/test_idle_shutdown.py\`**:
9 new tests:
- Idle fallback gets stopped (in its own zone)
- Active-override fallback kept alive on recent activity (primary stops
instead)
- Fallback with active jobs not stopped
- Graceful behavior when env var missing or malformed JSON
- 4 unit tests on the \`_parse_fallback_vms\` helper
- All 18 tests pass.
## Already applied to prod
I ran \`pulumi up\` and \`deploy.sh\` from this branch to apply the fix.
CI's pulumi-up will be a no-op on merge.
**End-to-end verification:**
1. Started \`encoding-worker-fallback-a\` manually (RUNNING)
2. Triggered \`encoding-worker-idle-check\` scheduler
3. fallback-a went to STOPPING within ~20s ✅
Before fix: fallback-a would have stayed RUNNING (old code never checked
fallbacks).
After fix: fallback gets stopped on the next 5-min idle sweep.
## Test plan
- [x] 18 unit tests pass locally (9 existing + 9 new)
- [x] \`pulumi preview\`: 1 created (secret IAM), 1 updated (function
with secret env var), 263 unchanged
- [x] \`pulumi up\` applied successfully
- [x] \`deploy.sh\` deployed new function code
- [x] Manually verified: fallback-a started → triggered idle-check →
stopped
@coderabbitai ignore
61 lines
2.1 KiB
Bash
Executable File
61 lines
2.1 KiB
Bash
Executable File
#!/bin/bash
|
|
# Deploy the encoding-worker-idle-shutdown Cloud Function.
|
|
#
|
|
# Pulumi manages the function resource (env vars, IAM, scheduler) but does not
|
|
# package or upload the function source. This script does that.
|
|
#
|
|
# Run this:
|
|
# - any time you change main.py / requirements.txt
|
|
# - after pulumi up if Pulumi reports no diff but the function is on stale code
|
|
#
|
|
# Prerequisites:
|
|
# - gcloud CLI authenticated as a principal with cloudfunctions.functions.update
|
|
# and storage.objects.create on encoding-worker-idle-source-${PROJECT_ID}
|
|
# - Pulumi infrastructure deployed first (creates the bucket, SA, and the
|
|
# ENCODING_WORKER_FALLBACK_VMS secret env var binding)
|
|
#
|
|
# Usage:
|
|
# ./deploy.sh
|
|
|
|
set -e
|
|
|
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
PROJECT_ID="${GOOGLE_CLOUD_PROJECT:-nomadkaraoke}"
|
|
REGION="us-central1"
|
|
FUNCTION_NAME="encoding-worker-idle-shutdown"
|
|
BUCKET_NAME="encoding-worker-idle-source-${PROJECT_ID}"
|
|
|
|
echo "Deploying encoding-worker-idle-shutdown Cloud Function..."
|
|
echo " Project: ${PROJECT_ID}"
|
|
echo " Region: ${REGION}"
|
|
echo " Function: ${FUNCTION_NAME}"
|
|
echo ""
|
|
|
|
cd "${SCRIPT_DIR}"
|
|
|
|
ZIP_PATH=/tmp/encoding-worker-idle-source.zip
|
|
echo "Creating source ZIP..."
|
|
rm -f "${ZIP_PATH}"
|
|
zip -r "${ZIP_PATH}" main.py requirements.txt
|
|
|
|
echo "Uploading to GCS..."
|
|
gcloud storage cp "${ZIP_PATH}" "gs://${BUCKET_NAME}/encoding-worker-idle-source.zip"
|
|
|
|
echo "Triggering function rebuild..."
|
|
gcloud functions deploy "${FUNCTION_NAME}" \
|
|
--gen2 \
|
|
--region="${REGION}" \
|
|
--source="gs://${BUCKET_NAME}/encoding-worker-idle-source.zip" \
|
|
--project="${PROJECT_ID}"
|
|
|
|
echo ""
|
|
echo "Function deployed."
|
|
echo ""
|
|
echo "Verify ENCODING_WORKER_FALLBACK_VMS env var is wired:"
|
|
echo " gcloud functions describe ${FUNCTION_NAME} --gen2 --region=${REGION} --project=${PROJECT_ID} \\"
|
|
echo " --format='value(serviceConfig.secretEnvironmentVariables)'"
|
|
echo ""
|
|
echo "Trigger a one-off run (skips the 5-min schedule):"
|
|
echo " curl -X POST -H \"Authorization: Bearer \$(gcloud auth print-identity-token)\" \\"
|
|
echo " https://${REGION}-${PROJECT_ID}.cloudfunctions.net/${FUNCTION_NAME}"
|