-
Notifications
You must be signed in to change notification settings - Fork 14.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate public endpoint Get Task to FastAPI, with main resynced
- Loading branch information
1 parent
807fd6d
commit 2752caf
Showing
7 changed files
with
502 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
# 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 collections import abc | ||
from datetime import datetime | ||
|
||
from pydantic import BaseModel, computed_field, field_validator | ||
|
||
from airflow.api_fastapi.common.types import TimeDeltaWithValidation | ||
from airflow.serialization.serialized_objects import encode_priority_weight_strategy | ||
from airflow.task.priority_strategy import PriorityWeightStrategy | ||
|
||
|
||
class TaskResponse(BaseModel): | ||
"""Task serializer for responses.""" | ||
|
||
task_id: str | None | ||
task_display_name: str | None | ||
owner: str | None | ||
start_date: datetime | None | ||
end_date: datetime | None | ||
trigger_rule: str | None | ||
depends_on_past: bool | ||
wait_for_downstream: bool | ||
retries: float | None | ||
queue: str | None | ||
pool: str | None | ||
pool_slots: float | None | ||
execution_timeout: TimeDeltaWithValidation | None | ||
retry_delay: TimeDeltaWithValidation | None | ||
retry_exponential_backoff: bool | ||
priority_weight: float | None | ||
weight_rule: str | None | ||
ui_color: str | None | ||
ui_fgcolor: str | None | ||
template_fields: list[str] | None | ||
downstream_task_ids: list[str] | None | ||
doc_md: str | None | ||
operator_name: str | None | ||
params: abc.MutableMapping | None | ||
class_ref: dict | None | ||
is_mapped: bool | None | ||
|
||
@field_validator("weight_rule", mode="before") | ||
@classmethod | ||
def validate_weight_rule(cls, wr: str | PriorityWeightStrategy | None) -> str | None: | ||
"""Validate the weight_rule property.""" | ||
if wr is None: | ||
return None | ||
if isinstance(wr, str): | ||
return wr | ||
return encode_priority_weight_strategy(wr) | ||
|
||
@field_validator("params", mode="before") | ||
@classmethod | ||
def get_params(cls, params: abc.MutableMapping | None) -> dict | None: | ||
"""Convert params attribute to dict representation.""" | ||
if params is None: | ||
return None | ||
return {param_name: param_val.dump() for param_name, param_val in params.items()} | ||
|
||
# Mypy issue https://github.com/python/mypy/issues/1362 | ||
@computed_field # type: ignore[misc] | ||
@property | ||
def extra_links(self) -> list[str]: | ||
"""Extract and return extra_links.""" | ||
return getattr(self, "operator_extra_links", []) |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# 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 fastapi import HTTPException, Request, status | ||
|
||
from airflow.api_fastapi.common.router import AirflowRouter | ||
from airflow.api_fastapi.common.types import get_class_ref | ||
from airflow.api_fastapi.core_api.datamodels.tasks import TaskResponse | ||
from airflow.api_fastapi.core_api.openapi.exceptions import create_openapi_http_exception_doc | ||
from airflow.exceptions import TaskNotFound | ||
from airflow.models import DAG | ||
from airflow.models.mappedoperator import MappedOperator | ||
|
||
tasks_router = AirflowRouter(tags=["Task"], prefix="/dags/{dag_id}/tasks") | ||
|
||
|
||
@tasks_router.get( | ||
"/{task_id}", | ||
responses=create_openapi_http_exception_doc( | ||
[ | ||
status.HTTP_400_BAD_REQUEST, | ||
status.HTTP_401_UNAUTHORIZED, | ||
status.HTTP_403_FORBIDDEN, | ||
status.HTTP_404_NOT_FOUND, | ||
] | ||
), | ||
) | ||
def get_task(dag_id: str, task_id, request: Request) -> TaskResponse: | ||
"""Get simplified representation of a task.""" | ||
dag: DAG = request.app.state.dag_bag.get_dag(dag_id) | ||
if not dag: | ||
raise HTTPException(status.HTTP_404_NOT_FOUND, f"Dag with id {dag_id} was not found") | ||
try: | ||
task = dag.get_task(task_id=task_id) | ||
task.__dict__.update( | ||
{"class_ref": get_class_ref(task), "is_mapped": isinstance(task, MappedOperator)} | ||
) | ||
except TaskNotFound: | ||
raise HTTPException(status.HTTP_404_NOT_FOUND, f"Task with id {task_id} was not found") | ||
return TaskResponse.model_validate(task, from_attributes=True) |
This file contains 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
Oops, something went wrong.