Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement set_state on TaskInstancePydantic #38297

Merged
merged 5 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions airflow/api_internal/endpoints/rpc_api_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ def _initialize_map() -> dict[str, Callable]:
TaskInstance._check_and_change_state_before_execution,
TaskInstance.get_task_instance,
TaskInstance._get_dagrun,
TaskInstance._set_state,
TaskInstance.fetch_handle_failure_context,
TaskInstance.save_to_db,
TaskInstance._schedule_downstream_tasks,
Expand Down
41 changes: 28 additions & 13 deletions airflow/models/taskinstance.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
from sqlalchemy.ext.mutable import MutableDict
from sqlalchemy.orm import reconstructor, relationship
from sqlalchemy.orm.attributes import NO_VALUE, set_committed_value
from sqlalchemy.sql.expression import case
from sqlalchemy.sql.expression import case, select

from airflow import settings
from airflow.api_internal.internal_api_call import internal_api_call
Expand Down Expand Up @@ -1846,6 +1846,32 @@ def key(self) -> TaskInstanceKey:
"""Returns a tuple that identifies the task instance uniquely."""
return TaskInstanceKey(self.dag_id, self.task_id, self.run_id, self.try_number, self.map_index)

@staticmethod
@internal_api_call
def _set_state(ti: TaskInstance | TaskInstancePydantic, state, session: Session) -> bool:
if not isinstance(ti, TaskInstance):
ti = session.scalars(
select(TaskInstance).where(
TaskInstance.task_id == ti.task_id,
TaskInstance.dag_id == ti.dag_id,
TaskInstance.run_id == ti.run_id,
TaskInstance.map_index == ti.map_index,
)
).one()

if ti.state == state:
return False

current_time = timezone.utcnow()
ti.log.debug("Setting task state for %s to %s", ti, state)
ti.state = state
ti.start_date = ti.start_date or current_time
if ti.state in State.finished or ti.state == TaskInstanceState.UP_FOR_RETRY:
ti.end_date = ti.end_date or current_time
ti.duration = (ti.end_date - ti.start_date).total_seconds()
session.merge(ti)
return True

@provide_session
def set_state(self, state: str | None, session: Session = NEW_SESSION) -> bool:
"""
Expand All @@ -1855,18 +1881,7 @@ def set_state(self, state: str | None, session: Session = NEW_SESSION) -> bool:
:param session: SQLAlchemy ORM Session
:return: Was the state changed
"""
if self.state == state:
return False

current_time = timezone.utcnow()
self.log.debug("Setting task state for %s to %s", self, state)
self.state = state
self.start_date = self.start_date or current_time
if self.state in State.finished or self.state == TaskInstanceState.UP_FOR_RETRY:
self.end_date = self.end_date or current_time
self.duration = (self.end_date - self.start_date).total_seconds()
session.merge(self)
return True
return self._set_state(ti=self, state=state, session=session)

@property
def is_premature(self) -> bool:
Expand Down
3 changes: 3 additions & 0 deletions airflow/serialization/pydantic/taskinstance.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ def _logger_name(self):
def clear_xcom_data(self, session: Session | None = None):
TaskInstance._clear_xcom_data(ti=self, session=session)

def set_state(self, state, session: Session | None = None) -> bool:
return TaskInstance._set_state(ti=self, state=state, session=session)

def init_run_context(self, raw: bool = False) -> None:
"""Set the log context."""
self.raw = raw
Expand Down