uranusjr commented on code in PR #70225: URL: https://github.com/apache/airflow/pull/70225#discussion_r4398510036
########## providers/standard/docs/sensors/asset.rst: ########## @@ -0,0 +1,243 @@ + .. 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. + + + +.. _howto/sensor:AssetEventSensor: + +AssetEventSensor +================ + +Use the :class:`~airflow.providers.standard.sensors.asset.AssetEventSensor` to wait for +asset events matching a set of filters to reach an expected count. + +Use this sensor when an already-running Dag needs an asset between tasks. For example, a +daily consumer can run its preparation tasks immediately, then wait for another Dag to +produce the partition for its own data interval. Moving that dependency to an asset schedule +would change when the entire Dag starts and the data interval used by the consumer. + +The sensor declares its target as an inlet, so the dependency is visible in asset lineage +without changing the Dag's schedule. It reads events through ``context["inlet_events"]``. + +.. note:: + + This sensor requires **Apache Airflow 3.2+**. The ``partition_key``, + ``partition_key_regexp_pattern`` and ``extra`` filters require + **Apache Airflow 3.4+**. + +When to use a schedule instead +------------------------------ + +If the dependency does not need to sit in the middle of a run, a schedule is +usually a better fit. See +:doc:`apache-airflow:authoring-and-scheduling/asset-scheduling` for details. + +* Schedule the Dag on the asset when the whole run can wait for it. +* Use ``AssetAndTimeSchedule`` (Airflow 3.4+) when a time-scheduled run needs + the asset before it starts. +* Use a partitioned asset timetable with ``RollupMapper`` to wait for every + partition in a window. + +Use this sensor when some tasks should run immediately and only later tasks +depend on the asset. Here, ``prepare`` starts as soon as the run does, and only +``process`` waits for the partition (``wait_for_partition_events`` is defined +:ref:`below <howto/sensor:AssetEventSensor:filtering>`): + +.. exampleinclude:: /../src/airflow/providers/standard/example_dags/example_asset_sensor.py + :language: python + :dedent: 4 + :start-after: [START example_asset_event_sensor_mid_run] + :end-before: [END example_asset_event_sensor_mid_run] + +Basic usage +----------- + +Pass an :class:`~airflow.sdk.Asset`, :meth:`~airflow.sdk.Asset.ref`, or +:class:`~airflow.sdk.AssetAlias` as ``asset``. A reference must resolve to an +existing asset when the task starts. The target is static so Airflow can record +the inlet; use templated event filters to select a run's events. By default the +sensor succeeds once **at least one** matching event exists. + +.. warning:: + + Without ``after``, ``before`` or a partition filter, the sensor considers + the asset's entire event history, so it succeeds immediately if a matching + event ever existed. Bound the query, for example by time, as below. + +.. exampleinclude:: /../src/airflow/providers/standard/example_dags/example_asset_sensor.py + :language: python + :dedent: 4 + :start-after: [START example_asset_event_sensor] + :end-before: [END example_asset_event_sensor] + +.. note:: + + For asset-triggered runs, ``dag_run.run_after`` can be the timestamp of the + newest triggering event, and ``after`` is inclusive. If the sensor waits on + the same asset that triggered the run, that event satisfies it immediately. + Use ``RunStart()`` to wait for events at or after the actual run start. Review Comment: This was written for a templated example. The example now uses RunStart, so the `run_after` sentence no longer applies. Rewrite to cover what users will hit. Something like for asset-triggered runs, the logical date and data interval are None, so `LogicalDate()` and `DataIntervalStart/End()` raise, so `RunStart()` should be used instead. -- 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]
