You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by GitBox <gi...@apache.org> on 2021/09/01 09:01:15 UTC

[GitHub] [airflow] uranusjr commented on a change in pull request #17839: Add DAG run abort endpoint

uranusjr commented on a change in pull request #17839:
URL: https://github.com/apache/airflow/pull/17839#discussion_r700016667



##########
File path: airflow/api_connexion/openapi/v1.yaml
##########
@@ -604,6 +604,41 @@ paths:
         '404':
           $ref: '#/components/responses/NotFound'
 
+  /dags/{dag_id}/dagRuns/{dag_run_id}/state:
+    parameters:
+      - $ref: '#/components/parameters/DAGID'
+      - $ref: '#/components/parameters/DAGRunID'
+
+    post:
+      summary: Set a state of DAG run
+      description: >
+        Set a state of DAG run

Review comment:
       ```suggestion
         description: Set a state of DAG run
   ```
   
   (Nitpicking) don’t need to extra wrap.

##########
File path: tests/api_connexion/endpoints/test_dag_run_endpoint.py
##########
@@ -1154,3 +1156,111 @@ def test_should_raises_403_unauthorized(self):
             environ_overrides={'REMOTE_USER': "test_view_dags"},
         )
         assert response.status_code == 403
+
+
+class TestPostSetDagRunState(TestDagRunEndpoint):
+    @parameterized.expand([("failed",), ("success",)])
+    @freeze_time(TestDagRunEndpoint.default_time)
+    def test_should_respond_200(self, state):
+        dag_id = "TEST_DAG_ID"
+        dag_run_id = "TEST_DAG_RUN_ID_1"
+        test_time = timezone.parse(self.default_time)
+        with create_session() as session:
+            dag = DagModel(dag_id=dag_id)
+            dag_run = DagRun(
+                dag_id=dag_id,
+                run_id=dag_run_id,
+                state=DagRunState.RUNNING,
+                run_type=DagRunType.MANUAL,
+                execution_date=test_time,
+                start_date=test_time,
+                external_trigger=True,
+            )
+            session.add(dag)
+            session.add(dag_run)

Review comment:
       It’s probably easier to use the `dag_maker` fixture for this (but the current implementation is OK).

##########
File path: airflow/api_connexion/schemas/dag_run_schema.py
##########
@@ -104,6 +105,12 @@ def autofill(self, data, **kwargs):
         return data
 
 
+class SetDagRunStateFormSchema(Schema):
+    """Schema for handling the request of setting state of DAG run"""
+
+    state = DagStateField(validate=validate.OneOf([State.SUCCESS, State.FAILED]))

Review comment:
       Nitpick: Use `DagRunState.SUCCESS.value` (and `FAILED`) instead.

##########
File path: airflow/api_connexion/endpoints/dag_run_endpoint.py
##########
@@ -271,3 +275,29 @@ def post_dag_run(dag_id, session):
         )
 
     raise AlreadyExists(detail=f"DAGRun with DAG ID: '{dag_id}' and DAGRun ID: '{run_id}' already exists")
+
+
+@security.requires_access(
+    [
+        (permissions.ACTION_CAN_READ, permissions.RESOURCE_DAG),
+        (permissions.ACTION_CAN_EDIT, permissions.RESOURCE_DAG_RUN),
+    ]
+)
+@provide_session
+def update_dag_run_state(dag_id: str, dag_run_id: str, session) -> dict:
+    """Set a state of a dag run."""
+    dag_run: Optional[DagRun] = (
+        session.query(DagRun).filter(DagRun.dag_id == dag_id, DagRun.run_id == dag_run_id).one_or_none()
+    )
+    if dag_run is None:
+        error_message = f'Dag Run id {dag_run_id} not found in dag {dag_id}'
+        raise NotFound(error_message)
+    try:
+        post_body = set_dagrun_state_form_schema.load(request.json)
+    except ValidationError as err:
+        raise BadRequest(detail=str(err))
+
+    state = post_body['state']
+    dag_run.set_state(state=DagRunState(state).value)

Review comment:
       ```suggestion
       dag_run.set_state(state=DagRunState(state))
   ```
   
   `set_state` accepts a `DagRunState`, not `str`.

##########
File path: airflow/api_connexion/openapi/v1.yaml
##########
@@ -1996,6 +2031,16 @@ components:
       required:
         - dag_id
 
+    UpdateDagRunState:
+      type: object
+      properties:
+        state:
+          description: The state to set this DagRun
+          type: string
+          enum:
+            - success
+            - failed

Review comment:
       I wonder if it makes sense to set the state to other valid values. But I guess those can be added later if someone asks for them, so they should not block this PR.




-- 
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