Repository navigation
Add on_dag_scheduling_state_change listener hook - #72378
avanish-garg wants to merge 6 commits into
Conversation
|
Congratulations on your first Pull Request and welcome to the Apache Airflow community! If you have any issues or are unsure about any anything please check our Contributors' Guide
|
Fires from patch_dag() (REST API) and the airflow dags pause/unpause CLI commands when a Dag's is_paused field actually changes. Mirrors the existing hookspec pattern in listeners/spec/dagrun.py. Addresses apache#36516.
00ae832 to
32b1b1f
Compare
…tatus-listener-hook-36516 # Conflicts: # airflow-core/src/airflow/api_fastapi/core_api/routes/public/dags.py # airflow-core/tests/unit/cli/commands/test_dag_command.py
potiuk
left a comment
There was a problem hiding this comment.
Thanks for this, @avanish-garg — a listener for pause changes is a useful gap to close. Since this was opened, main replaced the pause flag with a scheduling state (PAUSED / DRAINING / ACTIVE, set through DagModel.set_scheduling_state), so the hook needs a rework:
- Pauses now also happen in the bulk
PATCH /dagspath (api_fastapi/core_api/services/public/dags.py), when the scheduler pauses a Dag (jobs/scheduler_job_runner.py), and on auto-pause after too many consecutive failed runs (models/dagrun.py, a bulkUPDATEthat never goes through the model). Firing from one place every path shares would cover them all, rather than adding a call to each route. - A
boolcan't expressDRAINING; passing the old and newDagSchedulingStatewould keep the hook useful as the state model grows. - The hook currently fires before the request's transaction commits, so a listener can observe a pause that is then rolled back.
- Please drop
airflow-core/newsfragments/36516.feature.rst: airflow-core only takessignificantnewsfragments (see AGENTS.md).
Could you rebase onto main and rework it along those lines?
Drafted-by: Claude Code (Claude Opus 5); reviewed by @potiuk before posting
…tatus-listener-hook-36516
|
Thanks for the detailed pointers @potiuk. I am reworking it along those lines: one place that every pause path goes through (including the bulk Drafted-by: Claude Code (Opus 5.5) (no human review before posting) |
…tatus-listener-hook-36516
The pause hook only fired from the single-Dag PATCH route and the CLI, so bulk PATCH /dags, the scheduler finishing a drain, and the auto-pause after too many failed runs went unreported. It also passed a bool, which cannot express the draining state, and fired before the request committed, so a listener could see a pause that was then rolled back. DagModel.set_scheduling_state is now the one place every writer goes through, including the two former bulk UPDATEs, and the change is reported with the old and new DagSchedulingState once the outermost transaction commits.
closes: #36516
Adds an
on_dag_scheduling_state_change(dag_id, old_state, new_state)listener hook, fired whenever a Dag'sDagSchedulingState(ACTIVE/PAUSED/DRAINING) changes, after the change is committed.Where it fires from
DagModel.set_scheduling_stateis the single place every writer goes through. Onmainall pause writers already used it (singlePATCH /dags/{dag_id}, bulk/dags/bulk, the scheduler finishing a drain,start_drain, the CLI) except two raw bulkUPDATEs, which now load the rows under a row lock and call the setter:PATCH /dags(api_fastapi/core_api/routes/public/dags.py)max_consecutive_failed_dag_runs(models/dagrun.py), which also refreshes the row so the old state is not read from a stale copyAfter commit
When the state actually changes, the setter queues
(transaction, dag_id, old, new)on the session. Session event handlers inmodels/dag.py:A Dag that changes several times in one transaction is reported once, from its first old state to its last new state; a change that ends where it started is not reported. A failing listener is logged and does not break the commit. Because the hook runs after the commit, it receives the
dag_idrather than the model.Other changes
administration-and-deployment/listeners.rst(including that a listener must not reuse the committing session), and an example inexample_dags/plugins/event_listener.py.significantones).models/test_dag.py(commit, rollback, savepoint, coalescing, failing listener), the public Dags routes, the CLI,test_dagrun.py(auto-pause) and the scheduler drain path.Ran locally with
uv run --project airflow-core pytest(Python 3.12, SQLite):models/test_dag.py253 passed,routes/public/test_dags.py172 passed,listeners/12 passed,models/test_dagrun.py331 passed,test_scheduler_job.py -k "drain or pause or paused"27 passed,cli/commands/test_dag_command.py128 passed (2show_dagtests fail locally only becausegraphvizis not installed). ruff and mypy are clean on the changed files. Not run locally: breeze with Postgres/MySQL and the docs build.Was generative AI tooling used to co-author this PR?
Generated-by: Claude Code (Opus 5.5) following the guidelines