dura.workers¶
Defined in dura/workers.py. process_task, run_worker, and
run_workers are re-exported from the top-level dura package.
run_workers ¶
run_workers(engine: DurableEngine, *, handlers: dict, worker_count: int, heartbeat: Heartbeat | None = None, handle_signals: bool = True, lanes: list[tuple[int, int]] | None = None) -> None
Start the worker pool and block until SIGINT/SIGTERM, then drain.
On shutdown: stop claiming new work, let in-flight handlers finish (up to
SHUTDOWN_GRACE_SECONDS), and close every connection so the database is
left without -wal/-shm sidecars. Must run on the main thread when
handle_signals is True, since signal handlers install there only.
heartbeat (if given) is beaten by every worker each loop iteration so a
liveness probe can detect a fully wedged pool.
lanes is a list of (max_priority, count) pairs. Workers in a lane
only claim tasks at or below max_priority; the remaining workers are
unrestricted and claim from the full queue.
Source code in dura/workers.py
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 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | |
run_worker ¶
run_worker(engine: DurableEngine, *, handlers: dict[str, Callable], worker_id: str, stop_event: Event, heartbeat: Heartbeat | None = None, claim_timeout_secs: int = 120, poll_interval: float = 1.0, max_priority: int | None = None) -> None
Source code in dura/workers.py
process_task ¶
process_task(engine: DurableEngine, *, handlers: dict[str, Callable], task: ClaimedTask, worker_id: str = 'worker') -> None
Run one claimed task's handler and settle its run.
Outcomes: * handler returns normally -> the run is completed with its result * handler raises WorkflowSuspended -> the run parked itself awaiting an event; we leave it alone (neither completed nor failed) * handler raises TaskCancelledError -> the engine already marked it cancelled; nothing to do * no handler is registered for the task's name -> the run is failed without a retry (see below) * any other exception -> the run is failed (and retried per its strategy)
Settling (complete/fail) tolerates the run having been reclaimed and settled by another worker while this one was busy: that is logged, not raised, so a lease-expiry race never crashes the worker thread.
A missing handler is not treated like a handler-raised exception: retrying
it would just repeat the same KeyError forever (by default, instantly
and without limit), since no amount of retrying registers the handler. It
usually means the task was enqueued by different code than is now running
(e.g. a renamed task, a stale queue shared with another app), so the run is
failed outright, once, with a reason that says why.
Source code in dura/workers.py
Constants¶
SHUTDOWN_GRACE_SECONDS (float, default 10.0): how long run_workers
waits for in-flight handlers to finish on shutdown before abandoning them.