> ## Documentation Index
> Fetch the complete documentation index at: https://iii.dev/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Schedule a cron task

> Run a function on a recurring schedule by observing cron::on-tick.

`worker.observe({ function_id: "cron::on-tick", args: { expression } }, target)` means: when the
cron worker ticks for `expression`, `target` runs. The cron worker starts a job for each expression
its observers ask for, and stops it when the last observer of that expression goes away.

`expression` has seven fields: second, minute, hour, day of month, month, day of week, and year.
This observer runs `reports::daily-summary` every day at 09:00:00:

> [tab] Node / TypeScript
>
> ```typescript theme={"theme":{"light":"catppuccin-latte","dark":"dark-plus"}}
> import type { Observed } from "iii-sdk";
>
> worker.registerFunction("reports::daily-summary", async ({ input }: Observed<{ scheduled_time: string }>) => {
>   // build the report for input.scheduled_time
>   return null;
> });
>
> worker.observe(
>   { function_id: "cron::on-tick", args: { expression: "0 0 9 * * * *" }, capture: ["input"] },
>   "reports::daily-summary",
> );
> ```

> [tab] Python
>
> ```python theme={"theme":{"light":"catppuccin-latte","dark":"dark-plus"}}
> def daily_summary(observed: dict) -> None:
>     # build the report for observed["input"]["scheduled_time"]
>     return None
>
> worker.register_function("reports::daily-summary", daily_summary)
>
> worker.observe(
>     {"function_id": "cron::on-tick", "args": {"expression": "0 0 9 * * * *"}, "capture": ["input"]},
>     "reports::daily-summary",
> )
> ```

> [tab] Rust
>
> ```rust theme={"theme":{"light":"catppuccin-latte","dark":"dark-plus"}}
> use iii_sdk::{Capture, Error, Observed, RegisterFunction, Source};
> use serde_json::{json, Value};
>
> worker.register_function(
>     "reports::daily-summary",
>     RegisterFunction::new(|_observed: Observed<Value>| {
>         // build the report for observed.input["scheduled_time"]
>         Ok::<_, Error>(())
>     }),
> );
>
> worker.observe(
>     Source::function("cron::on-tick")
>         .args(json!({ "expression": "0 0 9 * * * *" }))
>         .capture([Capture::Input]),
>     "reports::daily-summary",
> );
> ```

> [tab] Go
>
> ```go theme={"theme":{"light":"catppuccin-latte","dark":"dark-plus"}}
> client.RegisterFunction("reports::daily-summary", func(ctx context.Context, data json.RawMessage) (any, error) {
>     // build the report for the tick's scheduled_time: data is an iii.Observed, the tick its Input
>     return nil, nil
> })
>
> client.Observe(iii.Source{
>     FunctionID: "cron::on-tick",
>     Args:       iii.Args{"expression": "0 0 9 * * * *"},
>     Capture:    []iii.Capture{iii.CaptureInput},
> }, "reports::daily-summary")
> ```

With `capture: ["input"]`, the target receives the tick as the payload's `input`:
`{ expression, scheduled_time, actual_time }`. An expression the cron worker cannot parse rejects
the observer with `INVALID_CRON_EXPRESSION`.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.