---
title: Start and resume runs
description: "Start runs on a schedule or from another system, and resume a waiting run from an email reply."
---

Start runs on a schedule or from another system, and resume a waiting run from an email reply.

## Triggers

A trigger starts runs of one workflow on a schedule, from a webhook, for
each new message in a Gmail label, or for each new object under an S3
prefix. It belongs to the organization, like a
connection, and is managed with `andon triggers`; workflow code declares
nothing about what starts it.

- A trigger names its workflow by key and runs whichever deployment is active
  when it fires. Activating a deployment starts no schedule and stops none.
  Set `key=` on a workflow that has triggers, so renaming the function keeps
  them.

### Scheduled runs

- `--cron` is five fields (minute, hour, day of month, month, day of week),
  read in `--timezone`, an IANA name that defaults to `UTC`. When the clocks
  change, a set time of day that is skipped fires once after the jump and one
  that repeats fires once; a schedule that fires every hour or more often
  keeps its interval.
- `next_run_at` is the next scheduled time, when the next run starts.
- Each scheduled time starts its own run with the trigger's saved input,
  whether or not the previous run has finished. After an outage a trigger
  starts one run, not one per missed time.
- A scheduled time that cannot start a run (the workflow is not in the
  active deployment, the saved input no longer fits it, or the organization is
  out of credits) is reported as the trigger's `last_error`, and the next
  scheduled time tries again.
- `andon triggers run <trigger_id>` starts a run now with the saved input and
  leaves the schedule unchanged.
- `andon deployment-triggers show <deployment_id>` lists each enabled trigger
  with why it would start no run, or answer no wait, with that deployment
  active (`error`: the deployment lacks its workflow, rejects a schedule's
  saved input, or has no event wait a Gmail trigger can answer), or `null`
  when it would work on it. Before activating, read it for the
  tested deployment and for the active one: a trigger with an `error` on
  only the tested one is what activating stops.

### Webhook deliveries

`andon triggers create <workflow_key> --webhook` makes a webhook trigger
instead, which starts a run for each event another system delivers:

- The trigger's `url` takes `POST` requests whose JSON body is the run input,
  with `Authorization: Bearer <secret>` and an `Idempotency-Key` header
  that is the same each time one event is delivered. A repeated key with the
  same body, in any key order, returns the same run, so the sender can retry
  safely.
- The answer is `202` with the `run_id` once the run is queued. `400` means
  the delivery cannot succeed as sent while the same deployment is active:
  the key is missing, or the body does not fit the workflow's input or names
  a file the run cannot read. `401` means the secret is wrong. `409`
  carries a `code`: `idempotency_key_reused` when the key was used with a
  different body, and otherwise a reason the sender may retry after
  (`trigger_disabled`, `no_active_deployment`, `workflow_not_found`,
  `insufficient_credits`).
- An admin issues the secret in the console, at the trigger's
  `console_url`, where it is shown once. No command returns it. Issuing
  again replaces it at once, and deleting the trigger revokes it. Until one
  is issued the trigger accepts no delivery.
- A webhook trigger has no saved input, schedule or `triggers run`; only
  `--enable` and `--disable` change it.

### New Gmail messages

`andon triggers create <workflow_key> --gmail <connection> [--label <name>]`
makes a Gmail trigger, which starts one run for each message that enters the
label (`INBOX` when none is named) of the connection's mailbox:

- Each run's input is `{"message_id": "<Gmail message id>"}`. The workflow's
  input model has that one field, and a step granted the same connection
  reads the message and its attachments:

  ```python
  AP_INBOX = ConnectionRef(Gmail, "ap-inbox")


  class NewEmail(BaseModel):
      message_id: str


  @step(connections=[AP_INBOX])
  async def read_invoice(ctx: StepContext, email: NewEmail) -> bytes:
      gmail = await ctx.connect(AP_INBOX)
      message = await gmail.get_message(email.message_id)
      pdf = next(a for a in message.attachments if a.filename.endswith(".pdf"))
      return await gmail.get_attachment(message.id, pdf.id)
  ```

- The trigger is refused unless the workflow in the active deployment takes
  that input and a step it runs, itself or through a child workflow, is
  granted the Gmail connection by the same name, so the run reads the
  mailbox the trigger watched.
- `andon triggers preview <workflow_key> --gmail <connection> [--label
  <name>]` saves nothing and shows the mailbox, its labels, and the newest
  message in the label with the input its run would get. Run it first.
- Watching starts when the trigger is saved. Mail already in the label
  starts no run. A message enters the label when it is delivered there or
  when the label is added to it later, and starts one run per trigger, ever.
  A draft starts no run.
- The run holds only the id. Read the message in the first step: one
  archived, unlabelled or moved to Trash still reads, and one deleted for
  good fails the step with Gmail's not-found error.
- A run starts for mail that is in the label when the trigger checks, about
  once a minute. A message archived, unlabelled or deleted for good before
  that check starts no run.
- Activate a version that changes which mailbox the workflow reads only
  after disabling its trigger: runs a check is starting at that moment
  start on the new version and fail at the step that reads the message.
  Disabling drops any held mail, and mail that arrives while the trigger is
  disabled starts no run, so do it when the mailbox is quiet or start runs
  for those messages by hand afterwards.
- Disabling stops the watch; enabling watches from then, so mail received
  while disabled starts no run. After an outage or lost access the trigger
  starts a run for each message that entered the label meanwhile and is
  still in it, oldest first.
- A message whose run cannot start (no credits, the workflow is not in the
  active deployment or no longer fits) holds the trigger there: `last_error`
  says why, and it continues by itself once the cause clears. Disabling and
  enabling the trigger drops the held mail.
- A trigger that could not check for about a week disables itself, because
  Gmail keeps a mailbox's changes only that long: `last_error` says so, and
  mail that entered the label in that time starts no run. Enabling it
  watches from then. To process a message from the gap, start a run with
  `{"message_id": "<id>"}` as its input. A trigger whose label or connection
  was deleted disables itself too; delete it and add another.
- A Gmail trigger has no saved input, schedule or `triggers run`; only
  `--enable` and `--disable` change it.

### Resume a wait from a Gmail reply

`andon triggers create <workflow_key> --gmail <connection> --resume <wait>
--match thread_id=<metadata field>` makes a Gmail trigger that starts no run:
it answers the workflow's runs that wait on `<wait>`, each with the replies
in its own email thread. A run sends its request, keeps the thread id
`Gmail.send` returns, and waits with it in its metadata:

```python
@dataclass
class AwaitingDocument:
    thread_id: str


class NewEmails(BaseModel):
    message_ids: list[str]


@step(connections=[CLIENTS])
async def request_documents(ctx: StepContext, client: Client) -> str:
    gmail = await ctx.connect(CLIENTS)
    sent = await gmail.send(to=[client.email], subject="Your W-2", body="...")
    return sent.thread_id


@workflow(key="collect_w2")
def collect_w2(client: Client) -> Filed:
    thread_id = request_documents(client)
    reply = wait_for_event(
        NewEmails,
        label="client_reply",
        metadata=AwaitingDocument(thread_id=thread_id),
        timeout="3d",
    )
    return branch(reply.timed_out, true=overdue(client), false=file_documents(reply.value))
```

- Each check, about once a minute, reads the label for the replies received
  since the earliest start among the runs with an open `<wait>`, and answers
  each such wait whose metadata field equals a reply's thread id with
  `{"message_ids": [...]}`. A reply was received after its run started, was
  not sent from the watched mailbox and is not a draft. Replies that arrive
  together answer one wait, so the response type takes several ids.
- A trigger never gives a run a message twice: a reply that arrived while it
  had no wait open answers its next wait with this label, and a message a
  trigger gave an earlier wait, accepted or refused, is passed over. Wait again in a
  `loop` until the documents are all there; the platform judges nothing
  about a reply.
- Filter mail in Gmail: a Gmail filter that labels the replies that matter
  (`has:attachment`, a sender) and a trigger on that label keep other mail
  from waking the run.
- The trigger is refused unless the active deployment's workflow has an
  event wait with that label, its metadata has the field as text, its
  response type takes `{"message_ids": [...]}` with one id or many, and a
  step it runs is granted the connection. A review is answered by a person,
  never a trigger.
- An answer names the trigger as its responder: the event's `trigger_id`
  is set and `responded_by` is `runtime`. A wait whose answer its response
  type rejects needs correction and takes its next answer by hand. A wait
  whose saved response type does not take the answer, such as more replies
  than a capped list allows or a wait of an earlier build, gets no answer;
  the trigger's `last_error` says so. A reply Gmail puts in another thread,
  such as a new email, is answered by hand through the signals API.
- Disabling stops the answers, and once a pause or delete returns no answer
  follows; enabling delivers the replies that arrived meanwhile. A deleted
  trigger's waits stay open until answered by hand or timed out.

### New S3 objects

`andon triggers create <workflow_key> --s3 <connection> --prefix <prefix>`
makes an S3 trigger, which starts one run for each new object under a prefix
of the connection's bucket:

- Each run's input is `{"file": "<FileRef>", "key": "<object key>"}`. The
  platform copies the object into the organization's uploads before the run
  starts, and `file` is that copy, pinned to the run like any `FileRef`
  input. The workflow reads the copy, never the bucket, and needs no S3
  connection:

  ```python
  class NewFile(BaseModel):
      file: FileRef
      key: str


  @step
  async def read_invoice(ctx: StepContext, new: NewFile) -> ParsedDocument:
      return await ctx.parse_document(new.file)
  ```

- The trigger is refused unless the workflow in the active deployment takes
  that input with `file` declared a `FileRef`.
- The prefix ends in `/`, such as `incoming/`, and every object under it at
  any depth starts a run; there is no filter by name, so a workflow skips a
  file by its `key`. A key ending in `/` (a folder marker) starts none. Do
  not write a workflow's output under the prefix its trigger watches.
- `andon triggers preview <workflow_key> --s3 <connection> --prefix
  <prefix>` saves nothing and shows the bucket, how many objects are under
  the prefix now, and the newest with the input its run would get. Run it
  first.
- Watching starts when the trigger is saved. Objects already under the
  prefix start no run, then or later. An object is new when its key was not
  there at the save and has started no run of this trigger: a key starts one
  run, ever, also when it is overwritten or removed and put again. Producers
  write each file under its own key.
- The trigger checks about once a minute and lists the whole prefix each
  time, so a prefix holds at most 10,000 objects. Past that the trigger
  holds, with `last_error` saying so, until objects are removed. An object
  can be removed once its run exists; a lifecycle rule on the prefix keeps
  it small.
- An object removed before the check that would copy it starts no run. One
  larger than 100 MB starts none either: `last_error` names it while it
  stays under the prefix, and the other objects keep starting runs.
- Disabling stops the watch; enabling watches from then, so objects put
  while disabled start no run. After an outage or lost access the trigger
  starts a run for each object put meanwhile that is still there, oldest
  first, however long it lasted.
- An object whose run cannot start (no credits, the workflow is not in the
  active deployment or no longer takes a file) holds the trigger:
  `last_error` says why, and it tries again every 15 minutes. So does an
  object that cannot be read, such as one archived until it is restored,
  tried again at the next check.
- A trigger whose connection was deleted, or now points to another bucket,
  disables itself; delete it and add another.
- An S3 trigger has no saved input, schedule or `triggers run`; only
  `--enable` and `--disable` change it.

### Enable a trigger

Creating or enabling a trigger starts unattended runs that spend credits.
Before you do either, or change an enabled trigger's schedule or input, show
the person the workflow and its schedule, timezone and input, that it is a
webhook, the mailbox and label it watches, or the bucket and prefix it
watches, and get their go-ahead. Only admins change triggers.
