seanghaeli commented on code in PR #75363:
URL: https://github.com/apache/airflow/pull/75363#discussion_r4734149666
##########
airflow-core/src/airflow/jobs/triggerer_job_runner.py:
##########
@@ -1685,6 +1744,14 @@ async def create_triggers(self):
else:
trigger_name = f"ID {trigger_id}"
trigger_instance = trigger_class(**deserialised_kwargs)
+ if workload.dag_run_data:
+ from airflow.triggers.callback import CallbackTrigger
+
+ # Same as setting task_instance on task-bound triggers
above.
+ if isinstance(trigger_instance, CallbackTrigger):
+ trigger_instance._callback_context =
self._build_callback_context(
Review Comment:
Moved the context build into `CallbackTrigger.run()`, so a build error now
sends a failed event and the callback fails with the error in its output, like
the missing Dag run case. The test breaks the Dag run data and checks the
callback state after `Trigger.submit_event`. For params I went the other way:
if the run's conf no longer validates against the Dag's params, the callback
gets them unvalidated with a warning in its log, on both paths, so the alert
still goes out. Dag callbacks do the same now, since on main they don't run at
all in that case.
##########
task-sdk/src/airflow/sdk/execution_time/context.py:
##########
@@ -1793,3 +1794,125 @@ def context_get_outlet_events(context: Context) ->
OutletEventAccessorsProtocol:
except KeyError:
outlet_events = context["outlet_events"] = OutletEventAccessors()
return outlet_events
+
+
+def build_dag_run_context(
+ dag_run: DagRunProtocol, dag: DAG | None = None, *, multi_team: bool =
False
+) -> Context:
+ """
+ Build the part of a task's template context that comes from its Dag run.
+
+ ``RuntimeTaskInstance.get_template_context`` adds the task keys to it, and
deadline callbacks,
+ which have a Dag run but no task, use it as their context. With ``dag``,
it also has ``dag``
+ and the Dag's ``params`` with the Dag run's ``conf`` applied.
+ """
+ from airflow.sdk.definitions.param import process_params
+ from airflow.sdk.execution_time.comms import
AssetEventDagRunReferenceResult
+ from airflow.sdk.plugins_manager import integrate_macros_plugins
+ from airflow.sdk.timezone import coerce_datetime
+
+ integrate_macros_plugins()
+
+ context: Context = {
+ "dag_run": dag_run,
+ "run_id": dag_run.run_id,
+ "partition_key": dag_run.partition_key,
+ "partition_date": coerce_datetime(dag_run.partition_date),
+ "triggering_asset_events": TriggeringAssetEventsAccessor.build(
Review Comment:
Loaded all of them instead, so nothing is left for the docs: `team_name` and
`triggering_user_name` on the triggerer path, with a team-owned macro test on
each path, and `triggering_asset_events` and `dag_run.note` on both.
##########
task-sdk/src/airflow/sdk/execution_time/callback_supervisor.py:
##########
@@ -198,6 +214,10 @@ def execute_callback(
module = import_module(module_path)
callback_callable = getattr(module, function_name)
callback_kwargs = decode_callback_kwargs(callback_kwargs,
callback_kwargs_encoding)
+ if render_kwargs and not inspect.isclass(callback_callable):
+ from airflow.sdk.execution_time.context import
render_callback_kwargs
+
+ callback_kwargs = render_callback_kwargs(callback_kwargs,
callback_kwargs["context"])
Review Comment:
Yes, let's support it. For a function, the returned notifier is now sent
with `notify()` instead of `result(context)`, so its fields aren't rendered
again. The catch is that a template written inside the function body isn't
rendered anymore, so it belongs in the kwargs; the newsfragment says so.
--
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]