Skip to content

Commit

Permalink
Run flow hooks in flow run context (#14654)
Browse files Browse the repository at this point in the history
  • Loading branch information
zzstoatzz authored Jul 17, 2024
1 parent 8cc374a commit 6604ec9
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
12 changes: 5 additions & 7 deletions src/prefect/flow_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from typing import (
Any,
AsyncGenerator,
Callable,
Coroutine,
Dict,
Generator,
Expand Down Expand Up @@ -415,7 +414,7 @@ def create_flow_run(self, client: SyncPrefectClient) -> FlowRun:

return flow_run

def call_hooks(self, state: Optional[State] = None) -> Iterable[Callable]:
def call_hooks(self, state: Optional[State] = None):
if state is None:
state = self.state
flow = self.flow
Expand Down Expand Up @@ -613,11 +612,7 @@ def start(self) -> Generator[None, None, None]:

if self.state.is_running():
self.call_hooks()
try:
yield
finally:
if self.state.is_final() or self.state.is_cancelling():
self.call_hooks()
yield

@contextmanager
def run_context(self):
Expand All @@ -638,6 +633,9 @@ def run_context(self):
except Exception as exc:
self.logger.exception("Encountered exception during execution: %r", exc)
self.handle_exception(exc)
finally:
if self.state.is_final() or self.state.is_cancelling():
self.call_hooks()

def call_flow_fn(self) -> Union[R, Coroutine[Any, Any, R]]:
"""
Expand Down
36 changes: 36 additions & 0 deletions tests/test_flows.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
IntervalSchedule,
RRuleSchedule,
)
from prefect.context import FlowRunContext, get_run_context
from prefect.deployments.runner import RunnerDeployment
from prefect.docker.docker_image import DockerImage
from prefect.events import DeploymentEventTrigger, Posture
Expand All @@ -40,6 +41,7 @@
ParameterTypeError,
ReservedArgumentError,
ScriptError,
UnfinishedRun,
)
from prefect.filesystems import LocalFileSystem
from prefect.flows import (
Expand All @@ -60,6 +62,7 @@
)
from prefect.states import (
Cancelled,
Cancelling,
Paused,
PausedRun,
State,
Expand Down Expand Up @@ -2809,6 +2812,39 @@ async def my_hook(flow, flow_run, state):
return my_hook


class TestFlowHooksContext:
@pytest.mark.parametrize(
"hook_type, fn_body, expected_exc",
[
("on_completion", lambda: None, None),
("on_failure", lambda: 100 / 0, ZeroDivisionError),
("on_cancellation", lambda: Cancelling(), UnfinishedRun),
],
)
def test_hooks_are_called_within_flow_run_context(
self, caplog, hook_type, fn_body, expected_exc
):
def hook(flow, flow_run, state):
ctx: FlowRunContext = get_run_context() # type: ignore
assert ctx is not None
assert ctx.flow_run and ctx.flow_run == flow_run
assert ctx.flow_run.state == state
assert ctx.flow == flow

@flow(**{hook_type: [hook]}) # type: ignore
def foo_flow():
return fn_body()

with caplog.at_level("INFO"):
if expected_exc:
with pytest.raises(expected_exc):
foo_flow()
else:
foo_flow()

assert "Hook 'hook' finished running successfully" in caplog.text


class TestFlowHooksWithKwargs:
def test_hook_with_extra_default_arg(self):
data = {}
Expand Down

0 comments on commit 6604ec9

Please sign in to comment.