-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ci: workflow checks eligibility server metadata
notify Slack on failure: - mismatched last update timestamp - no eligibility types loaded - no users loaded
- Loading branch information
1 parent
9817033
commit d57ef95
Showing
3 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters