Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CI: workflow checks eligibility server metadata #2610

Merged
merged 1 commit into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading