flow
A small workflow engine: directed graphs of tasks, run state, and a scheduler that decides what may run next.
from flow import Task, TaskGraph, ResourcePool, Scheduler
graph = TaskGraph([
Task("fetch"),
Task("build", depends_on=frozenset({"fetch"})),
Task("test", depends_on=frozenset({"build"})),
])
scheduler = Scheduler(graph, ResourcePool({}))
state = scheduler.new_state()
for task_id in scheduler.next_batch(state):
scheduler.start(task_id, state)
scheduler.finish(task_id, state, result=True)
Layout
| module | role |
|---|---|
task.py |
Task, ResourceRequest — what a unit of work declares |
graph.py |
TaskGraph — dependency structure, traversal, topological order |
state.py |
RunState — per-task status, the legal transitions between them |
scheduler.py |
eligibility, batching, and the start/finish/fail transitions |
resources.py |
ResourcePool — named capacities held for the duration of a task |
retry.py |
RetryPolicy — attempt counting and deterministic backoff |
calendar.py |
Calendar, Window — when tasks are permitted to start |
expressions.py |
the condition mini-language used to gate tasks |
validation.py |
static checks run before a graph executes |
serialize.py |
lossless persistence of a run in progress |
metrics.py |
counters and an event timeline |
clock.py |
injectable time source, so runs are reproducible |
Design notes
Determinism. Anything that could vary between runs is pinned. Ties in priority are broken by task id, topological order sorts its ready set, and retry backoff carries no jitter. Two runs of the same graph make the same decisions in the same order.
The scheduler does not execute. It reports what is eligible; the caller runs the work and reports back. Admission rules are therefore testable without running anything.
Half-open intervals. A calendar window is [start, end) throughout, so
adjacent windows neither overlap nor leave a gap.
Failure propagates. A task that will never succeed skips everything downstream of it, because those tasks depend on a result that will not exist.