Skip to content

Commit

Permalink
ci: workflow checks eligibility server metadata
Browse files Browse the repository at this point in the history
notify Slack on failure:

- mismatched last update timestamp
- no eligibility types loaded
- no users loaded
  • Loading branch information
thekaveman committed Jan 6, 2025
1 parent 9817033 commit d57ef95
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 0 deletions.
61 changes: 61 additions & 0 deletions .github/workflows/check-metadata.py
Original file line number Diff line number Diff line change
@@ -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)
42 changes: 42 additions & 0 deletions .github/workflows/check-metadata.yml
Original file line number Diff line number Diff line change
@@ -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 }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
*.env
*fixtures.json
!benefits/core/migrations/local_fixtures.json
metadata.json
*.mo
*.tfbackend
*.tmp
Expand Down

0 comments on commit d57ef95

Please sign in to comment.