Skip to content

Commit d57ef95

Browse files
committed
ci: workflow checks eligibility server metadata
notify Slack on failure: - mismatched last update timestamp - no eligibility types loaded - no users loaded
1 parent 9817033 commit d57ef95

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed

.github/workflows/check-metadata.py

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
from datetime import datetime, timezone
2+
from functools import cache
3+
import json
4+
from pathlib import Path
5+
import sys
6+
7+
import requests
8+
9+
10+
def get_agency_url(agency: str):
11+
path = Path("./metadata.json")
12+
if not path.exists():
13+
raise RuntimeError("Metadata file not found")
14+
15+
config = json.loads(path.read_text())
16+
return config[agency]
17+
18+
19+
@cache
20+
def get_metadata(url: str):
21+
response = requests.get(url, timeout=30)
22+
response.raise_for_status()
23+
return response.json()
24+
25+
26+
def check_metadata_timestamp(url: str):
27+
now = datetime.now(tz=timezone.utc)
28+
data = get_metadata(url)
29+
ts = data["db"]["timestamp"]
30+
timestamp = datetime.fromisoformat(ts)
31+
32+
if not all((timestamp.year == now.year, timestamp.month == now.month, timestamp.day == now.day)):
33+
raise RuntimeError(f"Database timestamp mismatch: {ts}")
34+
35+
36+
def check_metadata_users(url: str):
37+
data = get_metadata(url)
38+
users = data["db"]["users"]
39+
40+
if users < 1:
41+
raise RuntimeError("Database has no users")
42+
43+
44+
def check_metadata_eligibility(url: str):
45+
data = get_metadata(url)
46+
eligibility = data["db"]["eligibility"]
47+
48+
if len(eligibility) < 1:
49+
raise RuntimeError("Database has no eligibility")
50+
51+
52+
if __name__ == "__main__":
53+
args = sys.argv
54+
if len(args) < 2:
55+
raise RuntimeError("Usage: check-metadata AGENCY")
56+
57+
agency = args[1]
58+
url = get_agency_url(agency)
59+
check_metadata_timestamp(url)
60+
check_metadata_users(url)
61+
check_metadata_eligibility(url)

.github/workflows/check-metadata.yml

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
name: Check eligibility server metadata
2+
3+
on:
4+
workflow_dispatch:
5+
schedule:
6+
- cron: "0 13 * * *"
7+
8+
jobs:
9+
check-metadata:
10+
runs-on: ubuntu-latest
11+
strategy:
12+
fail-fast: false
13+
matrix:
14+
agency: [mst, sbmtd]
15+
steps:
16+
- uses: actions/setup-python@v5
17+
with:
18+
python-version-file: .github/workflows/.python-version
19+
cache: pip
20+
21+
- name: Install libraries
22+
run: |
23+
python3 -m pip install --upgrade pip
24+
pip install requests
25+
26+
- name: Create config file
27+
run: |
28+
cat > metadata.json <<- EOM
29+
${{ secrets.METADATA_CHECK_CONFIG }}
30+
EOM
31+
32+
- name: Check server metadata
33+
run: python .github/workflows/check-metadata.py ${{ matrix.agency }}
34+
35+
- name: Report failure to Slack
36+
if: always()
37+
uses: ravsamhq/notify-slack-action@v2
38+
with:
39+
status: ${{ job.status }}
40+
notify_when: "failure"
41+
env:
42+
SLACK_WEBHOOK_URL: ${{ secrets.ACTION_MONITORING_SLACK }}

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
*.env
33
*fixtures.json
44
!benefits/core/migrations/local_fixtures.json
5+
metadata.json
56
*.mo
67
*.tfbackend
78
*.tmp

0 commit comments

Comments
 (0)