diff --git a/.github/workflows/health_check.yml b/.github/workflows/health_check.yml new file mode 100644 index 00000000..1a521c96 --- /dev/null +++ b/.github/workflows/health_check.yml @@ -0,0 +1,29 @@ +name: Health check + +on: + workflow_dispatch: + schedule: + - cron: "0 5 * * *" + +jobs: + health-check: + name: Health check + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Install Poetry + uses: snok/install-poetry@v1 + + - name: Set up python + uses: actions/setup-python@v5 + with: + python-version-file: pyproject.toml + cache: poetry + + - name: Install dependencies + run: poetry install --only main + + - name: Run health check + run: poetry run python -m x_notes.health_check diff --git a/x_notes/health_check.py b/x_notes/health_check.py new file mode 100644 index 00000000..02031c4b --- /dev/null +++ b/x_notes/health_check.py @@ -0,0 +1,22 @@ +import json +from datetime import datetime, timedelta, timezone +from io import BytesIO + +import requests + +MAX_AGE_IN_DAYS = 3 + + +def health_check() -> None: + r = requests.get( + "https://github.com/andylolz/x-community-notes/raw/gh-pages/_data/meta.json" + ) + meta = json.load(BytesIO(r.content)) + delta = datetime.now(timezone.utc) - datetime.fromisoformat(meta["most_recent"]) + + if delta > timedelta(days=MAX_AGE_IN_DAYS): + raise Exception(f"Most recent tweet is more than {MAX_AGE_IN_DAYS} days old.") + + +if __name__ == "__main__": + health_check()