Skip to content

Scheduling Runs

Scwrap has no scheduling. To run a Scraper on a timetable, start its Runs from a scheduler outside Scwrap, such as a GitHub Actions cron or a Cloudflare Cron Trigger, with an Idempotency-Key.

Scwrap has no scheduling: it never starts a Run by itself, by design (Non-goals). A Run starts only when a caller sends POST /scrapers/<id>/run. To run a Scraper on a timetable, send that request from a scheduler outside Scwrap, such as a GitHub Actions cron or a Cloudflare Cron Trigger.

Whatever the scheduler, send an Idempotency-Key built from the job's name and its planned time, such as earthquakes-2026-11-30. A scheduler that fires twice, or a retry after a lost response, then starts one Run instead of two, as long as the repeat comes within 24 hours.

AnswerWhat the scheduler does
202The Run started.
200A repeat of the same key: the Run already started.
409, 503Retry after a few seconds with the same key. Nothing ran yet, or the Run for this key is still starting.
400, 401, 404, 422Fail the job and alert. Retrying does not help.

Give each scheduler its own named run token limited to the Scrapers it starts. Every Run it starts then carries that token's name as tokenName, and a leaked token reaches only those Scrapers.

The scheduler only starts the Run. To learn how it ended, read its record, or list the Scraper's Runs with GET /scrapers/<id>/runs and alert on failed ones.

GitHub Actions ​

A workflow in your Scraper repository, with the instance URL and run token as secrets of a GitHub environment:

yaml
name: Nightly earthquakes

on:
  schedule:
    - cron: "0 6 * * *"
  workflow_dispatch:

permissions: {}

jobs:
  run:
    runs-on: ubuntu-latest
    environment: scwrap
    timeout-minutes: 5
    steps:
      - name: Start the Run
        env:
          SCWRAP_INSTANCE_URL: ${{ secrets.SCWRAP_INSTANCE_URL }}
          SCWRAP_INSTANCE_RUN_TOKEN: ${{ secrets.SCWRAP_INSTANCE_RUN_TOKEN }}
        run: |
          curl -sS --fail-with-body --retry 4 --retry-delay 5 \
            -X POST "$SCWRAP_INSTANCE_URL/scrapers/earthquakes/run" \
            -H "Authorization: Bearer $SCWRAP_INSTANCE_RUN_TOKEN" \
            -H "Idempotency-Key: earthquakes-$(date -u +%F)" \
            -H "content-type: application/json" \
            -d '{"minMagnitude": "significant", "period": "week"}'
  • The key is the job name and the day, so running the workflow again by hand the same day returns that day's Run instead of starting another.
  • curl --retry retries a 503 with the same body and key. A failed request fails the job, and GitHub notifies the workflow's owner.
  • GitHub starts scheduled workflows late or skips them when it is busy, and turns them off in a public repository after 60 days without activity (Events that trigger workflows). Use it when a missed day is acceptable.

A Cron Trigger Worker ​

A small Worker of your own, outside the instance, with a Cron Trigger. Cloudflare starts it on time, and it can live in any account.

jsonc
{
  "name": "earthquakes-schedule",
  "main": "src/index.ts",
  "compatibility_date": "2026-11-12",
  "triggers": { "crons": ["0 6 * * *"] },
  "vars": { "SCWRAP_INSTANCE_URL": "https://scwrap.example.com" }
}
ts
interface Env {
  readonly SCWRAP_INSTANCE_URL: string;
  readonly SCWRAP_INSTANCE_RUN_TOKEN: string;
}

const input = { minMagnitude: "significant", period: "week" };

export default {
  async scheduled(controller, env) {
    const planned = new Date(controller.scheduledTime).toISOString();

    for (let attempt = 1; ; attempt += 1) {
      const response = await fetch(
        `${env.SCWRAP_INSTANCE_URL}/scrapers/earthquakes/run`,
        {
          body: JSON.stringify(input),
          headers: {
            authorization: `Bearer ${env.SCWRAP_INSTANCE_RUN_TOKEN}`,
            "content-type": "application/json",
            "idempotency-key": `earthquakes-${planned}`,
          },
          method: "POST",
        }
      );

      if (response.ok) {
        return;
      }

      if (attempt === 5 || ![409, 503].includes(response.status)) {
        throw new Error(`Run not started: ${response.status}`);
      }

      await new Promise((resolve) => setTimeout(resolve, attempt * 2000));
    }
  },
} satisfies ExportedHandler<Env>;

Store the token with wrangler secret put SCWRAP_INSTANCE_RUN_TOKEN and deploy with wrangler deploy.

  • The key is the trigger's planned time, controller.scheduledTime, so a second invocation for the same tick starts no second Run.
  • A thrown error marks the invocation failed in the Worker's metrics and logs; alert on it there.
  • The Worker only sends one request per tick, which fits the Workers Free plan.