Skip to content

Loops

How a cycle between cases is found before it ships, how to allow one on purpose, and how one is stopped if it happens anyway.

Applies to EngineCloud
View as Markdown
On this page
  1. At design time: the check refuses cycles
  2. A loop on purpose
  3. At run time: the chain is cut

Cases that start on each other’s outcomes can form a circle: A starts B, B starts C, C starts A again. Sometimes that is what you want (send a reminder until the bill is paid, at most three times). Most of the time it is a mistake that would run forever. Sypnia Cases finds circles when you design, and stops them when they run.

At design time: the check refuses cycles

The check builds the event graph from every starts: { event: … } of the map and looks for cycles (it finds the strongly connected parts of the graph). A cycle is an error:

Text
✗ loop   deliveries.pause-deliveries → deliveries.resume-deliveries → deliveries.pause-deliveries
         say how it stops: loop: { bounded: true, max, until } on one case of the cycle

In La Espiga, if «Resume deliveries» ended in an outcome that «Pause deliveries» listened to, the check would ask how the loop stops before it ships.

Two rules have no exception:

  • A case never listens to its own outcome (self-subscription).
  • A cycle must say how it stops. It is allowed only when one case in it declares a bounded loop.

A loop on purpose

When the baker does not get the WhatsApp message, La Espiga tries another channel, and then the message again. That is a cycle of two cases:

YAML
# cases/deliveries/tell-the-baker.case.yaml
starts:
  event:
    on: [deliveries.pause-deliveries.DeliveriesPaused.v1, deliveries.choose-another-channel.ChannelChosen.v1]
loop: { bounded: true, max: 3, until: "the baker got the message, or three channels were tried" }
outcomes:
  BakerTold: { channel: one_of(whatsapp, sms, call) }
failures:
  NotDelivered: { when: the channel did not deliver, payload: { channel: string } }

# cases/deliveries/choose-another-channel.case.yaml
starts: { event: deliveries.tell-the-baker.NotDelivered.v1 }
outcomes:
  ChannelChosen: { channel: one_of(sms, call) }
FieldWhat it means
boundedtrue: this cycle is on purpose
maxhow many times the case may run in one chain, from 1 to 10
untilthe condition that ends it, in plain words, for the people who approve the map. The cases make it true: here, BakerTold ends the chain because nothing listens to it inside the cycle

One case of the cycle declares the bounded loop, and that is enough for the whole cycle. A bounded loop is a change to the map like any other: someone proposes it and a person approves it.

At run time: the chain is cut

Every event carries its cause, the chain of cases that led to it, and its depth. Before starting a case, the dispatcher looks at the chain:

  • if the case is already in it (more times than its bounded loop allows), or
  • if the new event would be more than 10 events deep,

the case is not started. The delivery ends as LoopStopped, a runtime outcome that is never retried, and Telemetry opens a problem with the whole chain and sends an alert to the people who watch the project.

Text
LoopStopped   deliveries.pause-deliveries   depth 11
chain         weather.check-weather → deliveries.pause-deliveries → … → deliveries.pause-deliveries

This guard exists for what the check could not see: a cycle through another app (your case calls Rappi, Rappi calls your webhook), or a map changed by hand without the check. The limit of 10 is the same for every project.

  1. At design time: the check refuses cycles
  2. A loop on purpose
  3. At run time: the chain is cut

    Search pages, sections and terms