Skip to content

Latest commit

 

History

History
197 lines (173 loc) · 9.72 KB

workflow-events.md

File metadata and controls

197 lines (173 loc) · 9.72 KB

Workflows started by events, and concurrency

A Workflow run can start without anyone calling workflow_<name>: a Workflow in an organization's namespace subscribes to MCP events with subscriptions, and each new occurrence starts a run, acting as the organization's principal. A run can wait for an event, and be cancelled by one. concurrency limits how many runs of a Workflow run at once, whatever started them. All of it runs on the same engine and run store as every other run (see Workflows).

Workflows of organizations

muster reads Workflows in its own namespace and, in Kubernetes mode, in every namespace that has an organization principal (organizationPrincipals in the OAuth server's configuration). A Workflow of an organization's namespace:

  • is seen by whoever sees the MCPServers of that namespace: the principal of the organization, and a person whom Kubernetes RBAC lets list mcpservers there (when muster serves the MCPServers of every namespace). core_workflow_list shows its namespace; core_workflow_get takes one.
  • has its runs in that namespace, seen by the same callers in core_workflow_run_list, _get, _cancel, _retry and _signal.
  • starts children of its namespace's Workflows with workflow: steps.
  • is no workflow_<name> tool: the core catalogue serves the Workflows of muster's namespace. An organization's Workflows are started by their subscriptions and by the runs of their namespace.

subscriptions are valid only in such a namespace. Elsewhere -- muster's own namespace, or any Workflow in filesystem mode -- the Workflow's status says subscriptions are valid only in a namespace with an organization principal, which muster subscribes and runs as, and it subscribes to nothing. core_workflow_create, _update and _validate, which are for muster's namespace, refuse them.

subscriptions

apiVersion: muster.giantswarm.io/v1alpha1
kind: Workflow
metadata: { name: sdlc, namespace: org-acme }
spec:
  args:
    item: { type: string, required: true }
  subscriptions:
    - event: core_tool_changed          # as events/list names it for the principal
      arguments: { tool: x_org-acme_pro_list_items, items: ".items", key: ".id", version: ".status" }
      when: '{{ eq .event.data.item.status "Up Next" }}'   # optional filter
      subject: '{{ .event.data.key }}'  # one open run per (workflow, subject)
      args: { item: '{{ .event.data.item.url }}' }
  steps: [...]
  • event and arguments name an event type as events/list shows it to the namespace's principal: a server's (x_<prefix>_<event>, for a server of the namespace the principal holds a server:<name> grant for) or one of muster's own (core_schedule, core_tool_changed, core_workflow_run). See MCP events.
  • when, subject and args are templates over .event: eventId, name, timestamp and data. Every occurrence starts a run without when.
  • An occurrence whose subject an open run of the Workflow has is delivered to that run, as a delivered run event, and starts none: one open run per subject (an open dry run counts for dry runs only, a live one for live runs). Without a subject, every occurrence starts a run.
  • A run started by an occurrence is queued with args as its input and the subject, acts as the namespace's principal, and gets the occurrence as its first delivered run event.

How muster subscribes

A subscription is a row of the run store, shared by everyone who names the same event type with the same arguments in the same namespace as the same principal: one per key, however many Workflows and waiting runs use it. The subscriptions of a dry Workflow subscribe apart from a live one's; waits and cancelWhen, which only read, share either.

  • Each replica reads the Workflows' subscriptions at start, whenever a Workflow changes, and every 30 s, and makes the subscriptions they need.

  • A replica claims a due subscription with a row lease, as it claims a due run, and polls it with events/poll as its principal, through the same path a client's poll takes: muster's own event types and the servers', with the principal's visibility. No push (events/stream) and no webhooks.

  • The server paces the polls: its nextPollMs, kept within [1 s, 10 min], 30 s without one, give or take a tenth. A poll with hasMore polls again at once. The first poll of a subscription asks for the occurrences since it was made (maxAgeMs), so a server that replays gives the ones between the Workflow's change and the first poll.

  • Each occurrence is recorded once per subscription (by eventId) in the same transaction as the cursor that moves past it, with the runs it starts and the deliveries it makes. A repeated occurrence, or a replica that crashed after the poll, starts nothing twice; a restart goes on from the cursor.

  • The subscription's condition says how well it is observed:

    • observing: its last poll succeeded;
    • unobserved (since, reason): its polls fail -- the principal may not see the event type, the server is down. Polls back off from 5 s, doubling, up to 10 min;
    • gap (since): a poll came back truncated: the server lost occurrences since the cursor. Polling goes on from where the server resumed, and muster_workflow_subscription_gaps_total counts it.

    core_workflow_get shows the subscription of each entry of subscriptions: its condition, since when, why, its last success and its next poll.

  • A subscription no Workflow uses, and that no open run waits on, is dropped after an hour.

  • Going live. A dry Workflow's subscriptions start dry runs. Removing its spec.dryRun makes its entries subscribe afresh, with cursors of their own: a source that fires for what is there already -- core_tool_changed with includeExisting -- fires again for every item and starts live runs for them, though dry runs of the same subjects may still be open. Those stay dry; the dry subscription, which no Workflow uses any more, is dropped after an hour.

Without Postgres (the memory store), every replica keeps its own subscriptions and polls them: a muster with more than one replica needs Postgres, or each replica starts the runs of every occurrence.

concurrency

spec:
  concurrency:
    - { key: '{{ .input.owner }}', max: 2 }
    - { key: '{{ .input.repo }}', max: 1 }
  • Each key renders from a run's input (with its defaults) when the run is created, whoever starts it: a call, an event, a parent's workflow: step. A key that renders empty does not limit. A key that does not render (a missing field: use {{ get .input "owner" }} for an optional one) fails the run.

  • At most max runs of the Workflow (in its namespace) that share a key run at once: a run counts from its admission until it ends, waiting included.

  • A run a limit holds is created queued, with the reason (concurrency alice 2/2) as its held, in its waiting.for and in a held run event. No replica claims it.

  • When an admitted run ends, the held runs that share one of its keys are admitted, oldest first, each whose limits all have room. An admitted run is due at once.

  • A workflow_<name> call whose run is held answers at once, like a call whose run waits:

    {"run_id": "0b5c...", "status": "queued", "waiting": {"for": "concurrency alice 2/2"}}

    A task-augmented call's task is working, its status message the reason.

  • A failed run retried is admitted again, or held.

  • Dry runs count against the limits as live ones do: admission is muster's own write, which a dry run makes for real.

Postgres serializes the admission of one Workflow's runs with a transaction lock, counts the admitted runs of a key through an index over the runs' keys, and finds held runs by an index of their own: admitting costs the runs that share a key, not every run.

Waits and cancelWhen on events

steps:
  - id: ci
    wait:
      event: { name: x_github_check_suite_completed, arguments: { pr: '{{ .input.pr }}' } }
      until: '{{ eq .event.data.conclusion "success" }}'
      timeout: 72h
cancelWhen:
  event: { name: x_github_issue_closed, arguments: { issue: '{{ .input.issue }}' } }
  until: 'true'
  • A wait on an event subscribes to it, as whoever started the run, with arguments rendered from the run's state when the wait begins. It is over when an occurrence delivered since it began holds until, which sees it as .event (any occurrence without until); its result is {satisfied, timedOut, checks, since, event}, event the occurrence that ended it. It times out as any wait. It has no steps, every or signal.
  • cancelWhen with an event subscribes when the run is created, with arguments rendered from its input, and checks until for each occurrence delivered since. It has no steps or every.
  • Each occurrence of a subscription is appended as a delivered run event to every open run subscribed to it, and wakes those that wait; a run reads them in order, as it reads its signals.
  • core_workflow_run_get shows a wait on an event as waiting: {step, for, event, deadline}.

Metrics

Metric Labels Counts
muster_workflow_subscriptions (gauge) condition subscriptions, as last counted
muster_workflow_subscription_occurrences_total event new occurrences the subscriptions received
muster_workflow_subscription_gaps_total event polls that came back truncated
muster_workflow_runs_started_by_events_total workflow runs occurrences started
muster_workflow_run_deliveries_total event occurrences delivered to open runs
muster_workflow_runs_held_total workflow runs a concurrency limit held when they were created