ashb commented on code in PR #44241:
URL: https://github.com/apache/airflow/pull/44241#discussion_r1851738251
##########
airflow/api_fastapi/execution_api/routes/task_instances.py:
##########
@@ -122,6 +125,54 @@ def ti_update_state(
)
elif isinstance(ti_patch_payload, TITerminalStatePayload):
query = TI.duration_expression_update(ti_patch_payload.end_date,
query, session.bind)
+ elif isinstance(ti_patch_payload, TIDeferredStatePayload):
+ trigger_row = Trigger(
+ classpath=ti_patch_payload.classpath,
+ kwargs=ti_patch_payload.kwargs,
+ created_date=ti_patch_payload.created_date,
+ )
+ session.add(trigger_row)
+ session.flush()
+
+ ti = session.query(TI).filter(TI.id == ti_id_str).one_or_none()
+
+ if not ti:
+ raise HTTPException(
+ status_code=status.HTTP_400_BAD_REQUEST,
+ detail={
+ "message": f"TaskInstance with id {ti_id_str} not found.",
+ },
+ )
+
+ ti.state = TaskInstanceState.DEFERRED
+ ti.trigger_id = trigger_row.id
+ ti.next_method = ti_patch_payload.next_method
+ ti.next_kwargs = ti_patch_payload.kwargs or {}
+ # handle properly based on client
+ timeout = ti_patch_payload.timeout
+ # Calculate timeout too if it was passed
+ if timeout is not None:
+ ti.trigger_timeout = timezone.utcnow() +
timedelta(days=int(timeout))
Review Comment:
Timeout shouldn't be days!
##########
airflow/api_fastapi/execution_api/datamodels/taskinstance.py:
##########
@@ -61,6 +61,30 @@ class TITargetStatePayload(BaseModel):
state: IntermediateTIState
+class TIDeferredStatePayload(BaseModel):
+ """Schema for updating TaskInstance to a deferred state."""
+
+ state: Annotated[
+ Literal[IntermediateTIState.DEFERRED],
+ # Specify a default in the schema, but not in code, so Pydantic marks
it as required.
+ WithJsonSchema(
+ {
+ "type": "string",
+ "enum": [IntermediateTIState.DEFERRED],
+ "default": IntermediateTIState.DEFERRED,
+ }
+ ),
+ ]
+
+ classpath: str
+ kwargs: dict[str, Any]
+ created_date: UtcDateTime
+ next_method: str
+ # need to serialise to datetime.timedelta
+ timeout: str | None
+ # what about triggerer_id?
Review Comment:
This gets set in the db once the API processes the request, so it
shouldn't/can't be part of the request
##########
tests/api_fastapi/execution_api/routes/test_task_instances.py:
##########
@@ -193,6 +195,50 @@ def test_ti_update_state_database_error(self, client,
session, create_task_insta
assert response.status_code == 500
assert response.json()["detail"] == "Database error occurred"
+ def test_ti_update_state_to_deferred(self, client, session,
create_task_instance):
+ """
+ Test that tests if the transition to deferred state is handled
correctly.
+ """
+ clear_db_runs()
+
+ ti = create_task_instance(
+ task_id="test_ti_update_state_to_deferred",
+ state=State.RUNNING,
+ session=session,
+ )
+ session.commit()
+
+ payload = {
+ "state": "deferred",
+ "classpath": "my-class-path",
+ "kwargs": {},
+ "created_date": "2024-10-31T12:00:00Z",
+ "next_method": "execute_callback",
+ "timeout": None,
+ }
+
+ response = client.patch(f"/execution/task-instances/{ti.id}/state",
json=payload)
+
+ assert response.status_code == 204
+ assert response.text == ""
+
+ session.expire_all()
+
+ t = session.query(Trigger).all()
+ assert len(t) == 1
+ assert t[0].created_date == datetime(2024, 10, 31, 12, 0,
tzinfo=timezone.utc)
+ assert t[0].classpath == "my-class-path"
+ assert t[0].kwargs == {}
+
+ tis = session.query(TaskInstance).all()
+ assert len(tis) == 1
+
+ assert tis[0].state == TaskInstanceState.DEFERRED
+ assert tis[0].trigger_id == 1
Review Comment:
```suggestion
assert tis[0].trigger_id == t[0].id
```
##########
tests/api_fastapi/execution_api/routes/test_task_instances.py:
##########
@@ -193,6 +195,50 @@ def test_ti_update_state_database_error(self, client,
session, create_task_insta
assert response.status_code == 500
assert response.json()["detail"] == "Database error occurred"
+ def test_ti_update_state_to_deferred(self, client, session,
create_task_instance):
+ """
+ Test that tests if the transition to deferred state is handled
correctly.
+ """
+ clear_db_runs()
+
+ ti = create_task_instance(
+ task_id="test_ti_update_state_to_deferred",
+ state=State.RUNNING,
+ session=session,
+ )
+ session.commit()
+
+ payload = {
+ "state": "deferred",
+ "classpath": "my-class-path",
+ "kwargs": {},
+ "created_date": "2024-10-31T12:00:00Z",
Review Comment:
Hmm, why is this being sent in the payload? Should it be the time the server
received the request instead?
##########
airflow/api_fastapi/execution_api/routes/task_instances.py:
##########
@@ -122,6 +125,54 @@ def ti_update_state(
)
elif isinstance(ti_patch_payload, TITerminalStatePayload):
query = TI.duration_expression_update(ti_patch_payload.end_date,
query, session.bind)
+ elif isinstance(ti_patch_payload, TIDeferredStatePayload):
+ trigger_row = Trigger(
+ classpath=ti_patch_payload.classpath,
+ kwargs=ti_patch_payload.kwargs,
+ created_date=ti_patch_payload.created_date,
+ )
+ session.add(trigger_row)
+ session.flush()
+
+ ti = session.query(TI).filter(TI.id == ti_id_str).one_or_none()
+
+ if not ti:
+ raise HTTPException(
+ status_code=status.HTTP_400_BAD_REQUEST,
+ detail={
+ "message": f"TaskInstance with id {ti_id_str} not found.",
+ },
+ )
+
+ ti.state = TaskInstanceState.DEFERRED
+ ti.trigger_id = trigger_row.id
+ ti.next_method = ti_patch_payload.next_method
+ ti.next_kwargs = ti_patch_payload.kwargs or {}
+ # handle properly based on client
+ timeout = ti_patch_payload.timeout
+ # Calculate timeout too if it was passed
+ if timeout is not None:
+ ti.trigger_timeout = timezone.utcnow() +
timedelta(days=int(timeout))
+ else:
+ ti.trigger_timeout = None
+
+ # If an execution_timeout is set, set the timeout to the minimum of
+ # it and the trigger timeout
+ if ti.task:
+ execution_timeout = ti.task.execution_timeout
+ if execution_timeout:
+ if TYPE_CHECKING:
+ assert ti.start_date
+ if ti.trigger_timeout:
+ ti.trigger_timeout = min(ti.start_date +
execution_timeout, ti.trigger_timeout)
+ else:
+ ti.trigger_timeout = ti.start_date + execution_timeout
+
+ session.merge(ti)
Review Comment:
Merge isn't needed - the object is already attached to the session
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]