Skip to content

Daily trigger

The daily trigger starts the daily collection at 10:37 UTC. It's a small Cloudflare Worker, wfheaven-trigger, with a Cron Trigger. At 10:37 UTC it calls GitHub's API to start the Daily collection workflow in daily mode, as the Run workflow button does (ADR 0015).

GitHub's own schedule started the run hours late on several days, so GitHub's schedule is now only a fallback, at 15:37 UTC. A once-a-day guard in the workflow makes sure that only one daily run a day reads the job sources, whatever starts it.

The code is in trigger/. It's separate from the demo site's Worker, so the GitHub token is never in a Worker that faces the public.

How it works

  1. At 10:37 UTC, Cloudflare runs the Worker's scheduled() handler.
  2. The Worker sends POST /repos/hajdigergely/WFHeaven/actions/workflows/collect.yml/dispatches with {"ref": "main", "inputs": {"mode": "daily"}} and the GITHUB_TOKEN secret.
  3. GitHub answers 204 No Content and queues the run. The Worker logs Daily run started: GitHub answered HTTP 204.
  4. The workflow's first job, guard, runs daily/once_a_day.py. It asks GitHub's API for today's runs of the workflow and their steps. If any of them already started its Collect jobs step today (UTC), the collect job is skipped and no job source is read. Otherwise, the run goes on.
  5. At 15:37 UTC, GitHub's schedule starts the workflow again, often late. On a day the Cron Trigger worked, the guard stops it. On a day it didn't, this run is the daily run.

If GitHub answers with a server error (5xx), or the request fails, the Worker waits 30 seconds and tries once more. Any other answer, such as 401 for an expired token, isn't retried, because a retry gets the same answer. When the run can't be started, the Worker logs GitHub's status and short message, such as Bad credentials, and the invocation shows as failed in Cloudflare. The log never contains the token.

What the guard counts

The source rules matter more than one day's email, so the guard is strict:

  • Any run that started Collect jobs today counts, whatever happened next. A run that failed while emailing, or even while collecting, may already have read the sources, so it blocks every other daily run that day. Fix the cause; tomorrow's run picks up the jobs, because picks of an unsent email aren't remembered.
  • Re-runs count too. The guard reads every attempt of a run. If you re-run a run that got to Collect jobs, it stops. Re-run failed jobs skips the guard job, so the collect job checks again in its first step, Check again for an earlier collection, which fails the re-run.
  • A test email never counts, because it never reaches Collect jobs, and it skips the guard's check, because it reads no job source. The workflow also names it Test email (run-name), so it's easy to tell apart in the run history.
  • A run stopped before Collect jobs doesn't count, for example one that failed at Read testers, or one the guard stopped.
  • If GitHub's API can't be read, the guard fails and nothing runs. The 15:37 UTC schedule tries again.

The guard finds the step by its name, Collect jobs. If you rename the step, change COLLECT_STEP in daily/once_a_day.py too; a test checks that they match.

Set it up

You do these steps once, as the maintainer. Never paste the token into a chat, a file, or a command line.

Create the GitHub token

  1. On GitHub, click your profile picture, and then click Settings > Developer settings > Personal access tokens > Fine-grained tokens.
  2. Click Generate new token.
  3. Fill in the form:
    • Token name: wfheaven-trigger
    • Resource owner: your account
    • Expiration: 1 year, or a custom date. Write the date down: it goes in the roadmap.
    • Repository access: Only select repositories, and then hajdigergely/WFHeaven
    • Permissions > Repository permissions > Actions: Read and write. GitHub adds Metadata: Read-only by itself. Leave every other permission at No access.
  4. Click Generate token. Leave the page open: GitHub shows the token only once.

Deploy the Worker

Run these commands from the repository root. The Worker uses the same Cloudflare account as the demo, so wrangler login may already be done.

cd trigger
npm ci
npx wrangler login              # only if you aren't logged in
npx wrangler deploy
npx wrangler secret put GITHUB_TOKEN

At the wrangler secret put prompt, paste the token, and then press Enter. Then close the GitHub page.

wrangler deploy prints the schedule, 37 10 * * *. The Worker has no web address: workers_dev is false and it has no route, so nothing on the internet can call it.

Check that it works

Don't test the Worker by starting it by hand: each start runs a real daily run, which reads Himalayas and emails every tester. Check the first scheduled start instead:

  1. After 10:37 UTC, open GitHub, click Actions, and then click Daily collection. Today's run is listed as started by workflow_dispatch, a few minutes after 10:37 UTC.
  2. In the Cloudflare dashboard, open Workers & Pages > wfheaven-trigger

    Logs. The 10:37 UTC invocation shows Daily run started: GitHub answered HTTP 204.

  3. After 15:37 UTC, the scheduled run appears, usually later. Its guard job says A daily run already collected jobs today, and the collect job is skipped.

To follow the next invocation live, run npx wrangler tail in trigger/.

Renew the token

The token stops working on its expiry date. The Worker then logs GitHub answered HTTP 401, "Bad credentials", and the 15:37 UTC schedule starts the day's run instead, late.

  1. Before the expiry date, open the token on GitHub, at Fine-grained tokens > wfheaven-trigger, and click Regenerate token.
  2. In trigger/, run npx wrangler secret put GITHUB_TOKEN and paste the new token at the prompt.
  3. Update the expiry date in the roadmap.

Change the time

The time is in two places: the Cron Trigger in trigger/wrangler.jsonc and the fallback schedule in .github/workflows/collect.yml. Keep the fallback a few hours after the Cron Trigger, and keep both away from minute 00, which Jobicy's terms rule out (Job sources). Then run npx wrangler deploy in trigger/, and update every page that mentions the time.

Tests

The Worker's tests, in trigger/test/, replace GitHub's API with a fake: what's sent, the retry, and that the token is never logged. The guard's tests are in tests/test_once_a_day.py, also with a fake GitHub.

(cd trigger && npm ci && npm run typecheck && npm test)
python3 -m unittest tests.test_once_a_day

If something goes wrong, see The daily run didn't start.