Worker¶
Worker process internals: dispatcher, consumer, heartbeat, leader election, shutdown.
taskq.worker itself is a lazy __getattr__ shim (re-exports selected names from its
submodules on first access) and renders no useful members on its own. The directives below
target the concrete submodules instead. See also the Workers guide
for a task-oriented walkthrough.
Process entry point, registration, producer/consumer loops¶
run ¶
Worker-runtime wiring helpers and process entry point.
This module forms the production seam between WorkerDeps assembly
(open_worker_deps, deps.py) and the heartbeat task spawn. It also hosts
the worker process entry point worker_main and the _main bootstrap
coroutine that wires the full TaskGroup of long-lived siblings.
Deviations from the original sketch
- Signal handlers go through
install_signal_handlers, not inline lambdas. orchestrate_shutdowntakes notgparameter.ProcessScope/ThreadScope/LoopScopeare bootstrapped insideopen_worker_depsrather than before it (M3 single-process deployment , process exit and deps exit are coterminal; see_maincomment block).- The
_local_queue_seedkeyword-only parameter is a test seam, not public API. local_queuemaxsize usesmax_concurrency(notbatch_size, nobatch_sizefield exists onWorkerSettings).
M1 stub consumers accept a stub_work_timeout keyword-only parameter
(default 60.0 s) that controls the sentinel sleep duration. Integration
tests may pass a shorter override (e.g. stub_work_timeout=2.0) when
seeding jobs that must complete naturally during a short test run. The
bootstrap accepts the default; M2 replaces the stubs with the
real producer/consumer.
__all__
module-attribute
¶
__all__ = [
"_emit_resolved_capacity_startup_lines",
"_main",
"consumer_loop_stub",
"deregister_worker",
"di_consumer_loop",
"producer_loop",
"producer_loop_stub",
"register_worker",
"run_worker_to_exit",
"worker_main",
"worker_main_async",
]
run_worker_to_exit ¶
Run a worker coroutine to completion, guarding SIGTERM to process exit.
The dedicated-worker-process entrypoint shape, for entries that drive
:func:_main (or another worker coroutine) directly instead of going
through :func:worker_main - they bypass setup_logging on purpose
and must keep doing so. Same guard, same reason: see
:func:_sigterm_teardown_guard. The caller's next act after this
returns should be sys.exit of the returned code.
Source code in src/taskq/worker/_bootstrap.py
worker_main ¶
worker_main(
settings: WorkerSettings,
*,
actor_registry: Mapping[str, ActorRef[Any, Any]]
| None = None,
di_registry: ProviderRegistry | None = None,
cron_registry: list[CronScheduleSpec] | None = None,
connections: WorkerConnections | None = None,
pg_credential_provider: PgCredentialProvider
| None = None,
rate_limit_registry: RateLimitRegistry | None = None,
until_idle: bool = False,
idle_settle_window: float | None = None,
idle_poll_interval: float | None = None,
idle_max_runtime: float | None = None,
ignore_sigterm_on_exit: bool = False,
) -> int
Worker process entry point (synchronous).
Thin wrapper: runs :func:worker_main_async under an asyncio.Runner
and returns its int result. Uses Runner (not asyncio.run) for
finer control over teardown.
Because this function owns the loop, an application that must build
loop-bound dependencies (asyncpg pools) before constructing its actors
cannot use it - await :func:worker_main_async from its own coroutine
instead. Both take exactly these parameters.
actor_registry is a mapping from short name to :class:ActorRef
containing every @actor-decorated handler this worker intends to
run. Forwarded to :func:_main for the bootstrap config sync.
pg_credential_provider is the resolved Postgres credential
provider for the worker-internal per-slot transaction pool (see
:func:_main). Pass the same provider used to build
connections, the slot pool does not read WorkerConnections.
None authenticates it from the direct DSN.
di_registry is an optional pre-configured :class:ProviderRegistry
containing application-specific provider registrations (database pools,
HTTP clients, etc.). When supplied, the worker uses it instead of
creating a fresh registry, callers must not call validate() before
passing it here; the worker calls validate() as part of its bootstrap
sequence. WorkerSettings and Clock are registered automatically
if not already present.
rate_limit_registry is an optional owned :class:RateLimitRegistry
for this worker (e.g. one instance per process in a multi-process
deployment). When None, resolution falls back to a
RateLimitRegistry value provider at Scope.LOOP in
di_registry, then to the module singleton, import-time
.register() on the singleton keeps working exactly as before.
Co-present with a RateLimitRegistry provider in di_registry
this raises TypeError (ambiguous, bootstrap and dispatch would
diverge); pass one or the other.
Forwarded to :func:_main.
cron_registry is an optional list of :class:CronScheduleSpec
objects to auto-register at startup. When None (the default),
get_registered_crons() is used instead, schedules declared via
the @cron decorator are auto-discovered. When an explicit list
is passed (even empty []), only those schedules are registered;
decorator-registered schedules are skipped. For each spec, a direct
INSERT into … cron_schedules is executed inside
try/except asyncpg.UniqueViolationError: pass, the DB (actor, name)
UNIQUE constraint prevents duplicates, so concurrent worker replicas
can safely race. Startup auto-discovery is create-only,
skip-on-conflict: existing cron_schedules rows are never
modified by the registration pass. If a @cron decorator's
parameters change after the schedule was first registered, the
operator must manually update or delete and recreate the schedule.
until_idle enables drain mode (see :func:_main for details).
idle_settle_window, idle_poll_interval, and
idle_max_runtime override the corresponding settings when
until_idle is True; they are ignored otherwise.
ignore_sigterm_on_exit selects the caller's contract. Left
False (the default, and the only correct value for a library
caller) the host's SIGTERM disposition and signal mask come back
exactly as they were found. A dedicated worker process entrypoint -
one whose next act after this returns is its own death, at most
flushing a profile or closing a caller-owned pool first - passes
True: SIGTERM is then masked across the loop's teardown, ignored
once the loop is closed, and unblocked, so a supervisor's redundant
stop signal cannot kill an already-drained process and erase the
drain's verdict with rc = -15. Never pass True from a caller
that keeps running or forks children afterwards: an ignored
disposition survives exec, so such a child would ignore its own
SIGTERM.
Returns the exit code from :func:_main, 0 on clean shutdown,
3 if any jobs failed in drain mode, 4 if idle_max_runtime was
exceeded.
Source code in src/taskq/worker/_bootstrap.py
3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 | |
worker_main_async
async
¶
worker_main_async(
settings: WorkerSettings,
*,
actor_registry: Mapping[str, ActorRef[Any, Any]]
| None = None,
di_registry: ProviderRegistry | None = None,
cron_registry: list[CronScheduleSpec] | None = None,
connections: WorkerConnections | None = None,
pg_credential_provider: PgCredentialProvider
| None = None,
rate_limit_registry: RateLimitRegistry | None = None,
until_idle: bool = False,
idle_settle_window: float | None = None,
idle_poll_interval: float | None = None,
idle_max_runtime: float | None = None,
ignore_sigterm_on_exit: bool = False,
) -> int
Run a worker to completion on the caller's event loop.
Same parameters, same semantics and same exit codes as
:func:worker_main - which is now the thin synchronous wrapper that
drives this coroutine under its own asyncio.Runner. See
:func:worker_main for what each parameter means.
Why this exists as public API: worker_main owns the loop, and
asyncpg pools are loop-bound. An application whose actors are
closures over its own dependencies has to build those dependencies
(pools, HTTP clients) on the loop the worker will run on, before the
actor registry is constructed - and there is no hook in worker_main
to do that. Pre-creating a pool in a throwaway loop and handing it to
worker_main's Runner is not an option: the pool would belong to a
dead loop. Awaiting this from inside the caller's own coroutine puts
dependency construction, actor construction, and the worker on one
loop::
async def main() -> int:
pool = await asyncpg.create_pool(dsn) # this loop
registry = build_actors(pool) # closures over it
try:
return await worker_main_async(settings, actor_registry=registry)
finally:
await pool.close()
raise SystemExit(asyncio.run(main()))
Signal handling, logging setup and cron auto-discovery are identical to
:func:worker_main; nothing about running here changes the worker's
behaviour. The caller owns whatever it built - TaskQ closes only the
resources it created itself (see :class:~taskq.connections.WorkerConnections
for the ownership rule that governs anything passed via connections).
ignore_sigterm_on_exit follows :func:worker_main's contract with
one difference that matters here: the loop belongs to the caller, so
the guard covers the worker's own handover back to it - SIGTERM is
masked, this loop's own SIGTERM handler is removed (so the caller's
later loop.close() cannot rewrite the disposition), the
disposition is set to SIG_IGN, and the mask is restored. The
caller's post-worker work - closing a pool, dumping a profile,
exiting - then runs with SIGTERM ignored. Use it only when that
remaining work is bounded teardown and exit, never when the process
keeps running or forks children afterwards.
Source code in src/taskq/worker/_bootstrap.py
3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 | |
__getattr__ ¶
Source code in src/taskq/worker/run.py
make_heartbeat_kwargs ¶
make_heartbeat_kwargs(
deps: WorkerDeps,
worker_id: UUID,
backend: Backend,
cancel_wake_event: Event | None = None,
) -> dict[str, object]
Return keyword arguments that wire a cancel controller and optional cancel-wake event into heartbeat_loop for a given worker.
Usage (production, owned by the orchestration layer)::
kwargs = make_heartbeat_kwargs(deps, worker_id, backend, cancel_wake_event)
await heartbeat_loop(deps, worker_id, shutdown, **kwargs)
Returns:
| Type | Description |
|---|---|
dict[str, object]
|
|
Source code in src/taskq/worker/run.py
producer_loop
async
¶
producer_loop(
deps: WorkerDeps,
local_queue: Queue[JobRow],
shutdown_event: Event,
producer_stop_event: Event,
*,
backend: Backend,
worker_id: UUID,
slot_freed_event: Event | None = None,
rng: Random | None = None,
) -> None
Dispatch pending jobs from the database and feed them into local_queue.
On each iteration the producer:
- Waits for a wake signal (NOTIFY-driven
asyncio.Event) or thepoll_intervalfallback timer, whichever fires first. - Calls
backend.dispatch_batch()to atomically claim up to the worker's genuinely free slots,local_queue.maxsize - local_queue.qsize() - active_jobs.count() - active_jobs.intent_count()pending jobs (pending → running) usingFOR UPDATE SKIP LOCKED. - Puts each returned :class:
JobRowontolocal_queuefor the consumer tasks.
A round that returns fewer rows than it asked for arms a short
jittered cooldown (:data:_CLAIM_COOLDOWN_SECONDS) before the next
round, so a burst of wakes or freed slots costs one round rather than
one per trigger; a full round re-claims immediately.
Slot accounting: a worker with max_concurrency slots may
hold at most that many rows locked at once, the claim sizes by the
slots actually free (queue emptiness minus jobs actively running),
not by queue emptiness alone, which allowed up to 2x
max_concurrency rows locked (a full local queue while every
consumer was busy still looked like max_concurrency free slots:
double reclaim exposure on a crash, and head-of-line latency behind
long jobs while peer workers idle). The get()-to-register window is
counted through the claim intent the consumer loop records at the
take, so a consumer paused in DI resolution or a denied-admission
local retry is still a held slot to this arithmetic; the
completion-side slot frees at the consumer's deregister, and
the wake the consumer sets there re-arms this producer the moment
accounting settles, no claim waits for the next poll tick.
Exits cleanly when either shutdown_event or producer_stop_event
is set.
slot_freed_event is set by the consumer loops on every exit path
that releases a slot: a claimed job completing, failing, or being
released (each resolve_claim/deregister that lowers
active_jobs.count() + intent_count()), so the bootstrap wires one
shared event into this loop and every consumer. A queue-to-intent
take (local_queue.get() + mark_claimed()) moves a slot
between the queue and the intent count and changes net availability
by zero, so it signals nothing. rng supplies the
fallback-poll jitter (a test seam; production uses the module RNG
seeded per process). Both default to standalone behaviour: a private
event nobody sets degrades the slot-refill wait to the fallback
cadence, and the module RNG jitters as in production.
Source code in src/taskq/worker/run.py
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 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 | |
producer_loop_stub
async
¶
producer_loop_stub(
deps: WorkerDeps,
local_queue: Queue[JobRow],
shutdown_event: Event,
producer_stop_event: Event,
*,
backend: Backend,
worker_id: UUID,
) -> None
Observe producer_stop_event and shutdown_event; exit cleanly.
Why the unused parameters: this is a drop-in stand-in for
:func:producer_loop (used when a worker runs consume-only), so it
takes that function's signature verbatim and the caller can swap one
for the other without a shim. deps, local_queue, backend
and worker_id are exactly the arguments it does not need because
it never dispatches, a divergent signature here would move that
branch into every call site.
Outer loop: while not (producer_stop_event.is_set() or shutdown_event.is_set()).
Body races producer_stop_event.wait() against shutdown_event.wait() via
asyncio.wait(..., return_when=FIRST_COMPLETED); the loser is cancelled
to avoid a pending-task leak.
Source code in src/taskq/worker/run.py
consumer_loop_stub
async
¶
consumer_loop_stub(
deps: WorkerDeps,
local_queue: Queue[JobRow],
shutdown_event: Event,
*,
backend: Backend,
worker_id: UUID,
stub_work_timeout: float = 60.0,
slot_freed_event: Event | None = None,
) -> None
Pull one job per iteration, register, sleep sentinel, write terminal status.
Outer loop: while not (shutdown_event.is_set() or deps.producer_stop_event.is_set()).
Races local_queue.get() against shutdown_event.wait() and
deps.producer_stop_event.wait(); on a stop win the queue waiter is
cancelled and the stub returns cleanly.
On job get the stub registers in deps.active_jobs, awaits a cancellable
sentinel, writes terminal state via backend (shielded), and deregisters
in finally.
Source code in src/taskq/worker/run.py
2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 | |
di_consumer_loop
async
¶
di_consumer_loop(
deps: WorkerDeps,
local_queue: Queue[JobRow],
shutdown_event: Event,
*,
backend: Backend,
worker_id: UUID,
registry: ProviderRegistry,
process_scope: ProcessScope,
thread_scope: ThreadScope,
loop_scope: LoopScope,
actor_registry: Mapping[str, ActorRef[Any, Any]],
enqueuer: SubJobEnqueuer,
slot_freed_event: Event | None = None,
) -> None
Pull one job per iteration and dispatch via dispatch_one_job.
Outer loop: while not (shutdown_event.is_set() or deps.producer_stop_event.is_set()).
Races local_queue.get() against shutdown_event.wait() and
deps.producer_stop_event.wait(); on a stop win the queue waiter is
cancelled and the loop returns cleanly.
Each job is dispatched through dispatch_one_job which composes build_actor_scope + consume_one_job, providing DI-aware actor invocation with per-invocation TRANSIENT scope teardown.
Source code in src/taskq/worker/run.py
2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 | |
register_worker
async
¶
register_worker(
pool: Pool,
settings: WorkerSettings,
*,
workflow_execution: bool = False,
) -> UUID
Register the current worker in taskq.workers and return its UUID.
Generates a UUIDv7, inserts a row into {schema}.workers, and returns
the new UUID. Acquires from pool with a 2.0 s timeout; on timeout or
connection error the failure is logged and re-raised (registering is
fatal).
If settings.worker_label or settings.workgroup_instance are set,
they are stored directly for cross-process correlation and health checking.
The row's metadata records the worker's runtime facts: whether NOTIFY
dispatch is enabled, max_concurrency, the capacity the worker
runs at (which sizes local_queue and bounds every dispatch), and
workflow_execution — whether this worker's process imported the
flow definitions (the boot's F3 projection ran). The dispatch claim's
EXECUTION fence reads that fact as data: a workflow row is claimable
only by a worker that can resolve its body, so a worker that never
imported the definitions is never handed one to snooze-loop on.
workflow_execution defaults False: a caller that did not run
the projection is not capable, by the same predicate the projection
itself answers (workflow_execution_capable).
Source code in src/taskq/worker/run.py
deregister_worker
async
¶
Remove the worker row from {schema}.workers (best-effort).
Acquires from pool with a 2.0 s timeout. On timeout or connection
error, logs a structured warning deregister_worker_failed and
reports the failure without raising: shutdown MUST not block on this
cleanup, and the caller's backstop (the deps-teardown retry, then the
recovery sweep) decides what a still-unknown row fate means.
Returns the DELETE's outcome: True when the row is confirmed gone
(the DELETE executed and reported rowcount > 0), False when the
delete is not confirmed, either because a transient failure was
swallowed here (the dominant class: pool-acquire timeout, conn loss;
the row's fate is unknown) or because the DELETE matched no row (the
sweep or a peer already removed it, so there was nothing to delete).
A non-transient failure still raises: it would reproduce identically
in any retry, so reporting it as transient-shaped False would
livelock the backstop while logging like a blip.
Source code in src/taskq/worker/run.py
Maintenance leader (election, sweeps, cron, prune/archive)¶
leader ¶
Maintenance leader: election, watchdog, and recovery sweeps.
A single elected leader per cluster runs cooperative loops inside one
asyncio.TaskGroup: election, watchdog, scheduled-wake (sweep 3), cron,
sweep (sweeps 1/2/4), prune (sweep 5), archive expiry (sweep 6), stale
worker cleanup, queue depth, reservation slots, and backlog detection.
Non-leader pods retry election periodically and park the scheduled-wake,
cron, and capacity-drain loops - no timer, no capacity subscription - until
the role arrives; the backlog detector is the deliberate exception (every
worker samples; see _backlog_detection_loop).
Leadership is a row lease in maintenance_leader: the holder writes an
expires_at it chose from leader_lease and renews it every heartbeat
interval, and any pod may take the row over once that instant has passed.
The row alone decides the election in every state, held, lapsed, or
absent, so nothing about the role depends on the liveness of a
connection: a holder that dies without a FIN is replaced on a horizon
TaskQ controls rather than the server's connection-reaping schedule, and
the replacement needs no privilege beyond UPDATE on that row. The
schema-qualified advisory lock is still taken after winning, as a courtesy
to pods from releases that only understand the lock; it is never required,
never waited on, and its absence never gates or costs the role.
Failover SLA
Graceful stop sub-second when the wake lands: the resign is the first act of the shutdown (the shutdown-ordering contract below: the handover happens at shutdown START, not after the drain, so draining adds no latency to the handover), it deletes the row and then broadcasts on the leadership wake channel, a HINT (the row and the fence stay the authority) that makes a follower re-run the fenced elect after at most leader_wake_jitter of damping. The wake is never the guarantee: a lost, duplicated, malformed, or disabled broadcast, and a killed leader (that can notify no one), degrades to the plain bound ≤ heartbeat_interval + one round trip (the resign deletes the row; the next election wins it - the bound the hint degrades to, and the crash bound)
Worker killed ≤ leader_lease + heartbeat_interval + one round trip
Silent leader ≤ leader_lease + heartbeat_interval + one round trip
Won, unassumable ≤ leader_lease + heartbeat_interval + one failing
cycle's connection attempts (each bounded by
reload_factory_timeout; the trust-spent hand-back
deletes the row: the lapse backstop cannot fire
while sub-lease re-wins keep refreshing it; see
_hand_back_unassumable_lease)
Partition detect ≤ watchdog_interval + heartbeat_interval + 2 s
PG failover ≤ heartbeat_interval
Watchdog detect ≤ watchdog_interval + heartbeat_interval
Shutdown ordering contract (the no-leadership-while-stopping invariant): a worker that is shutting down never wins, holds, or renews the leader lease, and a leader that begins shutting down hands the lease over at shutdown START, so a rolling deploy never has a leaderless window and never sees a dying pod win or hold leadership.
- The stop signal this runtime obeys is
deps.shutdown_start_event, which either the shutdown orchestrator (before the DRAINING phase touches any row) orisolate_self(on entry, leaving the orchestrator's timestamps unset) arms; the worker-wideshutdownevent bound by the running election loop is observed too, because non-orchestrator paths (an externalshutdown_event.set(), a bare cancel of_main, a sibling crash,isolate_self's finally) fire it without the orchestrator - a loop that raced only the start event would sleep out a full tick after such a set. - The election loop parks the moment a stop signal is observed: no
attempts and no renewals for the rest of the shutdown, however long
the drain runs. It drops its liveness registration when it parks
(detector 2 must not trip on a loop that stopped by design) and
ignores the resign-broadcast wake (:meth:
wake_electionno-ops). - A pod that is LEADING when the stop lands hands the lease over at
shutdown START: demote first (the flag drops before any await), then
the fenced resign over
leader_conn, which is still open - the orchestrator closes it only after the phases. The successor's election tick (or the broadcast wake) elects while THIS pod is still draining its jobs. The teardown resign inrun()'s finally remains as the backstop for exits that never run the orchestrator (a sibling crash, a bare cancel of_main). - An elect statement that was in flight when the stop landed and WON
anyway is handed straight back: fenced resign over the conn the
elect just used, and the assume path never runs - the monitor/cron
conns do not open,
lead()never sets the flag, so no leader-gated loop, sweep or cron tick included, can begin. - The drain does not depend on this pod remaining leader: every phase
of
orchestrate_shutdownwrites through the dispatcher pool and the backend with no leadership premise, and the successor covers the maintenance sweeps from its first tick. - This clause binds leadership, not row lifetime: the heartbeat loop is
deliberately NOT gated on the stop signal and keeps re-stamping - and,
from a zero-row liveness write, re-inserting - the worker's row for the
whole drain. The drain's terminal writes resolve
job_attempts.worker_idthrough the holderFOR KEY SHARECTE, which records NULL once the row is gone, so a beat that stopped restoring would strip the attribution from every attempt a draining, reaped worker hands back. The row it restores lives only until_bootstrap._main'sfinallyrunsderegister_worker.
__all__
module-attribute
¶
__all__ = [
"ArchiveExpiryResult",
"MaintenanceLeader",
"PruneResult",
"_build_retention_per_status",
"_load_actor_retention_overrides",
"_schedule_utc_to_cron",
"archive_expiry_sweep",
"cleanup_stale_workers",
"complete_stale_batches",
"prune_terminal_jobs",
]
ArchiveExpiryResult
dataclass
¶
PruneResult
dataclass
¶
PruneResult(
total_deleted: int,
archived: int,
by_actor: dict[str, int],
by_status: dict[str, int],
cutoffs: dict[str, datetime],
duration_ms: int,
)
LeaderElection
dataclass
¶
LeaderElection(
elected_at: datetime,
transition: LeaderTransition,
previous_worker_id: UUID | None,
)
A won election: the new term's fence and how the row changed hands.
from_row
classmethod
¶
Classify a _LEADER_ELECT_SQL_TEMPLATE row for worker_id.
Source code in src/taskq/worker/leader.py
MaintenanceLeader ¶
MaintenanceLeader(
deps: WorkerDeps,
worker_id: UUID,
backend: Backend,
*,
clock: Clock,
rate_limit_registry: RateLimitRegistry | None = None,
actor_policies: Mapping[str, ActorFirePolicy]
| None = None,
cron_specs: Sequence[CronScheduleSpec] | None = None,
)
Elected leader that runs watchdog, sweeps, cron, and prune loops.
Source code in src/taskq/worker/leader.py
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 | |
wake_election ¶
Request an immediate election attempt (the resign-broadcast seam).
A hint, never a command: the loop still runs every gate it runs
on its ordinary cadence. A worker whose shutdown has begun
IGNORES the wake - no attempt, no win, no lease - the shutdown
ordering contract in this module's doc header. Safe to call from
any task on this loop; the wake is consumed by the election
loop's park wait (_follower_park), which damps it by
leader_wake_jitter - and refuses it outright while stopping,
the same refusal this arm-time gate makes.
Source code in src/taskq/worker/leader.py
run
async
¶
Source code in src/taskq/worker/leader.py
resign
async
¶
Hand the lease back so a peer elects on its next cycle.
Fenced on the term, so a resign issued after a takeover cannot delete the successor's row. Best-effort by design: the lease lapses on its own, so a resign that cannot reach the database costs a replacement pod at most one lease rather than correctness.
Runs at run() teardown, after the shutdown orchestrator has
already closed and nulled a TaskQ-owned leader_conn, so the
write rides the leader-owned monitor conn when the primary is gone.
Both conns are idle by then (the loops that used them have exited),
and the monitor conn is never the orchestrator's to close.
The fence prefers the live term and falls back to the last election
this pod won: a mid-run demotion clears deps.leader_term long
before teardown, and without the fallback this pod's own row, never
taken over, or the DELETE would fence it out, would sit until the
lease lapses while a replacement pod waits on it.
The other caller is _hand_back_unassumable_lease, mid-run and
on a pod that never led (leader_term is None, so the fallback
fence is the one that applies): it rides the leader conn the
winning elect just used, before that conn is dropped.
A resign that DELETED the row then broadcasts the vacancy on the
leadership wake channel (_emit_leader_wake), so a successor
assumes sub-second instead of on its next tick. The broadcast is
best-effort on top of a best-effort resign: it is a HINT, the
tick is the guarantee, and a broadcast that never lands costs
exactly what the pre-wake design cost.
Returns whether the resign actually DELETED the row. False is
the ordinary fenced no-op (a successor's row, or none left) and
every best-effort shape (no term to resign, no live conn to ride,
the write raising): the no-op and no-term/no-conn shapes are
silent by design (at teardown they are normal), while a raised
write carries its own leader-resign-failed WARN. A caller
whose policy REPORTS a hand-back must not do so on a False
here, which is why the trust-spent hand-back WARNs only on
a True.
Source code in src/taskq/worker/leader.py
1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 | |
archive_expiry_sweep
async
¶
archive_expiry_sweep(
conn: ConnLike,
*,
batch_size: int = DEFAULT_PRUNE_BATCH_SIZE,
schema: str = "taskq",
drain_gate: Callable[[], bool] | None = None,
statement_timeout_ms: int = DEFAULT_PRUNE_STATEMENT_TIMEOUT_MS,
sizer: SweepBatchSizer | None = None,
) -> ArchiveExpiryResult
Hard-delete expired jobs_archive rows, one bounded,
self-committing batch at a time.
Each batch is one _EXPIRY_CTE_SQL statement, committed before the
next runs, under the same server-side statement_timeout and
breaker semantics as :func:prune_terminal_jobs, see that
function's docstring and
:data:~taskq.constants.DEFAULT_PRUNE_STATEMENT_TIMEOUT_MS for the
timeout/breaker contract. drain_gate is called before every batch;
a False return stops the drain with the batches already
committed.
The POLICY FLOOR (hypertable deployments only): when the connected
server has a policy_retention job registered against
jobs_archive (:func:taskq.backend._retention_floor.retention_policy_floor,
probed once per run — before the batch loop, never per batch — and
failing open to None on any probe error), rows older than the
policy's own drop_after horizon gain the and finished_at >=
$floor conjunct and are left to the policy, which drops them
whole-chunk silently. the BOUNDARY, stated honestly: OLDER than the
floor, the POLICY owns deletion (silent, chunk-granular,
watermark-blind — see the pinned boundary in
tests/test_timescale_retention_interplay.py); NEWER than it
(inside the window), this sweep owns deletion (row-exact on
expire_at, the stamp honored exactly inside the window — the
young chunk whose rows have passed expire_at while their chunk
survives is still this sweep's work, and remains so). The floor's
clock is the POLICY's clock
(finished_at, the chunk alignment column) and the floor's value
is the policy's own horizon (parsed from the registered config, not
re-derived), so a policy registered with a different interval than
the current setting still governs the aged end exactly. A row older
than the floor inside a young chunk is dropped when the chunk itself
ages past the boundary, at most one chunk interval late — chunk
granularity is the trade. On vanilla Postgres the probe fails open
to None and this sweep runs full-range, byte-identical to the
no-floor behavior.
Source code in src/taskq/worker/_leader_shared.py
1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 | |
cleanup_stale_workers
async
¶
cleanup_stale_workers(
conn: ConnLike,
*,
worker_id: UUID,
staleness: timedelta,
schema: str = "taskq",
batch_size: int = DEFAULT_EVENT_WRITER_BATCH_SIZE,
) -> int
Delete worker rows whose last_seen_at exceeds staleness, one
bounded batch per call.
One call deletes at most batch_size workers in one short
transaction; repeated calls drain the stale set a batch at a time.
The caller's worker_id is never deleted. Returns the number of
worker rows removed by this call. Worker-level cascade
(maintenance_leader, job_attempts) is handled by the DDL
ON DELETE clauses, no extra sweeping needed, and the window
bounds workers per call, which bounds that per-worker fan-out per
transaction: a whole-fleet crash drains in committed batches instead
of one transaction rewriting every stale worker's attempt history.
Source code in src/taskq/worker/_leader_shared.py
complete_stale_batches
async
¶
complete_stale_batches(
conn: ConnLike,
*,
schema: str = "taskq",
batch_size: int = DEFAULT_EVENT_WRITER_BATCH_SIZE,
) -> int
Safety net: mark active batches with zero non-terminal jobs as complete.
Covers batches whose completion hook was lost (consumer crash) and intentionally-empty batches (expected_size=0, no jobs at all). One call is one bounded batch: at most batch_size completions, committed, so the sweep loop drains the remainder one call per tick.
Source code in src/taskq/worker/_leader_shared.py
prune_terminal_jobs
async
¶
prune_terminal_jobs(
conn: ConnLike,
*,
retention_per_status: dict[str, timedelta],
archive_retention: timedelta,
batch_size: int = DEFAULT_PRUNE_BATCH_SIZE,
schema: str = "taskq",
actor_overrides: dict[str, timedelta] | None = None,
drain_gate: Callable[[], bool] | None = None,
statement_timeout_ms: int = DEFAULT_PRUNE_STATEMENT_TIMEOUT_MS,
sizer: SweepBatchSizer | None = None,
) -> PruneResult
Archive-and-delete terminal jobs past their retention, one bounded, self-committing batch at a time.
Each batch is one transaction of two statements (the bounded
candidate window, then the lock-bearing _ARCHIVE_CTE_SQL write:
jobs → jobs_archive, job_attempts → job_attempts_archive, jobs
DELETE), committed before the next batch runs. The batch runs under
a server-side statement_timeout (the
:data:~taskq.constants.DEFAULT_PRUNE_STATEMENT_TIMEOUT_MS
derivation), and when sizer is given its latched tier, not
batch_size, sizes every window, so a database that keeps aborting
batches is retried at a reduced tier instead of the same one.
drain_gate is called before every batch; a False return stops
the drain (committed batches stay committed; the remainder waits for
the next attempt). The prune loop passes a gate that returns False on
shutdown and ticks detector-2 liveness between batches.
The archive predicate's clock is the database's own
(statement_timestamp() in the candidate statement, re-verified
at lock time by the write statement's), and the reported cutoffs are
anchored to a database-side clock_timestamp() read, see the
"Anchored to the database clock" comment in the body below.
Source code in src/taskq/worker/_leader_shared.py
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 | |
build_leader_lease_sql ¶
Render the elect / renew / resign statements for schema.
Validates schema against the canonical identifier regex before formatting; asyncpg cannot bind identifiers as parameters.
Source code in src/taskq/worker/leader.py
Dispatch¶
dispatch ¶
Dispatch per-job DI dispatch.
:func:dispatch_one_job composes :func:build_actor_scope with
:func:consume_one_job so the worker's per-job dispatch path is DI-aware:
actors with Annotated[T, Scope.X] parameters receive resolved instances
at dispatch time, scoped to their effective scope, with TRANSIENT teardown
running per invocation.
The dispatch SQL constants and the dispatch_batch asyncpg helper live
in :mod:taskq.backend._dispatch_sql (backend layer). This module
imports them from there since worker → backend is the correct layer
direction.
SlotPoolAcquireError ¶
Bases: Exception
A consumer slot could not obtain a usable transaction connection.
Infrastructure, not a job outcome: the job is already claimed and
recovers by lock-lease expiry, so the consumer loop must not count
this as a job failure. The bounded acquire (or slot-connection init
hook) that raised is recorded and logged at the raise site; this type
exists so the loop can route it around the failure accounting. The
underlying cause stays chained (__cause__).
Source code in src/taskq/worker/dispatch.py
dispatch_one_job
async
¶
dispatch_one_job(
*,
backend: Backend,
deps: WorkerDeps,
job: JobRow,
worker_id: UUID,
registry: ProviderRegistry,
process_scope: ProcessScope,
thread_scope: ThreadScope,
loop_scope: LoopScope,
actor_ref: ActorRef[BaseModel, BaseModel | None],
actor_config: ActorConfigLike,
clock: Clock,
active_jobs: ActiveJobRegistry | None = None,
max_retry_backoff: timedelta = DEFAULT_MAX_RETRY_BACKOFF,
logger_arg: BoundLogger | None = None,
enqueuer: SubJobEnqueuer,
) -> AttemptOutcome
Dispatch one job through the DI-resolved actor scope.
- Acquire the job's transaction connection, from the worker's
dedicated slot pool when one is open (the per-slot path), else
the LOOP-scope connection, else none (autonomous consume). An
acquire failure raises :class:
SlotPoolAcquireErrorbefore any span or metric exists: infrastructure, not a job outcome. On the per-slot path the acquired connection also shadows the LOOP-registeredasyncpg.Connectionfor the actor invocation (loop_slot_values), so the actor's own writes join this job's transaction and concurrent slots never share a connection. - Create the CONSUMER span with link to the PRODUCER span.
- Resolve the registered :class:
~taskq.obs.ErrorReporter(if any) and validate the payload against actor_ref's payload schema. - Build the interim JobContext with the CONSUMER span.
- Open build_actor_scope to resolve DI kwargs.
- Hand the resolved kwargs to consume_one_job via a run_actor closure that injects them.
- TRANSIENT scope is closed on exit (regardless of outcome).
- Record consumer-path metrics outside the span body.
On the per-slot path the job's enqueuer is a per-job
:class:~taskq.client._enqueuer.SubJobEnqueuer bound to the
acquired connection, so transactional sub-enqueues and their
flush/discard lifecycle are this job's alone.
Source code in src/taskq/worker/dispatch.py
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 | |
Heartbeat and cancellation¶
heartbeat ¶
Heartbeat loop and cancel-poll seam.
Each tick acquires one connection from heartbeat_pool, opens a single
transaction, and atomically extends workers.last_seen_at, jobs lock /
heartbeat columns, and reservation_slots leases for jobs locked by this
worker (worker-leased rows only: a non-worker-leased hold carries no lease
to renew, and stamping one onto it would hand the abandonment signal a hold
nobody renews again), except the jobs the worker has disowned
(WorkerDeps.disowned_jobs:
finished with, outcome unrecordable), whose leases must lapse so the
reclaim sweep can hand them back. The tick's whole command sequence runs
under one command-timeout budget (see the tick block and
:func:_lease_renewal_threshold for the lease-model arithmetic that
depends on it), with a bounded rollback-or-close teardown. The jobs-lock
renewal is threshold-gated (:func:_lease_renewal_threshold): a row
whose lease is still comfortably fresh is left alone so a healthy beat
stops paying a non-HOT update per running row per tick, while
rows carrying a per-job heartbeat_timeout (the reclaim sweep's
heartbeat arm needs their beats fresh) and rows at/under the threshold
renew on every beat. After max_heartbeat_failures consecutive failed
ticks, transient connection failures and unexpected errors alike, with
the count reset only by a fully successful tick, isolate_self
proactively transitions running jobs and signals shutdown.
The job statements are owed, not unconditional: a tick whose worker holds
nothing in memory and has no claim-side activity outstanding (see
:class:taskq.worker.deps.JobDmlWindow) runs only the workers
liveness update, because nothing it could renew, reconcile, or poll is
out there under this worker's lock.
heartbeat_loop
async
¶
heartbeat_loop(
deps: WorkerDeps,
worker_id: UUID,
shutdown: Event,
*,
cancel_controller: CancelController | None = None,
cancel_wake_event: Event | None = None,
) -> None
Source code in src/taskq/worker/heartbeat.py
607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 | |
isolate_self
async
¶
Source code in src/taskq/worker/heartbeat.py
1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 | |
cancel ¶
Active-job tracking, cancel-poll hook factory.
This module implements ActiveJobRegistry, the loop-scoped in-process map
of running jobs, the _ActiveJob dataclass, and the CancelController
class that drives the five-phase cancel-poll loop.
CancelController exposes two methods driven by heartbeat_loop:
-
run_in_tx(conn), runs inside the heartbeat transaction. Phases 1, 2, and the phase-3 eligibility check happen here. Phase-3 jobs are queued into_pending_abandonsrather than callingmark_abandoneddirectly, becausemark_abandoneduses a separate pool connection that would deadlock on the row lock that the heartbeat transaction still holds. -
run_post_tx(), called byheartbeat_loopafter the transaction block exits (and therefore after the transaction has committed and released its row locks). Drains_pending_abandons, callingmark_abandoned+ deregister for each entry.
Key correctness invariants ():
- _by_id mutations (register/deregister) are protected by asyncio.Lock.
- all() is synchronous: in asyncio's single-threaded model no other
coroutine can mutate _by_id between the list-copy and return unless
there is an intervening await. all() has no await, so the
copy is atomic from the event-loop perspective. The lock is not acquired
in all(), acquiring an asyncio.Lock requires await and would force
a coroutine boundary that breaks the atomicity guarantee.
- cancel_observed_at uses asyncio.get_running_loop().time() (monotonic
event-loop clock), never time.time() or datetime.now().
- Phase-2 PG write (conn.execute) happens before the cancel delivery
(deliver_first_cancel(active.task)) in
code order with no intervening await (PG-first invariant).
- Phase-1 stamps the operator's origin and cancel metadata on the live
context BEFORE it sets cancel_event/_abort_requested: a body
woken by the event always reads a coherent cancel state.
- No try/except inside the phase-2 block, PG-write failures propagate
to heartbeat_loop's outer handler.
- run_post_tx is always called after run_in_tx on the same tick, even
when run_in_tx raises; heartbeat_loop must call it in a finally
block (or equivalent) to drain any entries queued before the error.
__all__
module-attribute
¶
__all__ = [
"ActiveJobRegistry",
"CancelController",
"_ActiveJob",
"hydrate_operator_record",
"make_cancel_controller",
]
CancelController ¶
Bases: Protocol
Structural interface for cancel-poll controllers.
heartbeat_loop calls run_in_tx inside the open heartbeat
transaction and run_post_tx immediately after the transaction commits.
Implementations must satisfy this two-phase contract.
The concrete production implementation is _CancelController, constructed
via make_cancel_controller. Test stubs need only implement these two
methods to satisfy the type.
run_in_tx
async
¶
Execute cancel-poll phases 1-3 inside the heartbeat transaction.
An implementation that still owns cancellations the poll is the only
writer for keeps WorkerDeps.job_dml_window armed, so a tick whose
in-memory holds are all gone still runs the poll (see
:class:JobDmlWindow).
Source code in src/taskq/worker/cancel.py
ClaimIntent
dataclass
¶
The token a claim take hands back to the taker.
Identity: one instance per mark_claimed call, held by the loop
iteration that took the row and returned to resolve_claim at exit,
so the resolver drops only ITS own claim (the issue-461 class: the same
key can be re-marked by a later generation's take before the stale
generation unwinds). Object identity alone is what resolve_claim
scopes to; claim_epoch is what every hand-back pass scopes its SQL
exclusion to, so a stale attempt's standing intent cannot shield a
sibling claim of the same job_id (the same class the registered
entries' ctx.claim_epoch covers).
ActiveJobRegistry ¶
Loop-scoped in-process map of running jobs on this worker.
One instance per worker process, constructed in _main() / WorkerDeps
before the TaskGroup is entered. Multiple workers in the same process (test
scenarios) each carry their own independent registry.
Thread-safety: not applicable, asyncio workers are single-threaded. The
asyncio.Lock on _by_id prevents interleaving between coroutines that
await register / await deregister while the heartbeat or consumer is
also running.
Public surface
mark_enqueued(job_id, claim_epoch) -> None(sync): the producer's claim handed tolocal_queue.queued_ids() -> list[JobId](sync snapshot): the bare-id projection ofqueued_claims().queued_claims() -> list[tuple[JobId, int]](sync snapshot): the parked rows with the claim epoch they were parked at.mark_claimed(job_id, claim_epoch) -> ClaimIntent(sync): a queue take's identity; the coverage moves queued to intent.resolve_claim(job_id, token) -> None(sync, identity-scoped): drop only the caller's own take.held_ids() -> list[JobId](sync snapshot): the bare-id projection ofheld_claims().held_claims() -> list[tuple[JobId, int]](sync snapshot): every registered entry and standing intent with its claim epoch; the exclusion identity the hand-back passes bind.register(job_id, task, ctx) -> _ActiveJob(async): install an in-flight entry, absorbing only an intent at the same claim epoch.deregister(job_id, entry) -> None(async, identity-scoped): remove only the caller's own registration.get(job_id) -> _ActiveJob | None(sync).all() -> list[_ActiveJob](sync snapshot copy).count() -> int(sync): registered entries only.intent_count() -> int(sync): standing claim intents only.holds_released() -> int(sync): monotonic count of registrations and claim intents that have ended, so a beat can see a hold that came and went between two beats.
Source code in src/taskq/worker/cancel.py
mark_enqueued ¶
Record a claim the producer handed to the local queue.
Called by the producer before the queue put (the put is the first await after the claim's return, and the mark must precede any await a hand-back pass could interleave at).
Source code in src/taskq/worker/cancel.py
queued_ids ¶
queued_claims ¶
mark_claimed ¶
Record a queue take's claim identity for the hand-back passes.
Called after the take's waiter cleanup and before actor admission or registration. That cleanup can yield, so stale and newer local copies can record their takes out of order; the epoch guard below keeps the newer claim covered. The take moves the row's coverage from the queued map to the intent map, so the claim-to-register chain has no unfenced window once the mark lands.
Returns the claim token the caller must hand back to
resolve_claim at exit: the same key can be re-marked by a
later generation's take (a same-worker re-claim) before this
caller unwinds, and the token is the only identity that scopes
the resolve to this caller's own claim. A delayed stale take neither
replaces a newer claim's intent nor removes its queued coverage; the
stale token simply resolves no intent entry.
Source code in src/taskq/worker/cancel.py
resolve_claim ¶
Drop the claim intent once register covers it or the row is released.
Identity-scoped: the entry is removed only when the map's current
token IS the caller's. A stale generation's resolve (its take
superseded by a same-worker re-claim's new mark_claimed)
removes nothing, and the live claim's intent survives to fence
the hand-back passes for the window it exists to cover.
Source code in src/taskq/worker/cancel.py
held_ids ¶
Snapshot of every held row's bare id: the projection of held_claims().
Deliberately not the queued ids (queued_ids()): a row parked
in local_queue at a DRAINING exit must be re-pended by the exit
hand-back, which excludes this snapshot only, while the isolate
pass excludes both maps.
The hand-back passes bind held_claims() instead, so their
exclusion names the claim identity rather than the row; this
bare-id reader stays for the callers that only need to know
whether a row is held at all (the cancel controller's unheld
walk, the heartbeat's lost-claim probe). It derives from
held_claims() so the two readers cannot disagree about
coverage or order: one source of truth for the exclusion set.
Source code in src/taskq/worker/cancel.py
held_claims ¶
Snapshot of every held row with the claim epoch this process holds it at.
The exclusion identity a hand-back pass needs: Postgres stamps a
strictly monotonic claim_epoch on every claim, so
(job_id, claim_epoch) names one attempt and nothing else. A
bare job_id exclusion lets a stale attempt still registered
here shield a newer, unstarted claim of the same row from the
hand-back, and the worker then exits leaving that claim running
and locked to it.
This reader is the source of truth: held_ids() is its bare-id
projection. Registered entries come first (each at its context's
epoch), then standing claim intents. Both maps can hold the same
job_id at different epochs while a stale attempt unwinds, and
both pairs are returned so neither masks the other.
Source code in src/taskq/worker/cancel.py
register
async
¶
register(
job_id: JobId,
task: Task[object],
ctx: JobContext[BaseModel],
*,
workflow_claim: bool = False,
operator_record_claim: bool = True,
) -> _ActiveJob
Register a job as in-flight.
The lock ensures no concurrent deregister sees an inconsistent state.
A claim intent at this context's epoch is absorbed: the registration
now covers that claim. An intent at a newer epoch belongs to a later
take and remains standing. Likewise, a delayed stale registration
never overwrites a newer registered claim.
Returns the caller's entry, which it must hand back to deregister
at exit whether or not a newer claim kept the key. The same key can be
re-registered by a later attempt of the same job (a lapsed lease
re-claimed on this worker), and the return value is the only identity
that tells the caller's own registration from the live attempt's.
workflow_claim marks an attempt that executes a workflow node row
(see :class:_ActiveJob): its terminal belongs to the workflow engine.
operator_record_claim is the narrower gate for consuming the row's
operator metadata; the dispatch clears it until the execution door
classifies the row.
Source code in src/taskq/worker/cancel.py
deregister
async
¶
Remove a job from the registry (idempotent, ignores missing keys).
Identity-scoped: the pop happens only when the map's current entry
IS the entry this caller registered (or holds from get()).
A same-worker re-claim re-registers the same key with a NEW entry
(the later attempt's), so a stale attempt's exit removes only its
own registration and the live attempt's survives. The bare-id pop
this replaces evicted the live attempt's registration behind every
defence that reads held_ids() (issue 461). This is the same
fence taskq.progress._flush._drop_fenced_out_buffer applies to
the progress buffers, the one other same-keyed map whose entries
are per-attempt.
Source code in src/taskq/worker/cancel.py
get ¶
Return the registry entry for job_id, or None if absent.
Synchronous and lock-free: safe to call from the heartbeat hook or any non-mutating code path between awaits.
Source code in src/taskq/worker/cancel.py
all ¶
Return a snapshot copy of all in-flight entries.
Synchronous: in asyncio's cooperative multitasking, no other coroutine
can mutate _by_id while this method runs (there is no await
between list(...) and return). The copy ensures the caller's
for loop cannot raise RuntimeError: dictionary changed size during
iteration even if register/deregister are called in later
coroutine steps.
Callers that need a fresh count after iterating must call count()
separately.
Source code in src/taskq/worker/cancel.py
count ¶
holds_released ¶
How many registrations and claim intents this registry has ended.
Monotonic, so a reader that compares its previous value with the
current one learns that a hold ended even when it ended and left no
trace in count()/intent_count()/queued_ids(): a job
claimed and released entirely between two heartbeat beats is
invisible to every map the beat reads, and a terminal write fenced
by a concurrent cancel can leave its row running in Postgres for
the cancel ladder with nothing in memory pointing at it.
Source code in src/taskq/worker/cancel.py
intent_count ¶
Return the number of takes not yet absorbed by a registration.
The claim-to-register window (the consumer's DI resolution, payload
validation, and admission acquire all run before register) is
invisible to count(), and that window is where a denied
admission's bounded local retry now waits, so a producer sizing a
claim by registered jobs alone can double-claim every slot its
consumers are already holding.
count() + intent_count() is a CONSERVATIVE UPPER BOUND on the
in-process hold, not an exact sum. Both memberships are
conditional under the epoch guards: mark_claimed removes a
queued entry only when its epoch is not newer than the take's, so
a delayed stale take can coexist with a newer queued entry
(double-counting one row across the producer's three-term slot
expression local_queue.qsize() + count() + intent_count());
and register absorbs only an intent at its own claim epoch, so
a delayed stale registration can coexist with a newer standing
intent while the stale attempt unwinds (double-counting one row
in count() + intent_count() directly). The direction is safe:
the one production reader (producer_loop's available-slot
arithmetic) subtracts the sum, so an over-count only shrinks the
next claim round, and it self-clears when the stale attempt's
deregister / resolve_claim runs.
Source code in src/taskq/worker/cancel.py
make_cancel_controller ¶
Construct a CancelController for the given worker.
Validates schema_name eagerly (before any ticks run) so misconfiguration
is surfaced at startup rather than on the first heartbeat.
Source code in src/taskq/worker/cancel.py
apply_node_classification ¶
Apply the execution door's node-classification verdict to a live attempt.
Genuine workflow nodes open operator_record_claim and immediately flush
any operator record observed by a heartbeat poll while classification was
in flight, preserving coherent (cancel_origin, cancel_reason,
cancel_principal) state before the step body executes. Non-node rows
close the gate and discard any buffered record so caller-supplied vanilla
metadata never leaks onto JobContext.
Source code in src/taskq/worker/cancel.py
hydrate_operator_record
async
¶
Stamp ONE registered attempt with its row's operator record.
The shutdown's forcing probe is the second observer of an operator
request: its escalation can reach the row before the heartbeat poll's
phase-1 arm ever read it, and the attempt it then cancels must read the
operator's reason/principal — an OPERATOR origin beside a NULL record is
state no later tick repairs, because the attempt is gone. The read is the
poll's own record shape (:data:taskq.backend._sql.READ_CANCEL_RECORD_SQL)
over the shutdown's own row pool (the dispatcher pool, the one the drain's
hand-back statement runs on), and BOTH its awaits — the checkout and the
statement — carry dispatcher_command_timeout: a saturated pool must
not park the forcing phase inside the cancellation window.
Never raises and never blocks the cancel it precedes: the request is
already proven by the row's phase column (the escalation matched), so a
failed or unusable read is logged with its cause and the caller proceeds —
the record is metadata, never the signal. Vanilla attempts skip the read
entirely; their reason/principal stay None whatever free-form metadata
their row carries.
Source code in src/taskq/worker/cancel.py
1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 | |
Shutdown¶
shutdown ¶
Two-phase shutdown orchestration.
Consumers (orchestrate_shutdown, health endpoints) MUST observe
deps.shutdown_phase set at the START of each phase, before any
per-phase work. Value NONE (0) means the worker is running normally.
Phase ordering invariant: NONE (0) → DRAINING (1) → CANCELLING (2) → FORCING (3) → RELEASING (4).
The early handover: shutdown_start_event fires before phase 1
touches any row, and the leader runtime's election loop obeys it (park,
no attempts; a leader resigns at shutdown START, before the drain -
leader.py's shutdown-ordering contract). The worker-wide
shutdown_event does not fire until the phases complete; nothing in
the phases below reads leadership state.
SIGQUIT is not registered; produces a core dump on Linux. Use tini or
ulimit -c 0 for containerised deployments.
The second-SIGTERM contract: if the second SIGTERM arrives during
FORCING or RELEASING, setting escalate_event is a no-op, the
orchestrator is already past CANCELLING.
What the phases owe the work: a deploy is an infrastructure event, so it
never terminalises a job. Rows claimed but
never started are handed back at DRAINING (attempt refunded; the producer
repeats the hand-back on exit so a claim round in flight at the signal is
caught too). Rows mid-execution get the cooperative cancel at CANCELLING
and the forced cancel at FORCING; an actor that unwinds is interrupted ,
released back to the fleet, the spent attempt standing; and one still
alive past
both graces is interrupted with a hold at RELEASING (released only once
the process is provably gone). abandoned is never a shutdown verdict:
an operator-cancelled row the process dies with is terminalised with the
operator's own verdict (mark_cancelled, forced) at RELEASING - the
same write its unwinding consumer races - never relabelled abandoned.
__all__
module-attribute
¶
__all__ = [
"ShutdownPhase",
"drain_local_queue_to_pending",
"install_signal_handlers",
"orchestrate_shutdown",
]
ShutdownPhase ¶
Bases: IntEnum
Worker shutdown phase.
NONE , running normally.
DRAINING , stop accepting new dispatch, hand back claimed-but-unstarted
jobs (attempt refunded).
CANCELLING, cooperative cancel of remaining jobs; stamps the shutdown
origin on each signalled job.
FORCING , force-cancel grace, terminal writes shielded; the cancel
escalation write doubles as the origin probe (it lands only
on rows already carrying an operator's cancel request).
RELEASING , release jobs whose actors never unwound back to the fleet
(mark_interrupted: the spent attempt stands, held
until this process is provably gone); jobs under an
operator cancel are terminalised with the operator's own
verdict (mark_cancelled, forced) here.
The value 4 was ABANDONING before the release phase stopped
abandoning, the integer is unchanged, so /health JSON and the CLI
table keep their numbers; only the label moved to what the phase does.
drain_local_queue_to_pending
async
¶
drain_local_queue_to_pending(
deps: WorkerDeps,
worker_id: UUID,
*,
call_site: str = "shutdown",
) -> int
Re-pend every job this worker claimed but never started.
Issues a single bounded-timeout UPDATE that clears the lock on rows
where locked_by_worker = $worker_id and status = 'running',
excluding the claims with live consumers (deps.active_jobs):
CANCELLING owns those, and re-pending one would unlock a row
another worker can claim while its consumer still executes it. The
exclusion names the claim, not the row: a (job_id, claim_epoch)
pair, because this process can hold an older attempt of a row while
a claim round in flight re-claims that same row at a newer epoch,
and a row-scoped exclusion would shield the newer unstarted claim
and leave it locked to the worker on its way out. On
pool exhaustion or connection error the helper logs a warning and
returns 0 so the recovery sweep acts as the backstop rather than a
deadlocked shutdown.
Two callers, one pass each, both on this worker's way out: the
orchestrator's DRAINING phase, and the producer loop's own exit (a
claim round in flight when the stop event landed commits after the
DRAINING pass; the producer's exit pass is the one write that cannot
be overtaken by this worker's next claim, because there is none).
Both refund the claim's attempt increment through the shared
ATTEMPT_REFUND_SQL fragment, a claim that never reached an
actor bought nothing, so it spends nothing (the same idiom the
snooze arms carry; the refund is floored at 0
and a second pass matches no rows, so the two passes together are
exactly-once). mark_interrupted is the deliberate exception: its
attempt DID start executing, so it keeps the increment.
The caller-held-page strip: a canonical workflow node handed back here
drops wf_caller_emit_hold, an episode fact whose transaction (and
whose renewal, the mark's other spender) is gone. The strip is gated on
CALLER_EMIT_HOLD_ROW_SQL because this statement also touches vanilla
rows, whose metadata belongs to the caller.
Why the started_at IS not NULL conjunct: the dispatch claim CTE
stamps started_at = clock_timestamp() AT CLAIM
(backend/_dispatch_sql.py), so every row the drain exists for is
stamped and the conjunct never fences a claimed-but-unstarted row.
What it fences is the OTHER refund writer's output: the heartbeat's
claim-loss reconcile (worker/heartbeat.py) refunds through the same
fragment and UN-STAMPS started_at while leaving the row
running and locked (Sweep 1 owns that reclaim). On a
running-and-locked row a NULL started_at means exactly "this
claim was already refunded", so without the conjunct the drain
re-pends the reconciled row and refunds the same claim a second
time: the counter drops below the epoch a genuine execution's
job_attempts row holds, and the row is yanked out of the
Sweep-1 reclaim the reconcile preserved. The conjunct is a
row-version check, not a memory snapshot, so it holds on the
concurrent interleave too: the drain's UPDATE blocking on the
reconcile's open transaction re-evaluates the row when the
reconcile commits (EvalPlanQual) and the committed NULL stamp
drops it from the write set. The refund is exactly-once per claim
across both writers.
The disowned exclusion: a row this worker DISOWNED (a terminal write that failed on infrastructure, a slot-pool acquire failure) is not this helper's population. The disown arms cover attempts that may have STARTED executing, and refunding a started attempt re-creates the exact epoch the first execution ran under - the successor's re-claim would re-run the body at the same attempt number. The row's recovery stays where the disown arms put it: the lease lapses unrenewed and Sweep 1 reclaims at the charged attempt (the reconcile's documented owner).
The DB row carries no
"a consumer took it" mark, so the only honest discriminator for
"never started" is this process's own active-jobs registry; the
claim-to-register window (a job taken off local_queue but not yet
in active_jobs) is invisible to every shutdown arm, CANCELLING
iterates the same registry, and stays outside this predicate's
guarantee.
Args:
deps: The worker's dependency bundle.
worker_id: This worker's id; the hand-back is scoped to its rows.
call_site: Which of the two passes is calling ("draining_phase"
or "producer_exit"). It rides the failure record so an
operator can tell the orchestrator's pass from the producer's
when a rolling deploy leaves one of them unaccounted for.
Returns: Number of rows updated, or 0 on timeout / connection error.
Source code in src/taskq/worker/shutdown.py
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 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 | |
orchestrate_shutdown
async
¶
orchestrate_shutdown(
deps: WorkerDeps,
settings: WorkerSettings,
worker_id: UUID,
shutdown_event: Event,
escalate_event: Event | None = None,
*,
backend: Backend,
) -> int
Run the four-phase shutdown orchestration.
Phases are DRAINING → CANCELLING → FORCING → RELEASING, followed by
TaskQ-owned leader_conn close and shutdown_event.set(). Each
phase is assigned to deps.shutdown_phase before any per-phase work.
Returns 0 on clean exit.
Source code in src/taskq/worker/shutdown.py
784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 | |
install_signal_handlers ¶
install_signal_handlers(
loop: AbstractEventLoop,
deps: WorkerDeps,
worker_id: UUID,
shutdown_event: Event,
escalate_event: Event,
backend: Backend,
orchestrator_holder: list[Task[int]],
) -> None
Register SIGTERM/SIGINT/SIGHUP handlers.
SIGTERM/SIGINT: three-signal escalation counter. First signal
schedules orchestrate_shutdown via loop.create_task and
appends the created task to orchestrator_holder so that _main
can later await it for the exit code. Second signal sets
escalate_event to fast-advance CANCELLING → FORCING. Third signal
calls sys.exit(1) (Kubernetes SIGKILL is the hard backstop).
SIGHUP: sets deps.reload_event to signal the hot-reload
coordinator (:func:~taskq.worker.deps.reload_credentials) that a
credential refresh has been requested. The coordinator runs as a
sibling task in the worker's TaskGroup and performs the actual
pool/connection swap. SIGHUP can be sent multiple times, each one
sets the event. The coordinator clears the event before each
reload and never after, so a SIGHUP arriving mid-reload (success or
failure) is honored with exactly one follow-up reload: N signals
during one reload coalesce into one follow-up, not N. Reload
requests arriving while shutdown orchestration is in progress
(deps.shutdown_phase is not NONE) are skipped.
The signal counter is closure-scoped, each call to this function
creates a fresh, independent counter. The handler callable contains
zero await or I/O.
Source code in src/taskq/worker/shutdown.py
1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 | |
Drain (until-idle monitor and exit-code contract)¶
drain ¶
Drain monitor for until-idle mode.
When spawned as a sibling in the worker's TaskGroup, the drain monitor polls the backend for active jobs in the worker's subscribed queues. When the count stays zero for the settle window (and no jobs are active on this worker), the monitor triggers the normal graceful shutdown via orchestrate_shutdown, the same path SIGTERM takes.
Exit codes
0, all jobs succeeded (drain_failures == 0) 3, some jobs failed (drain_failures > 0) 4, max_runtime exceeded before drain completed
__all__
module-attribute
¶
__all__ = [
"EXIT_DRAIN_CLEAN",
"EXIT_DRAIN_TIMEOUT",
"EXIT_DRAIN_WITH_FAILURES",
"drain_monitor_loop",
]
drain_monitor_loop
async
¶
drain_monitor_loop(
deps: WorkerDeps,
settings: WorkerSettings,
worker_id: UUID,
shutdown_event: Event,
escalate_event: Event,
orchestrator_holder: list[Task[int]],
backend: Backend,
*,
idle_settle_window: float,
idle_poll_interval: float,
max_runtime: float | None,
) -> None
Monitor for queue drain and trigger graceful shutdown when idle.
Polls backend.count_active_jobs(queues) and deps.active_jobs.count() every idle_poll_interval. When both are zero, starts the settle timer. If still zero after idle_settle_window, triggers shutdown.
If max_runtime is set and exceeded, triggers shutdown with exit code 4.
Returns after creating the orchestrate_shutdown task. Spawns with may_return=True in the sibling spawner. Does not set shutdown_event , orchestrate_shutdown's finally block sets it at the correct point (after all phase work completes), exactly as the SIGTERM signal handler does.
Forgets the liveness registration on every exit path (same contract as the producer loop, run.py): the monitor stops ticking the moment it returns, while the watchdog keeps sweeping until the orchestration's finally sets shutdown_event.
Double-orchestration guard: if orchestrator_holder is already non-empty or deps.shutdown_phase is not ShutdownPhase.NONE, the monitor skips triggering, a SIGTERM-driven orchestration is already in progress.
Source code in src/taskq/worker/drain.py
47 48 49 50 51 52 53 54 55 56 57 58 59 60 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 | |
Workgroup supervisor¶
workgroup ¶
Workgroup supervisor, spawns and manages multiple TaskQ worker processes.
A workgroup is a single-process orchestrator that manages N child taskq worker
subprocesses, each with potentially different queues, poll intervals, concurrency
caps, etc. The supervisor restarts children that crash, optionally health-checks
them via the database, and cleanly propagates shutdown signals.
Config format (TOML)::
actors = "myapp.actors:registry"
[defaults]
poll_interval = 1.0
max_concurrency = 4
[supervisor]
shutdown_grace = 100.0
backoff_initial = 0.5
backoff_max = 30.0
backoff_factor = 2.0
burst_limit = 10
burst_window = 60.0
[[workers]]
name = "api"
queues = ["default"]
max_concurrency = 8
poll_interval = 0.5
pg_credential_provider = "infra.identity:pg_credentials"
[workers.health]
enabled = true
check_interval = 15
stale_after = 60
startup_grace = 15.0
consecutive_failure_limit = 3
A pg_credential_provider set at the [defaults] level applies to
every worker, and TOML has no per-worker null, a worker cannot opt
out of a defaults-level provider, so a workgroup mixing provider-backed
and provider-less workers must set the field per worker instead of in
[defaults].
__all__
module-attribute
¶
__all__ = [
"DEFAULT_SHUTDOWN_GRACE_SECS",
"WorkerSpec",
"WorkgroupConfig",
"load_workgroup_config",
"run_forever",
"worst_case_shutdown_seconds",
]
DEFAULT_SHUTDOWN_GRACE_SECS
module-attribute
¶
Default for [supervisor].shutdown_grace, the supervisor's
SIGTERM-to-SIGKILL window for its children.
Why 100.0: the window must cover a child's modelled worst-case clean
exit, WorkerSettings.worst_case_shutdown_seconds = cancellation
grace (30s) + cleanup grace (10s) + the bounded-close teardown tail
(8 sequential closes x 5s + 2s publish drain = 42s) = 82s, plus ~22%
margin for loop jitter and the shutdown watchdog's non-instantaneous
deadline trip (the dump interval and bounded metrics flush land inside
the tail, but the tail is a model, not a bound the supervisor enforces).
The old 30.0 default sat below even the children's 40s release floor
(cancellation + cleanup graces): the supervisor SIGKILLed children
before a held release could land, and every interrupted job rode the
lease-expiry crash path instead. Operators who want the tighter,
release-floor-only window can still set it explicitly; the startup
warning names the numbers rather than refusing.
SupervisorConfig
dataclass
¶
SupervisorConfig(
shutdown_grace: float = DEFAULT_SHUTDOWN_GRACE_SECS,
backoff_initial: float = 0.5,
backoff_max: float = 30.0,
backoff_factor: float = 2.0,
burst_limit: int = 10,
burst_window: float = 60.0,
health_pg_dsn: str | None = None,
health_pg_schema: str | None = None,
health_pg_direct_tls: bool | None = None,
)
Global supervisor behaviour tunables.
shutdown_grace
class-attribute
instance-attribute
¶
WorkerHealthConfig
dataclass
¶
WorkerHealthConfig(
enabled: bool = False,
check_interval: float = 15.0,
stale_after: float = 60.0,
startup_grace: float = 15.0,
consecutive_failure_limit: int = 3,
)
Per-worker health-check configuration.
The freshness verdict is server-side and exclusive at the boundary:
a worker whose server-measured age equals stale_after exactly is
already stale (the predicate is last_seen_at > now() - stale_after,
strict >).
WorkerSpec
dataclass
¶
WorkerSpec(
name: str,
queues: list[str],
poll_interval: float = 1.0,
max_concurrency: int = 8,
worker_group: str = "default",
force_update_actor_config: bool = False,
pg_credential_provider: str | None = None,
stream_limit: int = _STREAM_LIMIT,
health: WorkerHealthConfig = WorkerHealthConfig(),
)
Configuration for a single worker process managed by the workgroup.
force_update_actor_config
class-attribute
instance-attribute
¶
pg_credential_provider
class-attribute
instance-attribute
¶
health
class-attribute
instance-attribute
¶
cli_args ¶
Build the CLI argument list for this worker.
Source code in src/taskq/worker/workgroup.py
WorkgroupConfig
dataclass
¶
WorkgroupConfig(
actors: str,
supervisor: SupervisorConfig = SupervisorConfig(),
workers: list[WorkerSpec] = list[WorkerSpec](),
defaults: dict[str, Any] = dict[str, Any](),
)
Top-level workgroup configuration loaded from TOML.
supervisor
class-attribute
instance-attribute
¶
workers
class-attribute
instance-attribute
¶
defaults
class-attribute
instance-attribute
¶
from_toml
classmethod
¶
Load and validate the workgroup TOML configuration.
Source code in src/taskq/worker/workgroup.py
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 | |
load_workgroup_config ¶
Load a workgroup configuration from a TOML file.
Source code in src/taskq/worker/workgroup.py
worst_case_shutdown_seconds ¶
The children's modelled worst-case wall clock from SIGTERM to exit.
Delegates to WorkerSettings.worst_case_shutdown_seconds (cancellation
grace + cleanup grace + the bounded-close teardown tail) so the
supervisor surface and the worker's own model stay one number, no
drift. Exposed here so taskq workgroup validate and operators'
sizing scripts can compare a config's shutdown_grace against the
floor it must cover, without importing worker bootstrap internals.
Source code in src/taskq/worker/workgroup.py
run_forever
async
¶
Load config, spawn children, manage lifecycle until a signal arrives.
Blocks until SIGTERM or SIGINT, then shuts down all children gracefully.
SIGHUP is not a shutdown signal: it is forwarded to every living child
so each child's credential hot-reload runs (see
:func:_forward_sighup_to_children); without a handler the default
disposition would terminate the supervisor and orphan every child.
Returns normally after shutdown, the CLI caller handles the exit code.
Source code in src/taskq/worker/workgroup.py
1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 | |