Compare commits

...

6 Commits

Author SHA1 Message Date
Quigley Malcolm
6358aff386 Fix indentation of _safe_run_hook
In 9731eaa when splitting out the hook execution logic I screwed up
the indentation of most of the logic 🙈 This fixes that
2024-10-30 14:23:21 -05:00
Quigley Malcolm
9731eaaeb0 Split out actual execution of hook into private function to simplify safe_run_hooks 2024-10-30 11:03:01 -05:00
Quigley Malcolm
43d1720986 Refactor safe_run_hooks so that there is only one return statement 2024-10-30 10:54:21 -05:00
Quigley Malcolm
de43f599c0 Lint safe_run_hooks function signature 2024-10-30 10:49:41 -05:00
Quigley Malcolm
43945d68a1 Delay calculation of failures in safe_run_hooks to simplify code
If it isn't apparent, I'm trying to reduce the need for tracking variables
to make it easier extract the execution code to a separate private function
to make it easier to see what is happening.
2024-10-30 10:46:52 -05:00
Quigley Malcolm
c63dd9d294 Remove failed tracking in safe_run_hooks as it's duplicative of status 2024-10-30 10:46:37 -05:00

View File

@@ -644,36 +644,17 @@ class RunTask(CompileTask):
hooks.sort(key=self._hook_keyfunc)
return hooks
def safe_run_hooks(
self, adapter: BaseAdapter, hook_type: RunHookType, extra_context: Dict[str, Any]
) -> RunStatus:
ordered_hooks = self.get_hooks_by_type(hook_type)
if hook_type == RunHookType.End and ordered_hooks:
fire_event(Formatting(""))
# on-run-* hooks should run outside a transaction. This happens because psycopg2 automatically begins a transaction when a connection is created.
adapter.clear_transaction()
if not ordered_hooks:
return RunStatus.Success
status = RunStatus.Success
failed = False
num_hooks = len(ordered_hooks)
for idx, hook in enumerate(ordered_hooks, 1):
with log_contextvars(node_info=hook.node_info):
hook.index = idx
hook_name = f"{hook.package_name}.{hook_type}.{hook.index - 1}"
execution_time = 0.0
timing: List[TimingInfo] = []
failures = 1
if not failed:
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
)
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(
@@ -695,13 +676,45 @@ class RunTask(CompileTask):
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()
failures = 0 if status == RunStatus.Success else 1
if status == RunStatus.Success:
message = f"{hook_name} passed"
else:
message = f"{hook_name} failed, error:\n {message}"
failed = True
return (status, message, execution_time)
def safe_run_hooks(
self,
adapter: BaseAdapter,
hook_type: RunHookType,
extra_context: Dict[str, Any],
) -> RunStatus:
ordered_hooks = self.get_hooks_by_type(hook_type)
status = RunStatus.Success
if ordered_hooks:
if hook_type == RunHookType.End:
fire_event(Formatting(""))
# on-run-* hooks should run outside a transaction. This happens because psycopg2 automatically begins a transaction when a connection is created.
adapter.clear_transaction()
num_hooks = len(ordered_hooks)
for idx, hook in enumerate(ordered_hooks, 1):
with log_contextvars(node_info=hook.node_info):
hook.index = idx
hook_name = f"{hook.package_name}.{hook_type}.{hook.index - 1}"
execution_time = 0.0
timing: List[TimingInfo] = []
# Only run this hook if the previous hook succeeded
# otherwise, skip it
if status == RunStatus.Success:
(status, message, execution_time) = self._safe_run_hook(
adapter, hook, hook_name, timing, num_hooks, extra_context
)
else:
status = RunStatus.Skipped
message = f"{hook_name} skipped"
@@ -716,7 +729,7 @@ class RunTask(CompileTask):
message=message,
adapter_response={},
execution_time=execution_time,
failures=failures,
failures=0 if status == RunStatus.Success else 1,
node=hook,
)
)
@@ -732,7 +745,7 @@ class RunTask(CompileTask):
)
)
if hook_type == RunHookType.Start and ordered_hooks:
if hook_type == RunHookType.Start:
fire_event(Formatting(""))
return status