# Polling and failures

<a id="polling-and-failures"></a>

- Polling pauses while the Mac sleeps or is locked. On wake, unlock or panel open, sources refresh if due.
- Failures back off with repeated attempts, capped at 30 minutes or the configured interval if that is longer. They never shorten that interval. A success returns to the normal schedule.
- An answer can count as a failure too, when the service reports an error inside it: see [`fail`](https://legatobar.com/docs/source-polling.md#when-the-answer-reports-an-error).
- A data request gives up after 10 seconds without data, or 20 seconds in all, and has a 1 MB response limit, enforced while the answer downloads: Legato stops reading at 1 MB. Redirects must keep the same scheme, host and effective port.
- Before the first answer, a card shows what it is waiting for or an error. Later failures keep the last good value and add an error line. The footer shows the newest successful update on the page, so also check individual card errors.
- To read a page's sources again without waiting, click the arrow left of the pin at the top of the panel (⌘R). A server's cooldown still holds.
- macOS may ask for local-network access the first time Legato reaches a local service.

### When a service stops answering

A failed fetch keeps each card's last answer, with the error under it, so one dropped request doesn't blank a page. A service that has stopped answering for good would therefore stay green. Give the status card that stands for the service `failed`, and after a few failures in a row it says so:

```json
{ "id": "host", "type": "status", "label": "pve-lab", "state": "ok",
  "source": { "ref": "pve", "map": { "detail": "data.pveversion" },
              "failed": { "state": "critical", "detail": "Proxmox isn't answering", "after": 3 } } }
```

- The card takes that state and detail and notifies, as any status change does. Without `detail` it says "Not answering since 2:14 PM" (or "Not answering" when the source never answered).
- The next answer puts the card back to what the answer says.
- `after` counts failed fetches, which back off (see above): with `every` of `1m`, the third comes about 3 minutes after the first. Polling pauses while the Mac sleeps, so this tells you about the service only while the Mac is awake; for a service that must be watched around the clock, use a monitor such as Uptime Kuma as well.
- It applies before `when`, so a card that shows on a condition still shows when its source fails.
- A `fleet` takes `failed` too. Its items can't say how they stand while the source is silent, so the fleet shows one dot in their place, named by the card's label ("Server"), with that state and detail: it lights the page and notifies once, not once per item.
- A rate limit or a refused secret counts as a failure too: the card can't say what the service is doing.
- So does an answer the source's [`fail`](https://legatobar.com/docs/source-polling.md#when-the-answer-reports-an-error) turns down. Without `detail`, the card then says "Reporting an error since 2:14 PM", since the service is answering.

### When the answer reports an error

Some services answer HTTP 200 even when the request went wrong, and say so in the body: GraphQL APIs send an `errors` list, others `success: false`. Legato would take that as an answer, and the cards would show dashes. Give the source `fail`, and such an answer counts as a failed fetch:

```json
"sources": { "gh": { "url": "https://api.github.com/graphql", "method": "POST", "secret": "github", "body": { "query": "{ viewer { login } }" },
                     "fail": { "when": { "path": "errors", "exists": true }, "message": "errors[0].message" } } }
```

- `when` is a [condition](https://legatobar.com/docs/mapping-conditions.md#conditions), checked on the answer after [`derive`](https://legatobar.com/docs/mapping.md#derived-values), so it can test a derived value. `all` and `any` combine tests.
- **Test that the error is there** with [`exists`](https://legatobar.com/docs/mapping-conditions.md#conditions). Any other test whose path is missing or null doesn't hold here, whatever it compares, so an answer without `errors` passes; `equals: null` never fires.
- `message` is a [rule](https://legatobar.com/docs/mapping.md#mapping) for the service's reason. The note under the cards is "api.github.com reported an error: Bad credentials", on one line and cut at 200 characters, or "api.github.com reported an error" when there's no `message` or it gives nothing.
- It's then exactly a failed request: each card keeps its last answer with the note, polling [backs off](https://legatobar.com/docs/source-polling.md#polling-and-failures), a status card's `failed` counts it, and [History](https://legatobar.com/docs/source-reference.md#history) and [sparkline readings](https://legatobar.com/docs/mapping-sparklines.md#sparkline-readings) keep nothing from it. A rate limit the response's headers announce still holds.
- `fail` goes in `sources.<name>`, since it decides for every card on the source; a card's `failed` says what one status card shows after several failures. Only HTTP sources take it: calendars, reminders and History are read on this Mac.
- Adding `fail` to a source that [collects](https://legatobar.com/docs/source-reference.md#history) keeps its history as it is: the same rows and the same history in Settings → History.
- **Preview Source** shows an answer that `fail` turns down, with the reason above the cards.
- **A GraphQL answer can carry data and errors together**, such as one repository it couldn't read in a list of twenty. A rule on `errors` alone then holds the whole page on its last answer. Fail only when something every good answer has is missing: `{ "all": [ { "path": "errors", "exists": true }, { "path": "data.viewer.repositories", "exists": false } ] }`.

| Service | `fail` |
| --- | --- |
| GitHub GraphQL, Linear | `{ "when": { "path": "errors", "exists": true }, "message": "errors[0].message" }` |
| Cloudflare | `{ "when": { "path": "success", "equals": false }, "message": "errors[0].message" }` |
| Parcel | `{ "when": { "path": "success", "equals": false }, "message": "error_message" }` |
| Tautulli | `{ "when": { "path": "response.result", "not": "success" }, "message": "response.message" }` |
| SABnzbd | `{ "when": { "path": "status", "equals": false }, "message": "error" }` |

With [named requests](https://legatobar.com/docs/source-requests.md#requests-and-formats), the path starts with the request's name: `gql.errors[0].message`, or `{ "any": [ … ] }` over several.

### Rate limits and unchanged responses

- **Server-requested waits.** Legato honors `Retry-After` as seconds or an HTTP date on HTTP 429, on 503 when it carries `Retry-After`, and on 403 responses carrying rate-limit signals. A 503 without `Retry-After` is an ordinary failure and backs off as usual. Ordinary permission-denied 403 responses are treated as errors, not quota signals. A recognized rate-limit refusal without a usable deadline waits at least 60 seconds.
- **Exhausted quotas.** When `X-RateLimit-Remaining` or `RateLimit-Remaining` reaches 0, Legato waits for the matching `X-RateLimit-Reset` or `RateLimit-Reset`. Services send that in different forms, and Legato reads any of them: a delay in seconds, a Unix timestamp (any number from 1,000,000,000 up), an HTTP date or an ISO 8601 date. A successful response can therefore postpone the next poll too. Where several hints apply, Legato keeps the latest deadline, but no hint holds a source back for more than 24 hours.
- **Shared waits.** Sources using the same origin and configured credentials share the cooldown. Polling, **Fetch Once** and buttons respect it; an early request shows when it can try again. Rate-limited button and checklist-row commands are never replayed automatically. After the wait, click the button or tick the row again if you still want the action.
- **Conditional GETs.** For eligible GET requests, Legato retains the response and its ETag or Last-Modified validator in memory. A later request sends `If-None-Match` or `If-Modified-Since`; HTTP 304 reuses that response and reevaluates the current mappings and derived values. It does not fetch a full body again.
- **Cache boundaries.** POST reads and button requests are not cached. Cached responses are separated by URL and request headers, including authentication; changing credentials does not reuse another credential's data. `no-store` and `Vary: *` prevent storage. This is not an offline cache: reuse requires a server's 304 response, and the cache is lost when the app closes.

