Events posted by outside systems

An outside system can post a named event to start a DAG or wake a step already in a run. In this example a payment starts a run, which waits for the same order to be packed:

examples/docs/named_events.py
"""A payment starts a run; that run waits for its package to be packed.

`ship_paid` starts once per order paid (`POST /api/v1/events` or `schedr events post paid
--key order-123`), and waits for that order's `packed` event. Run alone, as `dags test` does,
nothing posts it and the wait runs out: `TimedOut`, which `late` handles.
"""

from datetime import timedelta

from schedr import Dag, Event, EventNamed, TimedOut, dag_ctx, task, wait_for
from schedr.types import Json


@task
def ship(event: Event[Json]) -> str:
    """Ship the package identified by the packing event."""
    return f"ship {event.key}: {event.data}"


@task
def late(outcome: TimedOut) -> str:
    """Report a package that did not arrive in time."""
    del outcome
    return "packing timed out"


with Dag(
    "ship_paid",
    description="Ships each paid order once its package is packed.",
    trigger=EventNamed("paid"),
):
    packed = wait_for(EventNamed("packed", key=dag_ctx.event.key), max_wait=timedelta(seconds=3))
    ship(packed.on(Event[Json]))
    late(packed.on(TimedOut))

Posting

Post as a signed-in user or with a token, as for triggering a run. For example:

curl -X POST "$SCHEDR_URL/api/v1/events" -H 'Content-Type: application/json' \
  -H "Authorization: Bearer $SCHEDR_TOKEN" \
  -H 'Idempotency-Key: payment-request-123' \
  -d '{"name":"paid","key":"order-123","data":{"amount":42},"at":"2026-11-08T10:00:00Z"}'
schedr events post packed --key order-123 --data '{"box":"A"}'

The name is written as a DAG ID. The key is 1 to 1024 UTF-8 bytes, with no control characters, line separators or bidirectional marks. Data is JSON, null if omitted, and is limited by [events] max_size (1 MiB by default). at is when the event happened; it defaults to the posting time. By default, it may be at most a minute ahead of that time. An event for a moment before the previous run’s gets an empty window (since is its own at). The CLI’s --at supplies it and --data also accepts @file.json.

Each post creates an event, even if its name and key have been posted before. To retry a request without creating another event, send the same Idempotency-Key: the API returns the first stored event. Reusing that request key for a different event is rejected. This request key is separate from the event’s key.

Posting from a task

A task or trigger can post and list events in its own deployment without settings:

from schedr import Dag, Event, EventNamed, api, task, trigger
from schedr.types import Json

@task
def pay() -> None:
    api.post_event("paid", key="order-123", data={"amount": 42},
                   idempotency_key="pay-order-123")
    print(api.events(name="paid", limit=10))

with Dag("payment"):
    pay()

@trigger(wait_for=EventNamed("paid"))
async def audit(event: Event[Json]) -> Event | None:
    await api.apost_event("audited", key=event.key)
    return None

@task
def record(order: str) -> None:
    print("audited", order)

with Dag("audit", trigger=audit()):
    record("order-123")

The event records task:<dag>/<run>/<task> as posted_by (a mapped task includes its index), or trigger:<dag> for a trigger. Use an idempotency_key= stable across attempts when a task may be retried. The agent’s own resend after a reconnect is one event anyway. Secrets the task has read are masked in what it posts, as in its output, and its log says so (once per process; schedr task-ctx says it on stderr each time). Keep secrets out of event keys: a DAG deduplicates two keys differing only in a secret as one key. api.ApiError has a .status: 403 means the route is not open to tasks. A 503 means no answer arrived from the master within a minute, or the agent stopped before an answer: the call may still have been made, so send it again with the key it was made with, error.body["idempotency_key"] (yours, or one the agent gave a call without). Tasks can use GET and POST on /api/v1/events, and POST /api/v1/notifications for notifications.

A command task can use the same API:

schedr task-ctx events post paid --key order-123 --data '{"amount":42}' --idempotency-key pay-order-123
schedr task-ctx events list --name paid
schedr task-ctx api GET '/api/v1/events?name=paid'

A Python script run by a command uses schedr.api as above. Outside schedr, use api.Client("https://schedr.example", token="..."), or set SCHEDR_SERVER and SCHEDR_TOKEN and use api.Client(). HTTP needs the schedr[http] extra. Under schedr dags test (and run_dag) a task posts to the test’s own local schedr, whose database goes with it; event triggers stay paused there, so an event starts no other DAG. Chained DAGs are unpaused for operator calls. For a unit test of one task use task_context().

Starting a run

Dag("ship_paid", trigger=EventNamed("paid")) starts one run per event key posted while the DAG is live. A key already seen by that DAG starts no second run. The run is planned for the event’s at, and its tasks can read dag_ctx.event.key, .data and .at. Events posted while the DAG is paused or absent are not caught up; a held DAG waits until the hold clears. Queued event runs count against [triggers] max_queued_events.

Waiting in a run

wait_for(EventNamed("packed", key=...)) is a step the master ends without a worker or slot. It takes the first matching event posted since the run was created, including one posted while the run was queued or before the step began waiting. Every run waiting for a matching event can receive it. Without key=, any key matches.

The key may be a literal string, a run parameter, dag_ctx.run.id, dag_ctx.dag_id, dag_ctx.event.key, or an upstream task’s output. A reference must resolve to a valid event key; a missing or invalid value fails the step. The event is an Event[Json], with key, data and at, read through .on(Event[Json]) as above. If no matching event is posted within max_wait, the step ends with TimedOut; without max_wait, the master’s [sensors] default_max_wait applies.

Reading an event in a sensor

A sensor can inspect a matching event before deciding whether to finish. Define it inside with Dag(...) when its key depends on the run: dag_ctx and the run’s parameters are references there, as are upstream outputs.

examples/docs/named_event_sensor.py
"""Wait for a packed event for the order that started this run."""

from datetime import timedelta

from schedr import Dag, Event, EventNamed, TimedOut, dag_ctx, sensor, task
from schedr.sensors import NOT_YET, NotYet
from schedr.types import Json


@task
def report(value: str) -> str:
    """Report the package."""
    return value


@task
def late(outcome: TimedOut) -> str:
    """Report a timeout."""
    del outcome
    return "packing timed out"


with Dag(
    "inspect_packing",
    description="For each paid order, waits until its package is packed.",
    trigger=EventNamed("paid"),
):

    @sensor(wait_for=EventNamed("packed", key=dag_ctx.event.key), max_wait=timedelta(seconds=3))
    async def packed(event: Event[Json]) -> str | NotYet:
        """Wait for a packed event with data."""
        if event.data is None:
            return NOT_YET
        return f"packed {event.key}: {event.data}"

    result = packed()
    report(result.on(str))
    late(result.on(TimedOut))

The master gives the first matching event posted since the run was created and no later than its max_wait to the function’s first parameter. NOT_YET means “not this event”: it waits for a later match until max_wait. A retry gets the same event. An event whose time was taken before the one it said NOT_YET to but stored after it is still given, if it is no more than a minute older (SCHEDR_HIDDEN__MASTER__EVENT_LOOKBACK); none is given twice. Without key=, any key matches. As with the node, every waiting run can receive the same event.

Deciding whether an event starts a run

A trigger can inspect each posted event before starting a run:

examples/docs/named_event_trigger.py
"""Inspect paid events before starting a run."""

from schedr import Dag, Event, EventNamed, dag_ctx, task, trigger
from schedr.types import Json


@trigger(wait_for=EventNamed("paid"))
async def paid_with_data(event: Event[Json]) -> Event[Json] | None:
    """Start a run only when the payment has data."""
    if event.data is None:
        return None
    return Event(data=event.data)


@task
def handle(key: str) -> str:
    """Handle the payment."""
    return f"handling {key}"


with Dag(
    "inspect_payment",
    description="Starts a run for each paid event whose data passes a check.",
    trigger=paid_with_data(),
):
    handle(dag_ctx.event.key)

@trigger(wait_for=EventNamed("paid")) takes no key=: it is called for each event of that name posted while the DAG is live, with the Event as its first parameter. Each event is passed once. The returned Event starts a run, with the input event’s key and at unless the return gives its own; None starts none. A repeated key still starts no second run. A failed call is retried with the same event, subject to run_retries= and the trigger’s other call rules.