<@ULVA73B9P> I have an on_completion method in my ...
# ask-marvin
b
@Marvin I have an on_completion method in my flow definition which persists my flow_run to an external database. Upon inspecting the results, over half of the flow_runs are being recorded as their state still as
Running
. How can I go about setting the state of my flow_run to be
Completed
in my on_completion method when I check that it is still listed as
Running
?
m
It seems like there's a misunderstanding here. The
on_completion
method is intended to be used for hooks that run when a flow completes, but it doesn't actually set the state of the flow run; it reacts to the flow run reaching a particular state. If over half of your flow runs are being recorded as still in the
Running
state, then they likely haven't actually completed yet, or there's something preventing them from reaching the
Completed
state. However, there is an example from Prefect recipes on GitHub, where an
emit_event
is used to trigger an action when a flow run completes. Perhaps you could look into using events with your on_completion code to respond to completion instead of attempting to set the state directly. Please check out the example to understand how to use
emit_event
on completion: Emit Event on Completion Example Remember, for specific actions to enforce or change the state of a flow run, you'll need to interact with Prefect's API or use some of Prefect's state handling features. If the flow runs aren't completing as expected, you might need to investigate the reason for that. It could be due to tasks hanging, external services not responding, etc. Forcing a state change might mask underlying issues.
👎 1
n
I actually think this is a pretty reasonable answer you should not be in an
on_completion
hook if you did not enter a
Completed
state there's likely something else going on
emit_event is likely not what you're looking for, but forcing an orchestration result from an on completion hook is definitely not what you want either I would want to understand why certain runs are stuck in running, is their infra disappearing or something?
b
I don’t think so. I get a log indicating the flow has entered a Completed state, and that the
on_completion
hook is being called, yet the
flow_run
param inside of the hook has a state still set to
Running
.
n
i'd be interested to see logs from such a situation!
b
Last screenshot is the row that got inserted from this run, with state still set to Running. This is happening in the
persist_flow_run
hook
n
hmm this is pretty strange. would you be able to share whats going on in your hooks?
I'm also a bit confused by this output I assume you have hooks on a subflow or task as well?