From d57ef9582ac67a1871161a1cb4ee78e8d84143d7 Mon Sep 17 00:00:00 2001 From: Kegan Maher Date: Mon, 6 Jan 2025 17:55:51 +0000 Subject: [PATCH] ci: workflow checks eligibility server metadata notify Slack on failure: - mismatched last update timestamp - no eligibility types loaded - no users loaded --- .github/workflows/check-metadata.py | 61 ++++++++++++++++++++++++++++ .github/workflows/check-metadata.yml | 42 +++++++++++++++++++ .gitignore | 1 + 3 files changed, 104 insertions(+) create mode 100644 .github/workflows/check-metadata.py create mode 100644 .github/workflows/check-metadata.yml diff --git a/.github/workflows/check-metadata.py b/.github/workflows/check-metadata.py new file mode 100644 index 0000000000..1f1572ad59 --- /dev/null +++ b/.github/workflows/check-metadata.py @@ -0,0 +1,61 @@ +from datetime import datetime, timezone +from functools import cache +import json +from pathlib import Path +import sys + +import requests + + +def get_agency_url(agency: str): + path = Path("./metadata.json") + if not path.exists(): + raise RuntimeError("Metadata file not found") + + config = json.loads(path.read_text()) + return config[agency] + + +@cache +def get_metadata(url: str): + response = requests.get(url, timeout=30) + response.raise_for_status() + return response.json() + + +def check_metadata_timestamp(url: str): + now = datetime.now(tz=timezone.utc) + data = get_metadata(url) + ts = data["db"]["timestamp"] + timestamp = datetime.fromisoformat(ts) + + if not all((timestamp.year == now.year, timestamp.month == now.month, timestamp.day == now.day)): + raise RuntimeError(f"Database timestamp mismatch: {ts}") + + +def check_metadata_users(url: str): + data = get_metadata(url) + users = data["db"]["users"] + + if users < 1: + raise RuntimeError("Database has no users") + + +def check_metadata_eligibility(url: str): + data = get_metadata(url) + eligibility = data["db"]["eligibility"] + + if len(eligibility) < 1: + raise RuntimeError("Database has no eligibility") + + +if __name__ == "__main__": + args = sys.argv + if len(args) < 2: + raise RuntimeError("Usage: check-metadata AGENCY") + + agency = args[1] + url = get_agency_url(agency) + check_metadata_timestamp(url) + check_metadata_users(url) + check_metadata_eligibility(url) diff --git a/.github/workflows/check-metadata.yml b/.github/workflows/check-metadata.yml new file mode 100644 index 0000000000..a1a846e22c --- /dev/null +++ b/.github/workflows/check-metadata.yml @@ -0,0 +1,42 @@ +name: Check eligibility server metadata + +on: + workflow_dispatch: + schedule: + - cron: "0 13 * * *" + +jobs: + check-metadata: + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + agency: [mst, sbmtd] + steps: + - uses: actions/setup-python@v5 + with: + python-version-file: .github/workflows/.python-version + cache: pip + + - name: Install libraries + run: | + python3 -m pip install --upgrade pip + pip install requests + + - name: Create config file + run: | + cat > metadata.json <<- EOM + ${{ secrets.METADATA_CHECK_CONFIG }} + EOM + + - name: Check server metadata + run: python .github/workflows/check-metadata.py ${{ matrix.agency }} + + - name: Report failure to Slack + if: always() + uses: ravsamhq/notify-slack-action@v2 + with: + status: ${{ job.status }} + notify_when: "failure" + env: + SLACK_WEBHOOK_URL: ${{ secrets.ACTION_MONITORING_SLACK }} diff --git a/.gitignore b/.gitignore index 228504a0b4..f2fc5a3f48 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ *.env *fixtures.json !benefits/core/migrations/local_fixtures.json +metadata.json *.mo *.tfbackend *.tmp