dizzy.engine.loop¶
dizzy.engine.loop
¶
The control loop — a generic two-queue dispatcher for a DIZZY feature.
This is the engine the scheduling shells drive. It owns the event store: every
emitted event is appended FIRST (getting its envelope, whose ingested_at is
the stream-append timestamp), and only then folded by projections and handed to
policies.
- COMMAND queue (external, the shell's) — policies dispatch commands onto it; the shell drains it, one command per unit of work.
- EVENT queue (local, this unit) — drained inside :meth:
Engine.run_command: each event is appended, folded by its projections (the data loop), then handed to its policies (the reactivity loop), which dispatch further commands.
The two loops, and where the second one lives. run_command runs the
procedure, whose emits land in the local FIFO event queue; _drain_events
drains that queue to empty. A policy's dispatched command never runs inline —
it goes to the OUTER command queue and becomes the next unit of work. So the
defined "drain the event queue, then drain the command queue, repeat" semantics
are real here, but only the first phase is owned by this class: the shell is
part of the semantics. That matters to anything modelling them, and it is why
the two shells can claim different guarantees while sharing this engine
verbatim — st (one lane) is sequentially consistent, mp (N workers
under at-least-once delivery) deliberately is not.
Ordering rule. Projections fold and the read model commits BEFORE policies dispatch. The commit is what makes fold-then-enqueue real across processes: a policy-dispatched command may be claimed by another worker within milliseconds, and that worker sees only committed state. The engine owns this boundary; projections never commit.
What keeps it feature-agnostic. Handlers are keyed by the runtime type of the command/event — the generated pydantic class — so dispatch is a dict lookup and no name appears in this file. Registration mirrors the feat topology, and is the wiring's job to perform.
Telemetry (host-level observation, never events): given a bus callable,
the engine emits a start/end record per element execution and one per event
appended. Elements know nothing about it; this is the engine observing itself.
Tracing goes through the injected OTel provider, which defaults to the no-op —
an engine must run with tracing switched off.
Engine
¶
One process's control loop over a registered topology.
Source code in dizzy/src/dizzy/engine/loop.py
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 | |
command_queue = command_queue
instance-attribute
¶
External (shell-owned) command queue — where dispatches go.
store = store
instance-attribute
¶
Append-only event store: the truth.
commit = commit
instance-attribute
¶
Called once per event, after its projections fold and before its
policies dispatch. None means the app has no read-model
transaction to close (see the ordering rule above).
current_event = None
instance-attribute
¶
The event whose policies are currently running, for wiring that needs to correlate a dispatch back to its cause.
projection_runners()
¶
The event class -> [(name, runner)] map, as registered.
This is exactly what :func:dizzy.engine.rebuild.rebuild and
:func:dizzy.engine.replicate.fold_envelopes take, so a host that has
already wired an engine does not wire the data loop a second time to
refold or to replicate — one registration, three triggers.
Source code in dizzy/src/dizzy/engine/loop.py
110 111 112 113 114 115 116 117 118 | |
emit_event(event)
¶
A procedure emitted an event -> onto the local event queue.
Source code in dizzy/src/dizzy/engine/loop.py
122 123 124 | |
dispatch_command(command)
¶
A policy dispatched a command -> onto the external queue.
Source code in dizzy/src/dizzy/engine/loop.py
126 127 128 | |
discard_pending_events()
¶
Drop events emitted but not yet drained.
A shell calls this after a failed command so un-appended events cannot leak into the NEXT command on a long-lived process.
Source code in dizzy/src/dizzy/engine/loop.py
130 131 132 133 134 135 136 | |
run_command(command)
¶
Run one command to quiescence: its procedure, then every event that cascades from it. Returns when the local event queue is empty; any command a policy dispatched is by then on the external queue.
Source code in dizzy/src/dizzy/engine/loop.py
211 212 213 214 215 216 217 218 219 220 221 222 223 | |