feat: composable task predicates with built-in recipes#156
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds a composable
Predicatesystem for gating task execution at three layers — enqueue, worker dispatch, and middleware — with a library of predefined recipes (time windows, queue health, feature flags, etc.).What's in
PredicateABC with&/|/~operator overloading, short-circuitAndPredicate/OrPredicate/NotPredicate,_CallablePredicateadapter for plain callables,coerce_predicate().True/False/Defer(seconds=N)/Cancel(reason=...)fromevaluate(). Async predicates are awaited transparently viarun_maybe_async.evaluate_predicate()swallows exceptions, returnsFalse, records onPredicateMetrics.taskito.predicates.recipes/— time-based (is_business_hours,in_time_window,after,before,is_weekend,in_timezone), job-attribute (by_queue,by_task,by_priority_at_least,retry_count_under,payload_matches), system-state (queue_size_under,queue_paused,error_rate_under), external-config (env_var_truthy,feature_flagwith pluggableFeatureFlagProvider).Queue.enqueue()/enqueue_many()—Deferbumps delay,CancelraisesPredicateRejectedError._wrap_taskand asyncAsyncTaskExecutor._execute—CancelraisesTaskCancelledError;Deferre-enqueues a fresh job with delay and cancels the current one.TaskMiddlewareacceptspredicate=.legacy_task_filter_to_predicate()keeps contrib'stask_filter=Callable[[str], bool]working. Sentry/OTel/Prometheus middlewares updated to forward both.PREDICATE_DEFERRED,PREDICATE_CANCELLED,PREDICATE_REJECTEDwithphase("enqueue"/"dispatch") discriminator.PredicateMetrics(allowed / denied / deferred / cancelled / errors).docs/content/docs/guides/core/predicates.mdxpage.Layered behavior
TrueFalse(on_false="defer")False(on_false="cancel")PredicateRejectedErrorTaskCancelledErrorDefer(seconds=N)Nto delayNdelay + cancel currentCancel(reason=...)PredicateRejectedErrorTaskCancelledErrorSystem-state recipes use a per-evaluation memo (
PredicateContext._state_cache) so composed predicates that read queue stats don't hammer storage.Test plan
cargo check --workspace(default, postgres, redis, native-async) — all passcargo test --workspace— passesuv run python -m pytest tests/— 596 passed, 9 skipped, including:test_predicates_core.py— 21 tests (operator algebra, short-circuit, fail-closed, async)test_predicates_recipes.py— 32 tests (every built-in recipe + validation)test_predicates_enqueue.py— 14 tests (allow/defer/cancel paths, events, metrics,enqueue_manysemantics)test_predicates_worker.py— 7 tests (real workers, sync defer/re-enqueue round-trip, fail-closed at dispatch)test_predicates_middleware.py— 6 tests (legacytask_filterback-compat, predicate gate)uv run ruff checkanduv run mypy py_src/— clean