Flows and dashboards are created by POST under an issued or superuser-pinned id; PUT only updates; deleted flows leave a tombstone with their key; the controller stores by id and runs by key, hooks live under /hooks/<flow id>; a key with state or queued work behind it is refused until cleared

This commit is contained in:
2026-11-02 17:41:23 +01:00
parent fae85f5ecb
commit 08a6376836
63 changed files with 2832 additions and 936 deletions
+103 -1
View File
@@ -151,6 +151,17 @@ class WorkItem:
)
def owned_by(item: WorkItem, flow: str) -> bool:
"""Whether *item* is work of the flow with key *flow*, not one reaching it.
A run item names its run's flow by identity rather than by key, and a
run is never removed with a flow's queued work, so it is nobody's here.
"""
return item.kind != "run" and (
item.flow == flow or item.node.split(".", 1)[0] == flow
)
class WorkQueue(ABC):
"""What the execution service pulls from."""
@@ -236,7 +247,21 @@ class WorkQueue(ABC):
@abstractmethod
def clear_flow(self, flow: str) -> None:
"""Forget anything held for a flow that no longer exists."""
"""Drop the work of a flow that no longer exists (`owned_by`).
What it parked, what waits on a timer, and what is queued or claimed
and not yet acknowledged, so a new flow under the same key never runs
it. A cascade already running finishes; its acknowledgement then has
nothing left to remove.
"""
@abstractmethod
def live_items(self) -> list[WorkItem]:
"""Every item still to run: queued, claimed, delayed or parked.
Not what was acknowledged, dead-lettered or marked done, which is
history. Reads the whole queue, so it is for rare questions.
"""
@abstractmethod
def stats(self) -> dict[str, Any]:
@@ -388,6 +413,23 @@ class MemoryWorkQueue(WorkQueue):
def clear_flow(self, flow: str) -> None:
with self._lock:
self._parked.pop(flow, None)
for lane in (self._items, self._due):
kept = [item for item in lane if not owned_by(item, flow)]
lane.clear()
lane.extend(kept)
self._delayed = [e for e in self._delayed if not owned_by(e[2], flow)]
heapq.heapify(self._delayed)
def live_items(self) -> list[WorkItem]:
"""What is waiting. A claimed item is gone from here once it is handed
out, so unlike Redis this cannot say what is running."""
with self._lock:
return [
*self._items,
*self._due,
*(entry[2] for entry in self._delayed),
*(item for held in self._parked.values() for item in held),
]
def backlog(self) -> int:
with self._lock:
@@ -683,8 +725,68 @@ class RedisWorkQueue(WorkQueue):
pipe = self._redis.pipeline()
pipe.delete(self._parked_key(flow))
pipe.srem(self._parked_flows_key, flow)
# Timers before the streams: one promoted in between is in the due
# stream by the time that is read.
for raw in cast(list[str], self._redis.zrange(self._delayed_key, 0, -1)):
if owned_by(WorkItem.from_fields(orjson.loads(raw), ""), flow):
pipe.zrem(self._delayed_key, raw)
for stream in (self._stream, self._due_stream):
ids = [
entry_id
for entry_id, fields in self._live_entries(stream)
if owned_by(WorkItem.from_fields(fields, entry_id), flow)
]
if ids:
pipe.xack(stream, GROUP, *ids)
pipe.xdel(stream, *ids)
pipe.execute()
def _live_entries(self, stream: str) -> list[tuple[str, dict[str, str]]]:
"""The entries of *stream* still to run: claimed and unacknowledged,
then never handed out. Acknowledged ones stay in a stream until it is
trimmed, and are history."""
group = self._group_info(stream)
if not group:
return []
last = str(group.get("last-delivered-id") or "0-0")
live: list[tuple[str, dict[str, str]]] = []
pending = int(group.get("pending") or 0)
if pending:
rows = cast(
list[dict[str, Any]],
self._redis.xpending_range(
stream, GROUP, min="-", max="+", count=pending
),
)
ids = {row["message_id"] for row in rows}
if ids:
# In id order, so the first is the oldest still held.
entries = self._redis.xrange(
stream, min=rows[0]["message_id"], max=last
)
live += [(i, f) for i, f in cast(Any, entries) if i in ids]
live += cast(Any, self._redis.xrange(stream, min=f"({last}", max="+"))
return live
def live_items(self) -> list[WorkItem]:
items = [
WorkItem.from_fields(fields, prefix + entry_id)
for stream, prefix in ((self._due_stream, DUE_PREFIX), (self._stream, ""))
for entry_id, fields in self._live_entries(stream)
]
items += [
WorkItem.from_fields(orjson.loads(raw), "")
for raw in cast(list[str], self._redis.zrange(self._delayed_key, 0, -1))
]
for flow in cast(set[str], self._redis.smembers(self._parked_flows_key)):
items += [
WorkItem.from_fields(orjson.loads(raw), "")
for raw in cast(
list[str], self._redis.lrange(self._parked_key(flow), 0, -1)
)
]
return items
def _group_info(self, stream: str = "") -> dict[str, Any]:
"""This consumer group's row of ``XINFO GROUPS``, or an empty one."""
try: