From 80b6c1ff36433d9c10d8f1c62998e291b11aa626 Mon Sep 17 00:00:00 2001 From: Pigbibi <20649888+Pigbibi@users.noreply.github.com> Date: Fri, 19 Jun 2026 01:33:32 +0800 Subject: [PATCH] Sync scheduler schedule from runtime target --- .github/workflows/sync-cloud-run-env.yml | 37 +++++++++++++++++------ tests/test_sync_cloud_run_env_workflow.sh | 13 +++++--- 2 files changed, 37 insertions(+), 13 deletions(-) diff --git a/.github/workflows/sync-cloud-run-env.yml b/.github/workflows/sync-cloud-run-env.yml index eb51220..7658278 100644 --- a/.github/workflows/sync-cloud-run-env.yml +++ b/.github/workflows/sync-cloud-run-env.yml @@ -988,6 +988,8 @@ jobs: - name: Sync Cloud Scheduler schedule if: steps.config.outputs.env_sync_enabled == 'true' + env: + RUNTIME_TARGET_JSON: ${{ steps.strategy_requirements.outputs.runtime_target_json }} run: | set -euo pipefail @@ -998,20 +1000,32 @@ jobs: fi mapfile -t scheduler_market_config < <(python - <<'PY' + import json import os + raw_runtime_target = os.environ.get("RUNTIME_TARGET_JSON", "").strip() + runtime_scheduler = {} + if raw_runtime_target: + try: + runtime_target = json.loads(raw_runtime_target) + except json.JSONDecodeError: + runtime_target = {} + scheduler = runtime_target.get("scheduler") if isinstance(runtime_target, dict) else {} + if isinstance(scheduler, dict): + runtime_scheduler = scheduler + market = os.environ.get("LONGBRIDGE_MARKET", "").strip().upper() - timezone = os.environ.get("LONGBRIDGE_MARKET_TIMEZONE", "").strip() + timezone = str(runtime_scheduler.get("timezone") or os.environ.get("LONGBRIDGE_MARKET_TIMEZONE", "")).strip() if not timezone: timezone = "Asia/Hong_Kong" if market == "HK" else "America/New_York" - def configured_time(name: str, default: str) -> str: - return os.environ.get(name, "").strip() or default + def configured_time(key: str, name: str, default: str) -> str: + return str(runtime_scheduler.get(key) or os.environ.get(name, "").strip() or default) print(timezone) - print(configured_time("CLOUD_SCHEDULER_MAIN_TIME", "45 15")) - print(configured_time("CLOUD_SCHEDULER_PROBE_TIME", "35 9,15")) - print(configured_time("CLOUD_SCHEDULER_PRECHECK_TIME", "45 9")) + print(configured_time("main_time", "CLOUD_SCHEDULER_MAIN_TIME", "45 15")) + print(configured_time("probe_time", "CLOUD_SCHEDULER_PROBE_TIME", "35 9,15")) + print(configured_time("precheck_time", "CLOUD_SCHEDULER_PRECHECK_TIME", "45 9")) PY ) market_timezone="${scheduler_market_config[0]}" @@ -1061,9 +1075,14 @@ jobs: time_fields = os.environ["SCHEDULE_TIME"].split() if len(current_fields) != 5: raise SystemExit(f"Cloud Scheduler schedule must have 5 fields: {os.environ['CURRENT_SCHEDULE']!r}") - if len(time_fields) != 2: - raise SystemExit(f"Cloud Scheduler time override must have 2 cron fields: {os.environ['SCHEDULE_TIME']!r}") - print(" ".join([*time_fields, *current_fields[2:]])) + if len(time_fields) == 5: + print(" ".join(time_fields)) + elif len(time_fields) == 2: + print(" ".join([*time_fields, *current_fields[2:]])) + else: + raise SystemExit( + f"Cloud Scheduler override must have 2 time fields or 5 cron fields: {os.environ['SCHEDULE_TIME']!r}" + ) PY )" diff --git a/tests/test_sync_cloud_run_env_workflow.sh b/tests/test_sync_cloud_run_env_workflow.sh index 9e050fe..da8b034 100644 --- a/tests/test_sync_cloud_run_env_workflow.sh +++ b/tests/test_sync_cloud_run_env_workflow.sh @@ -217,15 +217,20 @@ grep -Fq 'gcloud_args+=(--remove-secrets "$(IFS=,; echo "${remove_secret_vars[*] grep -Fq 'gcloud_args+=(--update-secrets "$(IFS=,; echo "${secret_pairs[*]}")")' "$workflow_file" grep -Fq -- '--update-env-vars "^|^$(join_by_delimiter "|" "${env_pairs[@]}")"' "$workflow_file" grep -Fq 'Sync Cloud Scheduler schedule' "$workflow_file" +grep -Fq 'RUNTIME_TARGET_JSON: ${{ steps.strategy_requirements.outputs.runtime_target_json }}' "$workflow_file" grep -Fq 'scheduler_location="${CLOUD_SCHEDULER_LOCATION:-${CLOUD_RUN_REGION}}"' "$workflow_file" -grep -Fq 'timezone = os.environ.get("LONGBRIDGE_MARKET_TIMEZONE", "").strip()' "$workflow_file" +grep -Fq 'raw_runtime_target = os.environ.get("RUNTIME_TARGET_JSON", "").strip()' "$workflow_file" +grep -Fq 'scheduler = runtime_target.get("scheduler") if isinstance(runtime_target, dict) else {}' "$workflow_file" +grep -Fq 'timezone = str(runtime_scheduler.get("timezone") or os.environ.get("LONGBRIDGE_MARKET_TIMEZONE", "")).strip()' "$workflow_file" grep -Fq 'timezone = "Asia/Hong_Kong" if market == "HK" else "America/New_York"' "$workflow_file" -grep -Fq 'configured_time("CLOUD_SCHEDULER_MAIN_TIME", "45 15")' "$workflow_file" -grep -Fq 'configured_time("CLOUD_SCHEDULER_PROBE_TIME", "35 9,15")' "$workflow_file" -grep -Fq 'configured_time("CLOUD_SCHEDULER_PRECHECK_TIME", "45 9")' "$workflow_file" +grep -Fq 'configured_time("main_time", "CLOUD_SCHEDULER_MAIN_TIME", "45 15")' "$workflow_file" +grep -Fq 'configured_time("probe_time", "CLOUD_SCHEDULER_PROBE_TIME", "35 9,15")' "$workflow_file" +grep -Fq 'configured_time("precheck_time", "CLOUD_SCHEDULER_PRECHECK_TIME", "45 9")' "$workflow_file" grep -Fq 'for suffix in scheduler probe-scheduler precheck-scheduler; do' "$workflow_file" grep -Fq 'current_schedule="$(gcloud scheduler jobs describe "${job_name}"' "$workflow_file" grep -Fq 'desired_schedule="$(CURRENT_SCHEDULE="${current_schedule}" SCHEDULE_TIME="${schedule_time}" python - <<' "$workflow_file" +grep -Fq 'if len(time_fields) == 5:' "$workflow_file" +grep -Fq 'print(" ".join(time_fields))' "$workflow_file" grep -Fq 'print(" ".join([*time_fields, *current_fields[2:]]))' "$workflow_file" grep -Fq 'gcloud scheduler jobs update http "${job_name}"' "$workflow_file" grep -Fq -- '--schedule="${desired_schedule}"' "$workflow_file"