Canonical page: [Crawling and Scheduling](https://cloudflavor.io/aemon/docs/crawling/)
Source snapshot: https://github.com/cloudflavor/aemon/blob/a85ee1d024b81c6373acd86379fe12f15c26a7a5/docs/crawling.md

# Crawling and Scheduling

A crawl is HTTP, HTML-to-text and FTS inserts — **no model, no tokens**. That is the
point: the model is spent when you ask a question; the crawler runs on a timer.

```bash
aemon crawl add hono-docs https://hono.dev --include /docs --max-pages 250
aemon crawl run --due-hours 12 --limit-sources 6     # one scheduled pass
aemon crawl status
```

## Discovery

In order:

1. An explicit sitemap.
2. `<base>/sitemap.xml`.
3. `Sitemap:` lines in `robots.txt`.
4. `/sitemap_index.xml`, `/sitemap-0.xml` (sitemap indexes are followed two levels).
5. A one-level link walk.

Real numbers: kubernetes.io 6 958 URLs, docs.astro.build 5 992, pydantic 2 004,
helm.sh 497.

## Politeness and incrementality

- `robots.txt` Disallow rules are honoured, one request per source at a time, with a
  configurable delay (default 250 ms) and a descriptive User-Agent.
- `ETag` / `If-Modified-Since` plus a content hash make a pass incremental. An unchanged
  pass costs one 304 per page and writes nothing (measured: 30 fetched, **0 changed**).
- Progress carries across runs: unseen URLs are crawled before already-indexed ones,
  deepest pages first, so a repeated `--max-pages 50` pass keeps extending the corpus
  instead of re-walking the top of the site.

## Scheduling

`deploy/` ships `aemon-crawl.service` and `aemon-crawl.timer`:

```
crawl run --due-hours 12 --all-due --max-pages 400    # oneshot, Nice=10, idle IO
daily at 09:17 ± 30 min
```

Enable with:

```sh
systemctl --user link $PWD/deploy/*.service $PWD/deploy/*.timer
systemctl --user enable --now aemon-crawl.timer
```

Two settings look redundant and are not:

- **`--all-due` matters.** With a fixed `--limit-sources`, a 20-source list took a week
  to cycle, so "every 48 hours" was only a floor. `--due-hours` is enforced inside the
  crawler, so a duplicated or manual run does nothing.
- **`--due-hours` must stay strictly below the timer's period.** A successful crawl
  stamps `last_run = now`; when `--due-hours` equals the period, every fire finds
  sources only as old as the period minus that run's jitter, nothing clears the
  threshold, and the pass exits `0 sources, 0 fetched` in milliseconds while looking
  green. The shipped pair is daily + 12 h, which refreshes each source at most once a
  day.

The due-window arithmetic is pinned by a test against the real clock, because the
alternative was a timer that fired forever and crawled nothing.

## Status

`aemon crawl status` lists each source with its last run and age in hours; `--json`
adds the same fields under `result.sources`.
