Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: Add config to TaskInstance context #700

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions cosmos/airflow/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ def create_test_task_metadata(


def create_task_metadata(
node: DbtNode, execution_mode: ExecutionMode, args: dict[str, Any], use_task_group: bool = False
node: DbtNode,
execution_mode: ExecutionMode,
args: dict[str, Any],
use_task_group: bool = False,
) -> TaskMetadata | None:
"""
Create the metadata that will be used to instantiate the Airflow Task used to run the Dbt node.
Expand Down Expand Up @@ -156,6 +159,7 @@ def generate_task_or_group(
test_behavior: TestBehavior,
test_indirect_selection: TestIndirectSelection,
on_warning_callback: Callable[..., Any] | None,
node_config: dict[str, Any] | None = None,
**kwargs: Any,
) -> BaseOperator | TaskGroup | None:
task_or_group: BaseOperator | TaskGroup | None = None
Expand All @@ -176,7 +180,7 @@ def generate_task_or_group(
if task_meta and node.resource_type != DbtResourceType.TEST:
if use_task_group:
with TaskGroup(dag=dag, group_id=node.name, parent_group=task_group) as model_task_group:
task = create_airflow_task(task_meta, dag, task_group=model_task_group)
task = create_airflow_task(task_meta, dag, task_group=model_task_group, extra_context=node_config)
test_meta = create_test_task_metadata(
"test",
execution_mode,
Expand All @@ -185,11 +189,12 @@ def generate_task_or_group(
node=node,
on_warning_callback=on_warning_callback,
)
test_task = create_airflow_task(test_meta, dag, task_group=model_task_group)
test_task = create_airflow_task(test_meta, dag, task_group=model_task_group, extra_context=node_config)
task >> test_task
task_or_group = model_task_group
else:
task_or_group = create_airflow_task(task_meta, dag, task_group=task_group)

return task_or_group


Expand Down Expand Up @@ -251,6 +256,7 @@ def build_airflow_graph(
test_indirect_selection=test_indirect_selection,
on_warning_callback=on_warning_callback,
node=node,
node_config=node.config,
)
if task_or_group is not None:
logger.debug(f"Conversion of <{node.unique_id}> was successful!")
Expand Down
7 changes: 5 additions & 2 deletions cosmos/core/airflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@

from cosmos.core.graph.entities import Task
from cosmos.log import get_logger

from typing import Any

logger = get_logger(__name__)


def get_airflow_task(task: Task, dag: DAG, task_group: "TaskGroup | None" = None) -> BaseOperator:
def get_airflow_task(
task: Task, dag: DAG, task_group: "TaskGroup | None" = None, extra_context: dict[str, Any] | None = None
) -> BaseOperator:
"""
Get the Airflow Operator class for a Task.

Expand All @@ -30,6 +32,7 @@ def get_airflow_task(task: Task, dag: DAG, task_group: "TaskGroup | None" = None
task_id=task.id,
dag=dag,
task_group=task_group,
extra_context=extra_context,
**task.arguments,
)

Expand Down
9 changes: 9 additions & 0 deletions cosmos/operators/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class DbtBaseOperator(BaseOperator):
(i.e. /home/astro/.pyenv/versions/dbt_venv/bin/dbt)
:param dbt_cmd_flags: List of flags to pass to dbt command
:param dbt_cmd_global_flags: List of dbt global flags to be passed to the dbt command
:param extra_context: A dictionary of values to add to the Airflow Task context
"""

template_fields: Sequence[str] = ("env", "vars")
Expand Down Expand Up @@ -105,6 +106,7 @@ def __init__(
dbt_executable_path: str = get_system_dbt(),
dbt_cmd_flags: list[str] | None = None,
dbt_cmd_global_flags: list[str] | None = None,
extra_context: dict[str, Any] | None = None,
**kwargs: Any,
) -> None:
self.project_dir = project_dir
Expand Down Expand Up @@ -132,6 +134,7 @@ def __init__(
self.dbt_executable_path = dbt_executable_path
self.dbt_cmd_flags = dbt_cmd_flags
self.dbt_cmd_global_flags = dbt_cmd_global_flags or []
self.extra_context = extra_context or {}
super().__init__(**kwargs)

def get_env(self, context: Context) -> dict[str, str | bytes | os.PathLike[Any]]:
Expand Down Expand Up @@ -231,3 +234,9 @@ def build_cmd(
env = self.get_env(context)

return dbt_cmd, env

def pre_execute(self, context: Any) -> None:
if self.extra_context:
logger.info("Extra context passed to operator, injecting into TaskInstance")
context["model_config"] = self.extra_context
return super().pre_execute(context)
4 changes: 1 addition & 3 deletions cosmos/operators/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,7 @@ def run_command(
)
if is_openlineage_available:
self.calculate_openlineage_events_completes(env, Path(tmp_project_dir))
context[
"task_instance"
].openlineage_events_completes = self.openlineage_events_completes # type: ignore
context["task_instance"].openlineage_events_completes = self.openlineage_events_completes # type: ignore

if self.emit_datasets:
inlets = self.get_datasets("inputs")
Expand Down
Loading