kgw7401 opened a new pull request, #53357:
URL: https://github.com/apache/airflow/pull/53357

   <!--
    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.
    -->
   
   <!--
   Thank you for contributing! Please make sure that your code changes
   are covered with tests. And in case of new features or big changes
   remember to adjust the documentation.
   
   Feel free to ping committers for the review!
   
   In case of an existing issue, reference it using one of the following:
   
   closes: #ISSUE
   related: #ISSUE
   
   How to write a good git commit message:
   http://chris.beams.io/posts/git-commit/
   -->
   
   
   
   <!-- Please keep an empty line above the dashes. -->
   ---
   
   closes: #52932
   
   # problem brief
   The issue said that docs provide an example of triggering_asset_events, 
which refers to the `source_dag_run` that can be used.
   I also found that, too. Here is the docs 
[link](https://airflow.apache.org/docs/apache-airflow/stable/authoring-and-scheduling/asset-scheduling.html#fetching-information-from-a-triggering-asset-event)
   But when using `source_dag_run`, no attribute error raised
   After reviewing the code, I found that source_dag_run is not implemented in 
AssetEventDagRunReferenceResult.
   
   # How do I implement
   AssetEventDagRunReference that is inherited by 
AssetEventDagRunReferenceResult doesn't have information about dag run. So I 
implemented it using the supervisor to directly retrieve the dagrun.
   Here is the code what i implemented.
   
   ```python
   class AssetEventDagRunReferenceResult(AssetEventDagRunReference):
       @classmethod
       def from_asset_event_dag_run_reference(
           cls,
           asset_event_dag_run_reference: AssetEventDagRunReference,
       ) -> AssetEventDagRunReferenceResult:
           return 
cls(**asset_event_dag_run_reference.model_dump(exclude_defaults=True))
       ....
       @cached_property
       def source_dag_run(self) -> AssetEventSourceDagRun | None:
           if not (self.source_dag_id and self.source_run_id):
               return None
           # Use the supervisor to get the DagRun information via API
           from airflow.sdk.execution_time.task_runner import SUPERVISOR_COMMS
   
           try:
               # Send request to supervisor to get DagRun information
               dag_run_response = SUPERVISOR_COMMS.send(
                   GetDagRun(dag_id=self.source_dag_id, 
run_id=self.source_run_id)
               )
   
               if dag_run_response is None:
                   return None
   
               # Type guard to ensure we have a DagRunResult
               if not isinstance(dag_run_response, DagRunResult):
                   return None
   
               # Check for required fields and provide defaults for optional 
datetime fields
               if (
                   dag_run_response.start_date is None
                   or dag_run_response.data_interval_start is None
                   or dag_run_response.data_interval_end is None
               ):
                   return None
   
               return AssetEventSourceDagRun(
                   dag_id=dag_run_response.dag_id,
                   run_id=dag_run_response.run_id,
                   logical_date=dag_run_response.logical_date,
                   start_date=dag_run_response.start_date,
                   end_date=dag_run_response.end_date,
                   state=dag_run_response.state,
                   data_interval_start=dag_run_response.data_interval_start,
                   data_interval_end=dag_run_response.data_interval_end,
               )
           except Exception:
               # If the API call fails, return None
               return None
   ```
   
   


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

Reply via email to