Contents Lesson 6 of 7

10 min read · practitioner

A heartbeat: knowing when the alert went quiet

By the end of this lesson you will have an outside check that expects to hear from your job every weekday evening and tells you when it does not, a job that reports success only when it did its work, and a Friday line in your alert channel saying it is still watching.

What you will see: a check called alert on the Healthchecks.io dashboard, green, last pinged a few minutes after 17:30 New York time; then, with the workflow disabled, the same check red and an email saying it is down, and nothing from GitHub.

The idea: silence looks like a quiet market

A working alert sends nothing on most days, so a dead job looks exactly like a market that never crossed your lines. This job goes quiet when Actions is off in the fork or the file is off the default branch; when a public repository's schedules are disabled after 60 days with no activity; when a run is delayed or dropped; when the data goes stale (no new bar, a delisted symbol, an expired key); and when something else spends the day's calls, so every run meets a 402.

GitHub covers one case: notifications for a failed scheduled run go to whoever last modified its cron line. A run that never happened sends nothing, because whatever would report it did not run.

A dead man's switch

Turn it round: an outside service expects success, and silence is the alarm. Healthchecks.io works this way, with a free Hobbyist plan of 20 checks. A check has a ping URL, https://hc-ping.com/<uuid>; add /fail to report failure. Give it a schedule, which may be a cron expression with a time zone (the same 30 17 * * 1-5 in America/New_York), and a grace time: how late a ping may be before you are told. Two hours covers GitHub's delays.

17:34          job pings success    check: up
17:30 + 2 h    no ping arrives      check: down, email to you

The watchdog lives elsewhere, as Knowing it broke before someone tells you insists: a monitor on the machine it watches says nothing when that machine is gone.

Ping honestly

Ping success only when every rule loaded and every symbol answered, the newest bar is at most one weekday behind today's trading date (a holiday passes, a dead feed fails on day two), delivery worked or had nothing to send, and the state was committed. Anything else pings /fail with a one-line reason as the body (Healthchecks.io keeps up to 100 kB), never a token or a price. In mock mode the job skips the freshness check and says so: the fixtures' dates are fixed, so every mock run would otherwise fail as stale. The ping URL is a secret too: whoever holds it can say all is well. Until it is stored, the two heartbeat steps print a warning and send nothing, rather than fail on an empty URL.

A heartbeat you can see. On a Friday trading date the job also sends Still watching 5 rules on 5 symbols. Newest bar . Alerts this week: 0. It costs no calls, since it all comes from alerts/state.json, and it goes out once per Friday: the date it was sent is recorded in the same file, so the 20:30 run of lesson 4 stays quiet about it. Both daily runs ping the same check; the two-hour grace covers the first.

The prompt

Add a heartbeat to the alert job. Plan first.
- scripts/alert.ts decides the outcome: ok, or failed with a one-line reason (402, stale data, delivery error, invalid rule). Stale means any symbol's newest bar is more than one weekday behind today's New York trading date. In mock mode, skip the staleness check and print that it was skipped: the fixtures' dates are fixed. On failure, write the reason to alert-outcome.txt, print it, exit non-zero.
- On a Friday trading date, also deliver a digest from alerts/state.json, with no extra fetch: rules, symbols, newest bar date, alerts sent this week. Send it once per Friday: record the digest's trading date in alerts/state.json when it is sent, and send nothing more when that date is already there, so the 20:30 run on the same Friday adds no second digest. A weekend run has no trading date: it checks nothing for staleness and reports ok.
- Workflow, after the state step: on success, curl HEARTBEAT_URL; on failure, curl HEARTBEAT_URL/fail with alert-outcome.txt as the body; a ten-second timeout; HEARTBEAT_URL from secrets. If HEARTBEAT_URL is not set, each step prints a warning and exits 0 without calling curl.
- Tests with fixed instants: one weekday old is fine, two is stale, Friday sends the digest and Thursday does not, and a second Friday run does not send it again.

What the agent should do. An outcome helper with tests, a digest formatter, and two workflow steps guarded by if: success() and if: failure(), each skipping with a warning when the secret is missing. Check their position: after the state step, or a run that failed to save still reports success.

Check it yourself

  1. On Healthchecks.io, create a check: schedule Cron, 30 17 * * 1-5, time zone America/New_York, grace two hours. Copy its ping URL.

The next checks run on GitHub, so commit, push the branch, open a pull request and merge it:

npm test
git status          # the files you expect, and no .env file
git add -A
git commit -m "feat(alert): honest heartbeat, a stale-data failure, and a Friday digest"
  1. Run the workflow before you store the URL: green, with a warning that no heartbeat was sent. Then gh secret set HEARTBEAT_URL and run it again. The check turns up within a minute.
  2. Give one rule a ticker that does not exist, push and run. The run is red, the check logs a failure with your reason, and you are notified. It costs one call live. Revert.
  3. Run gh workflow disable alert.yml and wait past the next 17:30 plus the grace time. The check goes down and you are told, with nothing from GitHub. Then gh workflow enable alert.yml.
  4. On Friday evening the digest arrives in your alert channel once, although two scheduled runs happen that evening.

Break it on purpose

The ping is a workflow step, so the break is in alert.yml: move the step that pings success above the step that runs the alert, remove the exchange suffix from one rule's symbol, push and run. The run is red, yet the check's log on Healthchecks.io starts with a success ping sent before a single rule was read: that ping measured that a runner started. Only the failure step further down turned the check red, and a run that never reaches it, such as one cancelled by hand, skips every if: failure() step and leaves the check green. Move the success ping back after the state step.

Compare with ours

git fetch upstream, then git diff upstream/ref/runs-without-you/6 -- .github src/lib/alerts.

Our version, on branch ref/runs-without-you/6
      - name: Heartbeat
        if: success()
        run: |
          if [ -z "$HEARTBEAT_URL" ]; then echo "::warning::HEARTBEAT_URL is not set: no heartbeat sent."; exit 0; fi
          curl -fsS -m 10 "$HEARTBEAT_URL"
        env:
          HEARTBEAT_URL: ${{ secrets.HEARTBEAT_URL }}
      - name: Heartbeat, failed
        if: failure()
        run: |
          if [ -z "$HEARTBEAT_URL" ]; then echo "::warning::HEARTBEAT_URL is not set: no failure ping sent."; exit 0; fi
          curl -fsS -m 10 --data-binary @alert-outcome.txt "$HEARTBEAT_URL/fail"
        env:
          HEARTBEAT_URL: ${{ secrets.HEARTBEAT_URL }}

Without the secret, curl "" would fail with a message about a URL, not about a missing secret; the guard says what is missing instead. If the job dies before writing its reason, the failure ping fails too, and the check still goes down after the grace time.

Try it now

Checkpoint: you would now hear about it if the job stopped.

  1. The commit you made for the checks is merged, and the heartbeat's secret is stored:
git fetch origin
git log --oneline -1 origin/main -- .github/workflows/alert.yml
gh secret list          # HEARTBEAT_URL among the names
  1. Write in PLAN.md the date of your repository's last commit, and the date 60 days after it.
  2. Take the lesson check. It describes a symptom and asks which quiet failure caused it.