Skip to content

Add on_dag_scheduling_state_change listener hook - #72378

Open
avanish-garg wants to merge 6 commits into
apache:mainfrom
avanish-garg:feature/dag-pause-status-listener-hook-36516
Open

avanish-garg wants to merge 6 commits into
apache:mainfrom
avanish-garg:feature/dag-pause-status-listener-hook-36516

Conversation

@avanish-garg

@avanish-garg avanish-garg commented Sep 1, 2026 •

Copy link
Copy Markdown
Contributor

closes: #36516

Adds an on_dag_scheduling_state_change(dag_id, old_state, new_state) listener hook, fired whenever a Dag's DagSchedulingState (ACTIVE / PAUSED / DRAINING) changes, after the change is committed.

Where it fires from

DagModel.set_scheduling_state is the single place every writer goes through. On main all pause writers already used it (single PATCH /dags/{dag_id}, bulk /dags/bulk, the scheduler finishing a drain, start_drain, the CLI) except two raw bulk UPDATEs, which now load the rows under a row lock and call the setter:

  • PATCH /dags (api_fastapi/core_api/routes/public/dags.py)
  • auto-pause after max_consecutive_failed_dag_runs (models/dagrun.py), which also refreshes the row so the old state is not read from a stale copy

After commit

When the state actually changes, the setter queues (transaction, dag_id, old, new) on the session. Session event handlers in models/dag.py:

  • fire the hook only when the outermost transaction commits (releasing a savepoint does not count)
  • drop queued changes whose transaction or savepoint rolls back
  • clear the queue when the root transaction ends

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_id rather than the model.

Other changes

  • Docs: new section in administration-and-deployment/listeners.rst (including that a listener must not reuse the committing session), and an example in example_dags/plugins/event_listener.py.
  • No newsfragment (airflow-core only takes significant ones).
  • Tests in 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.py 253 passed, routes/public/test_dags.py 172 passed, listeners/ 12 passed, models/test_dagrun.py 331 passed, test_scheduler_job.py -k "drain or pause or paused" 27 passed, cli/commands/test_dag_command.py 128 passed (2 show_dag tests fail locally only because graphviz is 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?
  • Yes (please specify the tool below)

Generated-by: Claude Code (Opus 5.5) following the guidelines

@boring-cyborg boring-cyborg Bot added area:API Airflow's REST/HTTP API area:CLI labels Sep 1, 2026
@boring-cyborg

boring-cyborg Bot commented Sep 1, 2026

Copy link
Copy Markdown

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
Here are some useful points:

  • Pay attention to the quality of your code (ruff, mypy and type annotations). Our prek-hooks will help you with that.
  • In case of a new feature add useful documentation (in docstrings or in docs/ directory). Adding a new operator? Check this short guide Consider adding an example Dag that shows how users should use it.
  • Consider using Breeze environment for testing locally, it's a heavy docker but it ships with a working Airflow and a lot of integrations.
  • Be patient and persistent. It might take some time to get a review or get the final approval from Committers.
  • Please follow ASF Code of Conduct for all communication including (but not limited to) comments on Pull Requests, Mailing list and Slack.
  • Be sure to read the Airflow Coding style.
  • Always keep your Pull Requests rebased, otherwise your build might fail due to changes not related to your commits.
    Apache Airflow is a community-driven project and together we are making it better 🚀.
    In case of doubts contact the developers at:
    Mailing List: [email protected]
    Slack: https://s.apache.org/airflow-slack

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.
@avanish-garg
avanish-garg force-pushed the feature/dag-pause-status-listener-hook-36516 branch from 00ae832 to 32b1b1f Compare September 9, 2026 14:04
…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 potiuk left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 /dags path (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 bulk UPDATE that 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 bool can't express DRAINING; passing the old and new DagSchedulingState would 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 takes significant newsfragments (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

@avanish-garg

avanish-garg commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks for the detailed pointers @potiuk. I am reworking it along those lines: one place that every pause path goes through (including the bulk UPDATE auto-pause), old and new DagSchedulingState in the hook, firing after commit, and no newsfragment. I will rebase on main and push when it is ready.


Drafted-by: Claude Code (Opus 5.5) (no human review before posting)

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.
@avanish-garg avanish-garg changed the title Add on_dag_pause_status_change listener hook Add on_dag_scheduling_state_change listener hook Nov 26, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Hook notification when you turn on/off (pause/unpause) a DAG from the UI

2 participants