From 714a6b550461b6e922cef09d633d81e548b7b8a0 Mon Sep 17 00:00:00 2001 From: Nikhil Woodruff <35577657+nikhilwoodruff@users.noreply.github.com> Date: Fri, 6 Jun 2025 16:41:51 +0100 Subject: [PATCH 1/2] =?UTF-8?q?Revert=20"Create=20GitHub=20Action=20script?= =?UTF-8?q?=20to=20prevent=20API=20v1=20deployment=20if=20v1=20and=20v2?= =?UTF-8?q?=E2=80=A6"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit 7c3d21b07ffac2a4c1161aeac06170acca46648d. --- .github/find-api-model-versions.py | 31 ------------------- .github/install-jq.sh | 6 ---- ...-versions.sh => request-model-versions.sh} | 23 +++++++------- .github/workflows/push.yml | 8 ----- 4 files changed, 12 insertions(+), 56 deletions(-) delete mode 100644 .github/find-api-model-versions.py delete mode 100644 .github/install-jq.sh rename .github/{request-simulation-model-versions.sh => request-model-versions.sh} (87%) diff --git a/.github/find-api-model-versions.py b/.github/find-api-model-versions.py deleted file mode 100644 index 0c0f53fcc..000000000 --- a/.github/find-api-model-versions.py +++ /dev/null @@ -1,31 +0,0 @@ -import os -import sys -from policyengine_api.constants import COUNTRY_PACKAGE_VERSIONS - - -def find_api_model_versions_and_output_to_github(): - """ - Find the API model versions and output them to a file for GitHub. - """ - # Try to get package versions for US and UK - us_version = COUNTRY_PACKAGE_VERSIONS.get("us") - uk_version = COUNTRY_PACKAGE_VERSIONS.get("uk") - - if not us_version: - print("Error: US package version not found.", file=sys.stderr) - sys.exit(1) - - if not uk_version: - print("Error: UK package version not found.", file=sys.stderr) - sys.exit(1) - - # Write to GitHub Actions environment - with open(os.environ["GITHUB_ENV"], "a") as f: - f.write(f"US_VERSION={us_version}\n") - f.write(f"UK_VERSION={uk_version}\n") - - -if __name__ == "__main__": - find_api_model_versions_and_output_to_github() - print("API model versions found and written to GitHub environment.") - sys.exit(0) diff --git a/.github/install-jq.sh b/.github/install-jq.sh deleted file mode 100644 index 45562a138..000000000 --- a/.github/install-jq.sh +++ /dev/null @@ -1,6 +0,0 @@ -#! /usr/bin/env bash - -set -e - -apt-get update -apt-get install -y jq \ No newline at end of file diff --git a/.github/request-simulation-model-versions.sh b/.github/request-model-versions.sh similarity index 87% rename from .github/request-simulation-model-versions.sh rename to .github/request-model-versions.sh index 8e563dc65..cfd8c164c 100755 --- a/.github/request-simulation-model-versions.sh +++ b/.github/request-model-versions.sh @@ -3,10 +3,10 @@ set -e # Google Cloud Workflow execution script -# Usage: ./request-simulation-model-versions.sh -b -us -uk [-t timeout] [-i interval] +# Usage: ./wait_for_country_versions.sh -b -us -uk [-t timeout] [-i interval] usage() { - echo "Usage: $0 -us -uk [-t timeout] [-i interval]" + echo "Usage: $0 -b -us -uk [-t timeout] [-i interval]" echo "" echo "Required flags:" echo " -b bucket_name - GCS bucket name" @@ -19,8 +19,8 @@ usage() { echo " -h help - Show this help message" echo "" echo "Example:" - echo " $0 -us v1.2.3 -uk v1.2.4" - echo " $0 -us v1.2.3 -uk v1.2.4 -t 600 -i 15" + echo " $0 -b my-bucket -us v1.2.3 -uk v1.2.4" + echo " $0 -b my-bucket -us v1.2.3 -uk v1.2.4 -t 600 -i 15" exit 1 } @@ -34,9 +34,6 @@ CHECK_INTERVAL="10" # Parse command line arguments while [ $# -gt 0 ]; do case "$1" in - -h|--help) - usage - ;; -b) if [ -z "$2" ]; then echo "Error: -b requires a bucket name" @@ -88,7 +85,7 @@ while [ $# -gt 0 ]; do done # Validate required arguments -if [ -z "$BUCKET_NAME" ] || -z "$US_VERSION" ] || [ -z "$UK_VERSION" ]; then +if [ -z "$BUCKET_NAME" ] || [ -z "$US_VERSION" ] || [ -z "$UK_VERSION" ]; then echo "Error: Missing required arguments" echo "bucket_name (-b), us_version (-us), and uk_version (-uk) are required" usage @@ -101,10 +98,15 @@ if ! [[ "$TIMEOUT_SECONDS" =~ ^[0-9]+$ ]] || ! [[ "$CHECK_INTERVAL" =~ ^[0-9]+$ fi # Configuration -PROJECT_ID="prod-api-v2-c4d5" -WORKFLOW_LOCATION="us-central1" +PROJECT_ID="${GOOGLE_CLOUD_PROJECT:-$(gcloud config get-value project 2>/dev/null)}" +WORKFLOW_LOCATION="${WORKFLOW_LOCATION:-us-central1}" WORKFLOW_NAME="wait-for-country-packages" +if [ -z "$PROJECT_ID" ]; then + echo "Error: Could not determine project ID. Set GOOGLE_CLOUD_PROJECT environment variable." + exit 1 +fi + echo "Starting workflow execution..." echo "Project: $PROJECT_ID" echo "Location: $WORKFLOW_LOCATION" @@ -132,7 +134,6 @@ echo "Input: $INPUT_JSON" # Execute workflow echo "Executing workflow..." EXECUTION_RESULT=$(gcloud workflows execute "$WORKFLOW_NAME" \ - --project="$PROJECT_ID" \ --location="$WORKFLOW_LOCATION" \ --data="$INPUT_JSON" \ --format="json") diff --git a/.github/workflows/push.yml b/.github/workflows/push.yml index e430f10f0..4b89edcd0 100644 --- a/.github/workflows/push.yml +++ b/.github/workflows/push.yml @@ -65,14 +65,6 @@ jobs: uses: actions/setup-python@v5 with: python-version: "3.11" - - name: Install dependencies (required for finding API model versions) - run: make install - - name: Install jq (required only for GitHub Actions) - run: ".github/install-jq.sh" - - name: Find API model versions and write to environment variable - run: python3 .github/find-api-model-versions.py - - name: Ensure full API and simulation API model versions are in sync - run: ".github/request-simulation-model-versions.sh -b prod-api-v2-c4d5-metadata -us ${{ env.US_VERSION }} -uk ${{ env.UK_VERSION }}" - name: GCP authentication uses: "google-github-actions/auth@v2" with: From c9beec62d793d6a463a30db4c68d2d24a1ad9903 Mon Sep 17 00:00:00 2001 From: nikhilwoodruff Date: Fri, 6 Jun 2025 16:43:16 +0100 Subject: [PATCH 2/2] Add error handling --- changelog_entry.yaml | 4 +++ .../jobs/calculate_economy_simulation_job.py | 28 +++++++++++-------- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/changelog_entry.yaml b/changelog_entry.yaml index e69de29bb..41da6a2b4 100644 --- a/changelog_entry.yaml +++ b/changelog_entry.yaml @@ -0,0 +1,4 @@ +- bump: patch + changes: + fixed: + - Reverted a previous PR. diff --git a/policyengine_api/jobs/calculate_economy_simulation_job.py b/policyengine_api/jobs/calculate_economy_simulation_job.py index 19e39e42a..eb82486b7 100644 --- a/policyengine_api/jobs/calculate_economy_simulation_job.py +++ b/policyengine_api/jobs/calculate_economy_simulation_job.py @@ -296,17 +296,23 @@ def run( progress_log.model_dump(mode="json"), severity="INFO" ) except Exception as e: - trace = traceback.format_exc() - # Send error log to GCP - error_log: V2V1Comparison = V2V1Comparison.model_validate( - { - **comparison_data, - "v2_error": trace, - } - ) - logger.log_struct( - error_log.model_dump(mode="json"), severity="ERROR" - ) + try: + trace = traceback.format_exc() + # Send error log to GCP + error_log: V2V1Comparison = ( + V2V1Comparison.model_validate( + { + **comparison_data, + "v2_error": trace, + } + ) + ) + logger.log_struct( + error_log.model_dump(mode="json"), severity="ERROR" + ) + except Exception as e2: + print("Something really wrong.", e2) + pass # Compute baseline economy logger.log_struct(