Update Intraday Data (Market Hours) #524
This file contains hidden or 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
| name: Update Intraday Data (Market Hours) | |
| on: | |
| workflow_dispatch: | |
| inputs: | |
| force: | |
| description: 'Run even if market is closed (for debugging)' | |
| required: false | |
| default: 'false' | |
| push: | |
| branches: [ main ] | |
| paths: | |
| - 'scripts/**' | |
| - 'requirements.txt' | |
| - '.github/workflows/update-intraday.yml' | |
| schedule: | |
| - cron: '*/5 * * * *' | |
| permissions: | |
| contents: write | |
| concurrency: | |
| group: data-branch-publish | |
| cancel-in-progress: false | |
| jobs: | |
| update: | |
| runs-on: ubuntu-latest | |
| env: | |
| FORCE_RUN: ${{ github.event_name == 'push' && '1' || '0' }} | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - uses: actions/setup-node@v4 | |
| with: | |
| node-version: '20' | |
| cache: 'npm' | |
| - uses: actions/setup-python@v5 | |
| with: | |
| python-version: '3.11' | |
| cache: 'pip' | |
| cache-dependency-path: 'requirements.txt' | |
| - name: Install Python deps | |
| run: | | |
| python -m pip install --upgrade pip | |
| pip install -r requirements.txt | |
| - name: "Gate: decide whether to run (ET)" | |
| id: gate | |
| shell: bash | |
| run: | | |
| python - <<'PY' >> "$GITHUB_OUTPUT" | |
| import datetime | |
| import os | |
| from zoneinfo import ZoneInfo | |
| et = ZoneInfo("America/New_York") | |
| now = datetime.datetime.now(tz=et) | |
| force = ("${{ github.event.inputs.force }}" or "false").lower() == "true" | |
| if os.environ.get("FORCE_RUN") == "1": | |
| print("run=1") | |
| print("reason=forced_by_push") | |
| raise SystemExit(0) | |
| run = True | |
| reason = "ok" | |
| if not force: | |
| if now.weekday() >= 5: | |
| run = False | |
| reason = "weekend" | |
| else: | |
| mins = now.hour * 60 + now.minute | |
| if mins < (9*60 + 30) or mins > (16*60): | |
| run = False | |
| reason = "outside_regular_hours" | |
| print(f"run={1 if run else 0}") | |
| print(f"reason={reason}") | |
| PY | |
| - name: Stop if gated | |
| if: steps.gate.outputs.run != '1' | |
| run: echo "Gated (${{ steps.gate.outputs.reason }}). Skipping update." && exit 0 | |
| - name: Build intraday_latest.json | |
| run: node scripts/fetch_intraday.js | |
| - name: Publish intraday to data branch | |
| shell: bash | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| run: | | |
| set -euo pipefail | |
| rm -rf _data_branch | |
| git clone --depth 1 --branch data "https://x-access-token:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git" _data_branch | |
| cd _data_branch | |
| git config user.name "github-actions[bot]" | |
| git config user.email "41898282+github-actions[bot]@users.noreply.github.com" | |
| mkdir -p public/data | |
| cp -f ../public/data/intraday_latest.json public/data/intraday_latest.json | |
| git add public/data/intraday_latest.json | |
| if git diff --cached --quiet; then | |
| echo "No changes to commit." | |
| exit 0 | |
| fi | |
| git commit -m "data(intraday): refresh intraday_latest.json [skip ci]" | |
| for attempt in 1 2 3; do | |
| echo "Push attempt $attempt..." | |
| git fetch origin data --depth=50 || true | |
| git rebase origin/data || true | |
| if git push origin HEAD:data; then | |
| echo "Push succeeded." | |
| exit 0 | |
| fi | |
| echo "Push rejected; retrying..." | |
| sleep $((attempt*2)) | |
| done | |
| echo "Push failed after retries." | |
| exit 1 |