Skip to content
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
972ff6d
Add tasks state command to airflowctl
hyungryuk May 1, 2026
2440e4b
Add unit tests for task_state command and TaskInstancesOperations
hyungryuk May 2, 2026
9f9eb16
Support --map-index in airflowctl tasks state
May 7, 2026
eff9a56
Merge branch 'main' into airflowctl-tasks-state-map-index
IamJasonBian May 11, 2026
f65a04f
Merge branch 'main' into airflowctl-tasks-state-map-index
IamJasonBian May 18, 2026
af5fb7f
Fix airflowctl integration tests for tasks state and taskinstances get
Jun 19, 2026
a036aa4
Merge branch 'main' into airflowctl-tasks-state-map-index
IamJasonBian Jun 19, 2026
66f21f7
Add TaskInstancesOperations.get tests for airflowctl
Jun 20, 2026
95a68e2
Merge branch 'main' into airflowctl-tasks-state-map-index
IamJasonBian Jun 22, 2026
aa2cbef
Remove redundant error-path test for task instance get
Jun 22, 2026
64adc52
Merge branch 'main' into airflowctl-tasks-state-map-index
IamJasonBian Jun 23, 2026
deded12
Merge branch 'main' into airflowctl-tasks-state-map-index
IamJasonBian Jun 25, 2026
f762b02
Merge branch 'main' into airflowctl-tasks-state-map-index
IamJasonBian Jun 27, 2026
2b61f1a
Merge branch 'main' into airflowctl-tasks-state-map-index
IamJasonBian Jul 3, 2026
38c47f6
Merge branch 'main' into airflowctl-tasks-state-map-index
IamJasonBian Jul 3, 2026
0a51fb9
Merge branch 'main' into airflowctl-tasks-state-map-index
IamJasonBian Jul 3, 2026
e562b08
Merge remote-tracking branch 'upstream/main' into airflowctl-tasks-st…
Jul 20, 2026
518dc2d
Merge branch 'main' into airflowctl-tasks-state-map-index
IamJasonBian Jul 20, 2026
3e2ebe5
Align airflowctl tasks state with CLI argument conventions
Jul 20, 2026
b0ab7a6
Merge remote-tracking branch 'origin/airflowctl-tasks-state-map-index…
Jul 20, 2026
fd672fe
Merge branch 'main' into airflowctl-tasks-state-map-index
Jul 22, 2026
18bf937
Merge branch 'main' into airflowctl-tasks-state-map-index
IamJasonBian Jul 22, 2026
075e0d4
Merge branch 'main' into airflowctl-tasks-state-map-index
IamJasonBian Jul 24, 2026
790d9c4
Merge branch 'main' into airflowctl-tasks-state-map-index
IamJasonBian Aug 7, 2026
aa482d1
Merge branch 'main' into airflowctl-tasks-state-map-index
IamJasonBian Aug 23, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ def date_param():
"dags update example_bash_operator --no-is-paused",
# Dag Run commands
"dagrun list --dag-id example_bash_operator --state success --limit=1",
# Tasks state command (manual command, uses flags) - needs a Dag run with completed tasks
'tasks state --dag-id=example_bash_operator --dag-run-id="manual__{date_param}" --task-id=runme_0',
# Task instance get (auto-generated command, uses positional args) - needs a Dag run with completed tasks
'taskinstances get example_bash_operator "manual__{date_param}" runme_0',
# XCom commands - need a Dag run with completed tasks
'xcom add example_bash_operator "manual__{date_param}" runme_0 {xcom_key} \'{{"test": "value"}}\'',
'xcom get example_bash_operator "manual__{date_param}" runme_0 {xcom_key}',
Expand Down
2 changes: 1 addition & 1 deletion airflow-ctl/docs/images/command_hashes.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
main:27a22c00dcf32e7a1a4f06672dc8e3c8
main:0460d9c03248bee26207b20b05aa36b9
assets:70619a2d92bda80930cde2aefcd8e1cd
auth:d79e9c7d00c432bdbcbc2a86e2e32053
backfill:74c8737b0a62a86ed3605fa9e6165874
Expand Down
142 changes: 77 additions & 65 deletions airflow-ctl/docs/images/output_main.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions airflow-ctl/src/airflowctl/api/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
PoolsOperations,
ProvidersOperations,
ServerResponseError,
TaskInstancesOperations,
VariablesOperations,
VersionOperations,
XComOperations,
Expand Down Expand Up @@ -467,6 +468,12 @@ def xcom(self):
"""Operations related to XComs."""
return XComOperations(self)

@lru_cache() # type: ignore[prop-decorator]
@property
def task_instances(self):
"""Operations related to task instances."""
return TaskInstancesOperations(self)

@lru_cache() # type: ignore[prop-decorator]
@property
def plugins(self):
Expand Down
27 changes: 27 additions & 0 deletions airflow-ctl/src/airflowctl/api/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
ProviderCollectionResponse,
QueuedEventCollectionResponse,
QueuedEventResponse,
TaskInstanceResponse,
TriggerDAGRunPostBody,
VariableBody,
VariableCollectionResponse,
Expand Down Expand Up @@ -911,6 +912,32 @@ def delete(
raise e


class TaskInstancesOperations(BaseOperations):
"""Task instance operations."""

def get(
self,
dag_id: str,
dag_run_id: str,
task_id: str,
map_index: int = None, # type: ignore
) -> TaskInstanceResponse | ServerResponseError:
"""
Get a task instance.

When ``map_index`` is non-negative, the mapped task instance endpoint is
called; otherwise the standard (unmapped) endpoint is used.
"""
path = f"dags/{dag_id}/dagRuns/{dag_run_id}/taskInstances/{task_id}"
if map_index is not None and map_index >= 0:
path = f"{path}/{map_index}"
try:
self.response = self.client.get(path)
return TaskInstanceResponse.model_validate_json(self.response.content)
except ServerResponseError as e:
raise e


class PluginsOperations(BaseOperations):
"""Plugins operations."""

Expand Down
50 changes: 50 additions & 0 deletions airflow-ctl/src/airflowctl/ctl/cli_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,36 @@ def _load_help_texts_yaml() -> dict[str, dict[str, str]]:
help="The Dag ID of the Dag to pause or unpause",
)

# Task Commands Args
ARG_TASK_DAG_ID = Arg(
flags=("--dag-id",),
type=str,
dest="dag_id",
required=True,
help="The Dag ID",
)
ARG_DAG_RUN_ID = Arg(
flags=("--dag-run-id",),
type=str,
dest="dag_run_id",
required=True,
help="The Dag Run ID",
)
ARG_TASK_ID = Arg(
flags=("--task-id",),
type=str,
dest="task_id",
required=True,
help="The Task ID",
)
ARG_MAP_INDEX = Arg(
flags=("--map-index",),
type=int,
dest="map_index",
default=-1,
help="If set, query the mapped task instance with this map index (negative means non-mapped)",
)

ARG_ACTION_ON_EXISTING_KEY = Arg(
flags=("-a", "--action-on-existing-key"),
type=str,
Expand Down Expand Up @@ -1015,6 +1045,21 @@ def merge_commands(
),
)

TASK_COMMANDS = (
ActionCommand(
name="state",
help="Get the state of a task instance",
func=lazy_load_command("airflowctl.ctl.commands.task_command.task_state"),
args=(
ARG_TASK_DAG_ID,
ARG_DAG_RUN_ID,
ARG_TASK_ID,
ARG_MAP_INDEX,
ARG_OUTPUT,
),
),
)

core_commands: list[CLICommand] = [
GroupCommand(
name="auth",
Expand Down Expand Up @@ -1057,6 +1102,11 @@ def merge_commands(
help="Manage Airflow variables",
subcommands=VARIABLE_COMMANDS,
),
GroupCommand(
name="tasks",
help="Manage Airflow tasks",
subcommands=TASK_COMMANDS,
),
]
# Add generated group commands
core_commands = merge_commands(
Expand Down
33 changes: 33 additions & 0 deletions airflow-ctl/src/airflowctl/ctl/commands/task_command.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

from __future__ import annotations

from airflowctl.api.client import NEW_API_CLIENT, ClientKind, provide_api_client
from airflowctl.ctl.console_formatting import AirflowConsole


@provide_api_client(kind=ClientKind.CLI)
def task_state(args, api_client=NEW_API_CLIENT) -> None:
"""Get the state of a task instance."""
ti = api_client.task_instances.get(
dag_id=args.dag_id,
dag_run_id=args.dag_run_id,
task_id=args.task_id,
map_index=args.map_index,
)
AirflowConsole().print_as(data=[{"state": ti.state}], output=args.output)
3 changes: 3 additions & 0 deletions airflow-ctl/src/airflowctl/ctl/help_texts.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,6 @@ xcom:
plugins:
list: "List all installed Airflow plugins"
list-import-errors: "List all plugin import errors"

taskinstances:
get: "Retrieve a task instance by Dag ID, run ID, and task ID"
119 changes: 119 additions & 0 deletions airflow-ctl/tests/airflow_ctl/ctl/commands/test_task_command.py

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Do you think it's also worth adding a test to the test_operations.py file for the get command? The tests you added only seem to cover tasks state.

@IamJasonBian IamJasonBian Jun 20, 2026

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

@justinpakzad added here, let me know if I'm checking the right things!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good. Just had another look and none of the existing tests in that file test the error path so I think we can remove test_get_not_found_raises.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

kk, just removed

Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
from __future__ import annotations

import datetime
import uuid

import pytest

from airflowctl.api.client import ClientKind
from airflowctl.api.datamodels.generated import TaskInstanceResponse, TaskInstanceState
from airflowctl.api.operations import ServerResponseError
from airflowctl.ctl import cli_parser
from airflowctl.ctl.commands import task_command


class TestTaskCommands:
parser = cli_parser.get_parser()
dag_id = "example_dag"
dag_run_id = "manual__2024-01-01T00:00:00+00:00"
task_id = "my_task"

task_instance_response = TaskInstanceResponse(
id=uuid.uuid4(),
task_id=task_id,
dag_id=dag_id,
dag_run_id=dag_run_id,
map_index=-1,
run_after=datetime.datetime(2024, 1, 1, 0, 0, 0),
try_number=1,
max_tries=1,
task_display_name=task_id,
dag_display_name=dag_id,
pool="default_pool",
pool_slots=1,
executor_config="{}",
state=TaskInstanceState.SUCCESS,
)

def test_task_state(self, api_client_maker):
api_client = api_client_maker(
path=f"/api/v2/dags/{self.dag_id}/dagRuns/{self.dag_run_id}/taskInstances/{self.task_id}",
response_json=self.task_instance_response.model_dump(mode="json"),
expected_http_status_code=200,
kind=ClientKind.CLI,
)
task_command.task_state(
self.parser.parse_args(
[
"tasks",
"state",
f"--dag-id={self.dag_id}",
f"--dag-run-id={self.dag_run_id}",
f"--task-id={self.task_id}",
]
),
api_client=api_client,
)

def test_task_state_not_found(self, api_client_maker):
api_client = api_client_maker(
path=f"/api/v2/dags/{self.dag_id}/dagRuns/{self.dag_run_id}/taskInstances/{self.task_id}",
response_json={"detail": "Task instance not found"},
expected_http_status_code=404,
kind=ClientKind.CLI,
)
with pytest.raises(ServerResponseError):
task_command.task_state(
self.parser.parse_args(
[
"tasks",
"state",
f"--dag-id={self.dag_id}",
f"--dag-run-id={self.dag_run_id}",
f"--task-id={self.task_id}",
]
),
api_client=api_client,
)

@pytest.mark.parametrize("map_index", [0, 1, 7])
def test_task_state_mapped(self, api_client_maker, map_index):
mapped_response = self.task_instance_response.model_copy(update={"map_index": map_index})
api_client = api_client_maker(
path=(
f"/api/v2/dags/{self.dag_id}/dagRuns/{self.dag_run_id}"
f"/taskInstances/{self.task_id}/{map_index}"
),
response_json=mapped_response.model_dump(mode="json"),
expected_http_status_code=200,
kind=ClientKind.CLI,
)
task_command.task_state(
self.parser.parse_args(
[
"tasks",
"state",
f"--dag-id={self.dag_id}",
f"--dag-run-id={self.dag_run_id}",
f"--task-id={self.task_id}",
f"--map-index={map_index}",
]
),
api_client=api_client,
)
2 changes: 2 additions & 0 deletions docs/spelling_wordlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1674,6 +1674,8 @@ taskInstance
taskinstance
TaskInstanceKey
taskinstancekey
TaskInstances
taskinstances
taskmeta
tasksDuration
tasksetmeta
Expand Down