# [Polled] — a function machines call on a timer

> `[Polled(EverySeconds = 5)]` declares that machines call this function on a timer — a host's poll, a heartbeat, a device's report. It changes nothing about how the function runs. `osy lint` then judges what every call pays when nothing changed (cost-polled-function-pays-for-what-did-not-change) and names the fix for each shape it finds.

<!-- id: function-polled · area: function · stability: preview · html: https://osysharp.com/reference/function/polled/ -->

## Summary        {#summary}
How often a function is called is a fact about its CALLER, and the source does not hold it. A host that polls every
five seconds calls its poll 17,280 times a day; nothing in the poll's own code says so, and every per-call cost rule
judges it as if it ran once. Mark it, and `osy lint` knows:

```osy title="a poll a host calls every five seconds" test app=function-polled
entity Host {
  [MaxLength(40)] string Name = "";
  long Seen;
  security { allow read when IsAuthenticated; }
}
class PollAnswer { public long Version; public bool Unchanged; }

[Polled(EverySeconds = 5)]
PollAnswer Poll(Host host, long known) {
  if (host.Seen == known) { return new PollAnswer { Version = known, Unchanged = true }; }
  return new PollAnswer { Version = host.Seen };
}
```

## Signature      {#signature}
```osy syntax
[Polled(EverySeconds = <whole seconds, 1 to 86400>)]
<ReturnType> <Name>(<parameters>) { … }
```

## Description    {#description}

### What it changes   {#effect}
Nothing about how the function runs, is called, is secured or is deployed. It is a statement for the linter: the
function is judged by [A polled function pays only for what changed](https://osysharp.com/reference/function/polled-cost/) on what EVERY call pays when nothing changed — a read the same for every
caller, a read per row in a loop, a write on every path, housekeeping for everybody, a `runas` — and each finding
says how many calls a day the interval means for each caller.

### Where it goes   {#where}
On the FUNCTION, not on the door it is called through. The same function may be reached by a REST endpoint, an MCP
tool or a page; it is polled whichever one a machine uses, so it is said once, where the function is. A `static`
member of a class takes it as a top-level function does.

### When you do not need it   {#inferred}
Two other ways tell the linter a function is called on a timer, and need no attribute:

- a workflow reminder's `ThenEvery`, or a schedule rule, that runs it more often than once an hour with a literal
  cadence ([Schedule (recurring work)](https://osysharp.com/reference/scheduling/schedule/));
- an `Endpoint` of a `RestApi` that takes an API key and nothing else, or an MCP `Tool`, whose answer is a class with a
  member named `PollSeconds`, `LeaseSeconds` or `NextPollAt`. The linter infers a poll from that, at CONSIDER
  (`cost-machine-door-pays-for-what-did-not-change`); the attribute makes it a SHOULD and adds the interval.

### What the compiler refuses   {#refusals}
- No interval, or one that is not a whole number of seconds from 1 to 86400: `[Polled]`, `[Polled(EverySeconds = 0)]`,
  `[Polled(5)]`. Write `[Polled(EverySeconds = 5)]`.
- Beside an attribute that says the function is not called by a machine on a timer: `[OnStart]` (it runs when the
  app is loaded), `[On]` (when its event is raised), `[Client]` (in the browser), `[AuthMethod]`, `[Test]`.

## Examples       {#examples}
A device reports every minute; the report is its own row, written when it moved:

```osy title="a device's report, written only when it changed" test app=function-polled-report
entity Device {
  [MaxLength(40)] string Name = "";
  int Reading;
  security { allow read when IsAuthenticated; }
}

[Polled(EverySeconds = 60)]
void Report(Device device, int reading) {
  if (device.Reading != reading) { device.Reading = reading; }
}
```

## See also       {#see-also}
- [A polled function pays only for what changed](https://osysharp.com/reference/function/polled-cost/) — the rule, each shape it finds, and the fix for each
- [Cost.Mark() and Cost.StatementsSince(mark)](https://osysharp.com/reference/testing/cost/) — hold what an idle call costs with a test
- [Schedule (recurring work)](https://osysharp.com/reference/scheduling/schedule/) — work for everybody belongs on a schedule, not on a caller's path
