Hello, I'm trying to create a completion hook for ...
# ask-community
y
Hello, I'm trying to create a completion hook for a flow. The flow returns some results. Is there any way we can pass these results into that completion hook? Here's my code structure:
Copy code
def post_hook_save_to_db(flow, flow_run, state):
    print(state)
    print("This is flow")
    print(flow.name) --this would print "test"
    print(flow.result)   --this would fail. How can I update that to print something as below
    # do something
@flow (on_completion=[post_hook_save_to_db], name = "test",persist_result=True)
def test():
    return something
1
j
hey the right way to do this is something like:
Copy code
from prefect import flow
from prefect.runtime import flow_run

def on_completion(flow, flow_run, state):
    print(f"My flow finished with: {state.result()}")

@flow(log_prints=True, on_completion=[on_completion])
def my_flow():
    print(f"I'm in flow: {flow_run.name}")
    return 42
which gives me
Copy code
19:43:24.604 | INFO    | prefect.engine - Created flow run 'curious-auk' for flow 'my-flow'
19:43:24.606 | INFO    | Flow run 'curious-auk' - View at <https://app.prefect.cloud/.../>
19:43:24.895 | INFO    | Flow run 'curious-auk' - I'm in flow: curious-auk
19:43:26.293 | INFO    | Flow run 'curious-auk' - Running hook 'on_completion' in response to entering state 'Completed'
19:43:26.299 | INFO    | Flow run 'curious-auk' - Hook 'on_completion' finished running successfully
19:43:26.301 | INFO    | Flow run 'curious-auk' - Finished in state Completed()
My flow finished with: 42
y
Thank you Jake! Is there any reason why when we pull `flow_run.dict()`from the
on_completion
func in the above, the
end_time
is
None
and the
'state_type'
is
StateType.RUNNING
,
'state_name'
is
'Running'
, but when we get the
state
from the
on_completion
func from above, it is
Completed()
?