You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "itzzdev09 (via GitHub)" <gi...@apache.org> on 2026/08/27 16:51:54 UTC
[PR] Add state command to airflowctl tasks [airflow]
itzzdev09 opened a new pull request, #72163:
URL: https://github.com/apache/airflow/pull/72163
Adds `airflowctl tasks state`, the missing counterpart to the deprecated `airflow tasks state`, which is already annotated `@deprecated_for_airflowctl("airflowctl tasks state")` in `airflow-core/src/airflow/cli/commands/task_command.py` but had nothing to point at.
The command selects a Dag run with either `run_id` or `--logical-date` and prints the state of the task instance. It goes through the existing `task_instances.get` endpoint, so no change to `operations.py` was needed.
To stay consistent with the sibling commands, it reuses the argument handling, validation and error wording already used by `tasks failed-deps`:
- exactly one of `run_id` / `--logical-date` is required
- `--logical-date` must be parseable and timezone-aware
- `--map-index` is reported in the 404 message, so a missing mapped instance is distinguishable from a missing task
- non-404 API errors propagate rather than being swallowed
It prints `None` for a task instance with no state, matching the output of the deprecated command so scripts migrating across keep working.
```
$ airflowctl tasks state example_dag run_this manual__2025-01-01T00:00:00+00:00
success
```
closes: #66174
### Tests
16 new tests in `TestState` covering state values, unset state, map index, logical-date lookup, both argument-validation failures, bad logical dates, 404 handling with and without a map index, missing Dag run, and non-404 propagation.
`46 passed` for the full `test_task_command.py` file β no existing test was modified. `ruff check`, `ruff format --check` and `mypy` are clean on the changed files.
---
##### Was generative AI tooling used to co-author this PR?
- [X] Yes β Claude Code (Sonnet 5)
Generated-by: Claude Code (Sonnet 5) following [the guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#gen-ai-assisted-contributions)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add state command to airflowctl tasks [airflow]
Posted by "SameerMesiah97 (via GitHub)" <gi...@apache.org>.
SameerMesiah97 commented on code in PR #72163:
URL: https://github.com/apache/airflow/pull/72163#discussion_r3874806366
##########
airflow-ctl/src/airflowctl/ctl/cli_config.py:
##########
@@ -1199,6 +1199,22 @@ def merge_commands(
ARG_MAP_INDEX,
),
),
+ ActionCommand(
+ name="state",
+ help="Get the state of a task instance",
+ description=(
+ "Get the state of a task instance. "
+ "Select the run with either run_id or --logical-date (pass exactly one)."
Review Comment:
When `--logical-date` is provided, this resolves the run ID through a lookup using `order_by="-id"` and `limit=1`. Should the help text explain that the latest matching Dag run is selected? Alternatively, if multiple matches should not be possible, are the ordering and limit necessary?
##########
airflow-ctl/src/airflowctl/ctl/commands/task_command.py:
##########
@@ -126,6 +126,37 @@ def failed_deps(args, api_client=NEW_API_CLIENT) -> None:
)
+@provide_api_client(kind=ClientKind.CLI)
+def state(args, api_client=NEW_API_CLIENT) -> None:
+ """Get the state of a task instance."""
+ if (args.run_id is None) == (args.logical_date is None):
+ rich.print("[red]Provide either run_id or --logical-date, but not both[/red]")
+ sys.exit(1)
+
+ run_id = args.run_id or _find_run_id_by_logical_date(api_client, args.dag_id, args.logical_date)
Review Comment:
This is hard to understand. You can make it more explicit. Please see the below:
```
if args.run_id is not None and args.logical_date is not None:
rich.print("[red]Provide either run_id or --logical-date, but not both[/red]")
sys.exit(1)
if args.run_id is not None:
run_id = args.run_id
else:
run_id = _find_run_id_by_logical_date(api_client, args.dag_id, args.logical_date)
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add state command to airflowctl tasks [airflow]
Posted by "itzzdev09 (via GitHub)" <gi...@apache.org>.
itzzdev09 commented on code in PR #72163:
URL: https://github.com/apache/airflow/pull/72163#discussion_r3876092295
##########
airflow-ctl/src/airflowctl/ctl/cli_config.py:
##########
@@ -1199,6 +1199,22 @@ def merge_commands(
ARG_MAP_INDEX,
),
),
+ ActionCommand(
+ name="state",
+ help="Get the state of a task instance",
+ description=(
+ "Get the state of a task instance. "
+ "Select the run with either run_id or --logical-date (pass exactly one)."
Review Comment:
Good question β I dug into it, and multiple matches aren't possible.
`DagRun` declares a uniqueness constraint on the pair:
```python
UniqueConstraint("dag_id", "logical_date", name="dag_run_dag_id_logical_date_key")
```
(`airflow-core/src/airflow/models/dagrun.py`)
The lookup filters `logical_date_gte == logical_date_lte == <value>`, i.e. an exact match on that column, so within one `dag_id` the query can return at most one row. `order_by="-id"` therefore doesn't disambiguate anything, and `limit=1` is only a cheap guard rather than a tie-break.
I've left the query itself alone here: `_find_run_id_by_logical_date` is pre-existing and shared with `tasks failed-deps` and `tasks states-for-dag-run`, so dropping the ordering would change those two commands as well β that felt out of scope for a PR adding a new command. Happy to do it as a follow-up (or here, if you'd rather).
What I did change is the help text, so it states the selection rule instead of leaving it implicit (2e1d575):
> Get the state of a task instance. Select the run with either run_id or --logical-date (pass exactly one). --logical-date matches the Dag run with exactly that logical date.
I described it as an exact match rather than "the latest matching run", since given the constraint there's only ever one.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add state command to airflowctl tasks [airflow]
Posted by "boring-cyborg[bot] (via GitHub)" <gi...@apache.org>.
boring-cyborg[bot] commented on PR #72163:
URL: https://github.com/apache/airflow/pull/72163#issuecomment-5442385765
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](https://github.com/apache/airflow/blob/main/contributing-docs/README.rst)
Here are some useful points:
- Pay attention to the quality of your code (ruff, mypy and type annotations). Our [prek-hooks]( https://github.com/apache/airflow/blob/main/contributing-docs/08_static_code_checks.rst#prerequisites-for-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](https://github.com/apache/airflow/blob/main/airflow-core/docs/howto/custom-operator.rst) Consider adding an example Dag that shows how users should use it.
- Consider using [Breeze environment](https://github.com/apache/airflow/blob/main/dev/breeze/doc/README.rst) 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](https://www.apache.org/foundation/policies/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]( https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#coding-style-and-best-practices).
- 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: dev@airflow.apache.org
Slack: https://s.apache.org/airflow-slack
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add state command to airflowctl tasks [airflow]
Posted by "itzzdev09 (via GitHub)" <gi...@apache.org>.
itzzdev09 commented on PR #72163:
URL: https://github.com/apache/airflow/pull/72163#issuecomment-5445903696
Pushed 0903e25 β this fixes the static-checks failure on the earlier run.
The cause was the `generate-airflowctl-help-images` prek hook: it regenerates the CLI help SVGs from `cli_config.py`, so adding a `tasks` subcommand left the checked-in `output_tasks.svg` (and its entry in `command_hashes.txt`) stale, and the hook failed with "files were modified by this hook".
`output_tasks.svg` and the `tasks` hash are now regenerated. As a check that I reproduced the generation faithfully rather than just producing something plausible, I regenerated the other 16 commands too β all of their hashes and images come out byte-identical to what's committed, so the only changes in the diff are the `tasks` line and its image:
```
-tasks:ea587dc805cadbce81cd640d357f2661
+tasks:eb70701dfe1b9baeda39d5eea07fde5f
```
The new image lists the command in its alphabetical slot:
```
failed-deps Returns the unmet dependencies for a task instance
state Get the state of a task instance
states-for-dag-run Get the status of all task instances in a Dag run
```
Workflows are showing `action_required` again on the new commit β could you re-approve when you have a moment?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add state command to airflowctl tasks [airflow]
Posted by "itzzdev09 (via GitHub)" <gi...@apache.org>.
itzzdev09 commented on code in PR #72163:
URL: https://github.com/apache/airflow/pull/72163#discussion_r3888411234
##########
airflow-core/newsfragments/72163.feature.rst:
##########
Review Comment:
You're right β airflowctl is its own package and isn't part of core, so the news fragment doesn't belong here. Removed it. Thanks for pointing that out!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add state command to airflowctl tasks [airflow]
Posted by "justinpakzad (via GitHub)" <gi...@apache.org>.
justinpakzad commented on code in PR #72163:
URL: https://github.com/apache/airflow/pull/72163#discussion_r3888204063
##########
airflow-core/newsfragments/72163.feature.rst:
##########
Review Comment:
I'm not sure we actually need the news fragment here, `airflowctl` is it's own package and isn't part of core.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add state command to airflowctl tasks [airflow]
Posted by "kaxil (via GitHub)" <gi...@apache.org>.
kaxil commented on PR #72163:
URL: https://github.com/apache/airflow/pull/72163#issuecomment-5942560091
Closing this. #71206 was opened three weeks earlier for #66174 and has been approved.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add state command to airflowctl tasks [airflow]
Posted by "itzzdev09 (via GitHub)" <gi...@apache.org>.
itzzdev09 commented on code in PR #72163:
URL: https://github.com/apache/airflow/pull/72163#discussion_r3876090824
##########
airflow-ctl/src/airflowctl/ctl/commands/task_command.py:
##########
@@ -126,6 +126,37 @@ def failed_deps(args, api_client=NEW_API_CLIENT) -> None:
)
+@provide_api_client(kind=ClientKind.CLI)
+def state(args, api_client=NEW_API_CLIENT) -> None:
+ """Get the state of a task instance."""
+ if (args.run_id is None) == (args.logical_date is None):
+ rich.print("[red]Provide either run_id or --logical-date, but not both[/red]")
+ sys.exit(1)
+
+ run_id = args.run_id or _find_run_id_by_logical_date(api_client, args.dag_id, args.logical_date)
Review Comment:
Made it explicit in 2e1d575 β thanks.
I kept the check covering both cases rather than only "both given", because the snippet above changes behaviour when *neither* is passed: `run_id` stays `None` and we fall through to `_find_run_id_by_logical_date(..., None)`, which raises `AttributeError` on `None.replace(...)` instead of printing the error and exiting 1. `test_state_requires_exactly_one_of_run_id_and_logical_date` covers both inputs (`ids=["neither", "both"]`), so that path is asserted.
What it looks like now:
```python
has_run_id = args.run_id is not None
has_logical_date = args.logical_date is not None
# Rejects both "neither given" and "both given".
if has_run_id == has_logical_date:
rich.print("[red]Provide either run_id or --logical-date, but not both[/red]")
sys.exit(1)
if has_run_id:
run_id = args.run_id
else:
run_id = _find_run_id_by_logical_date(api_client, args.dag_id, args.logical_date)
```
One note: `failed_deps` and `states_for_dag_run` still use the compact `(args.run_id is None) == (args.logical_date is None)` form β I'd originally matched them for consistency. Happy to convert those two in this PR as well if you'd prefer all three to read the same way; I left them alone to keep the diff to the new command.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add state command to airflowctl tasks [airflow]
Posted by "SameerMesiah97 (via GitHub)" <gi...@apache.org>.
SameerMesiah97 commented on PR #72163:
URL: https://github.com/apache/airflow/pull/72163#issuecomment-5446364354
> Pushed 0903e25 β this fixes the static-checks failure on the earlier run.
>
> The cause was the `generate-airflowctl-help-images` prek hook: it regenerates the CLI help SVGs from `cli_config.py`, so adding a `tasks` subcommand left the checked-in `output_tasks.svg` (and its entry in `command_hashes.txt`) stale, and the hook failed with "files were modified by this hook".
>
> `output_tasks.svg` and the `tasks` hash are now regenerated. As a check that I reproduced the generation faithfully rather than just producing something plausible, I regenerated the other 16 commands too β all of their hashes and images come out byte-identical to what's committed, so the only changes in the diff are the `tasks` line and its image:
>
> ```
> -tasks:ea587dc805cadbce81cd640d357f2661
> +tasks:eb70701dfe1b9baeda39d5eea07fde5f
> ```
>
> The new image lists the command in its alphabetical slot:
>
> ```
> failed-deps Returns the unmet dependencies for a task instance
> state Get the state of a task instance
> states-for-dag-run Get the status of all task instances in a Dag run
> ```
>
> Workflows are showing `action_required` again on the new commit β could you re-approve when you have a moment?
I canβt trigger CI. I donβt have the privileges.
@potiuk could you help with that?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add state command to airflowctl tasks [airflow]
Posted by "itzzdev09 (via GitHub)" <gi...@apache.org>.
itzzdev09 commented on PR #72163:
URL: https://github.com/apache/airflow/pull/72163#issuecomment-5455192844
Heads up on a force-push just now (`0903e25` β `9b6b10c`): the file contents are byte-identical to before β I verified the tree hashes match β the only change is that I added a `Co-authored-by:` trailer crediting @SameerMesiah97 on the review-fix commit, since the explicit run-selection structure came from your suggestion.
Both commits are otherwise untouched, and the PR is still `MERGEABLE`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add state command to airflowctl tasks [airflow]
Posted by "itzzdev09 (via GitHub)" <gi...@apache.org>.
itzzdev09 commented on PR #72163:
URL: https://github.com/apache/airflow/pull/72163#issuecomment-5445560315
Thanks for the review @SameerMesiah97 β pushed 2e1d575 addressing both comments, replies inline.
- **Run selection** is now explicit (named booleans + `if`/`else`). I kept the check covering the "neither given" case too, since dropping it would turn that input into an `AttributeError` instead of a clean exit β details in the thread.
- **Help text** now states the selection rule. On the ordering question: `DagRun` has `UniqueConstraint("dag_id", "logical_date")`, so an exact logical-date match can only ever return one run β `order_by="-id"` isn't disambiguating anything. I left the shared `_find_run_id_by_logical_date` helper alone because `tasks failed-deps` and `tasks states-for-dag-run` use it too; happy to clean it up as a follow-up.
- **Newsfragment** added as `airflow-core/newsfragments/72163.feature.rst`. Worth flagging: there's no `airflow-ctl/newsfragments` directory, and `contributing-docs/18_contribution_workflow.rst` only lists `airflow-core` and `chart` as destinations. This change is in `airflow-ctl` rather than core, so let me know if it belongs elsewhere or should be dropped.
On CI β the workflows are still awaiting approval, which I can't trigger myself as a first-time contributor. Could you kick that off when you get a chance?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add state command to airflowctl tasks [airflow]
Posted by "kaxil (via GitHub)" <gi...@apache.org>.
kaxil closed pull request #72163: Add state command to airflowctl tasks
URL: https://github.com/apache/airflow/pull/72163
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org