Skip to content

Commit

Permalink
Split out actual execution of hook into private function to simplify …
Browse files Browse the repository at this point in the history
…`safe_run_hooks`
  • Loading branch information
QMalcolm committed Oct 30, 2024
1 parent 43d1720 commit 9731eaa
Showing 1 changed file with 44 additions and 29 deletions.
73 changes: 44 additions & 29 deletions core/dbt/task/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,46 @@ def get_hooks_by_type(self, hook_type: RunHookType) -> List[HookNode]:
hooks.sort(key=self._hook_keyfunc)
return hooks

def _safe_run_hook(
self,
adapter: BaseAdapter,
hook: HookNode,
hook_name: str,
timing: List[TimingInfo],
num_hooks: int,
extra_context: Dict[str, Any],
) -> Tuple[RunStatus, str, float]:
with collect_timing_info("compile", timing.append):
sql = self.get_hook_sql(adapter, hook, hook.index, num_hooks, extra_context)

started_at = timing[0].started_at or datetime.utcnow()
hook.update_event_status(

Check warning on line 660 in core/dbt/task/run.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/task/run.py#L660

Added line #L660 was not covered by tests
started_at=started_at.isoformat(), node_status=RunningStatus.Started
)

fire_event(

Check warning on line 664 in core/dbt/task/run.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/task/run.py#L664

Added line #L664 was not covered by tests
LogHookStartLine(
statement=hook_name,
index=hook.index,
total=num_hooks,
node_info=hook.node_info,
)
)

with collect_timing_info("execute", timing.append):
status, message = get_execution_status(sql, adapter)

Check warning on line 674 in core/dbt/task/run.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/task/run.py#L673-L674

Added lines #L673 - L674 were not covered by tests

finished_at = timing[1].completed_at or datetime.utcnow()
hook.update_event_status(finished_at=finished_at.isoformat())
execution_time = (finished_at - started_at).total_seconds()

Check warning on line 678 in core/dbt/task/run.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/task/run.py#L676-L678

Added lines #L676 - L678 were not covered by tests

if status == RunStatus.Success:
message = f"{hook_name} passed"

Check warning on line 681 in core/dbt/task/run.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/task/run.py#L680-L681

Added lines #L680 - L681 were not covered by tests
else:
message = f"{hook_name} failed, error:\n {message}"

Check warning on line 683 in core/dbt/task/run.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/task/run.py#L683

Added line #L683 was not covered by tests

return (status, message, execution_time)

Check warning on line 685 in core/dbt/task/run.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/task/run.py#L685

Added line #L685 was not covered by tests

def safe_run_hooks(
self,
adapter: BaseAdapter,
Expand All @@ -669,37 +709,12 @@ def safe_run_hooks(
execution_time = 0.0
timing: List[TimingInfo] = []

# Only run this hook if the previous hook succeeded
# otherwise, skip it
if status == RunStatus.Success:
with collect_timing_info("compile", timing.append):
sql = self.get_hook_sql(
adapter, hook, hook.index, num_hooks, extra_context
)

started_at = timing[0].started_at or datetime.utcnow()
hook.update_event_status(
started_at=started_at.isoformat(), node_status=RunningStatus.Started
(status, message, execution_time) = self._safe_run_hook(
adapter, hook, hook_name, timing, num_hooks, extra_context
)

fire_event(
LogHookStartLine(
statement=hook_name,
index=hook.index,
total=num_hooks,
node_info=hook.node_info,
)
)

with collect_timing_info("execute", timing.append):
status, message = get_execution_status(sql, adapter)

finished_at = timing[1].completed_at or datetime.utcnow()
hook.update_event_status(finished_at=finished_at.isoformat())
execution_time = (finished_at - started_at).total_seconds()

if status == RunStatus.Success:
message = f"{hook_name} passed"
else:
message = f"{hook_name} failed, error:\n {message}"
else:
status = RunStatus.Skipped
message = f"{hook_name} skipped"

Check warning on line 720 in core/dbt/task/run.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/task/run.py#L719-L720

Added lines #L719 - L720 were not covered by tests
Expand Down

0 comments on commit 9731eaa

Please sign in to comment.