Sophia Ponte
07/18/2024, 3:36 AMMarvin
07/18/2024, 3:36 AMpython
from prefect import flow
@flow
def fail_with_exception():
raise ValueError("This flow immediately fails")
if __name__ == "__main__":
fail_with_exception()
2. Return a Manual State: You can return a manually created state to set the final state of the flow explicitly. Below is an example of this approach:
python
from prefect import flow
from prefect.states import Failed
@flow
def fail_manually():
print("This flow is meant to fail")
return Failed(message="Manual failure")
if __name__ == "__main__":
fail_manually()
### Links and Resources:
1. Prefect Flow Concepts: https://docs.prefect.io/latest/concepts/flows/
2. Code Example Source: https://raw.githubusercontent.com/zzstoatzz/prefect-code-examples/main/examples/flows/basic.py
Using either of these approaches, you can ensure your flow will fail while returning None or raising an appropriate exception.Sophia Ponte
07/18/2024, 3:37 AMMarvin
07/18/2024, 3:37 AMFailed
state:
python
from prefect import flow
from prefect.states import State, Failed
@flow
def return_true_but_fail():
# Your logic here
result = True
# Manually set the flow state to failed even though returning True
return State(result=result, state=Failed(message="Manual failure"))
if __name__ == "__main__":
return_true_but_fail()
This approach ensures that the flow will return True as a result, but the overall state of the flow will be set to Failed manually.
### Links and Resources:
1. Prefect Flow Concepts: https://docs.prefect.io/latest/concepts/flows/
2. Example Code Source: https://raw.githubusercontent.com/zzstoatzz/prefect-code-examples/main/examples/flows/basic.py
This should give you the desired behavior of having the flow appear as failed while returning True.Nate
07/18/2024, 4:07 AMFailed
via API in an on_completion
hook after return
ing True
or None
, but I probably wouldn't) to do this. what's the use case / what situation are you expressing when doing this?
----
fwiw dont recommend but
In [5]: from prefect import get_client
...: from prefect.states import Failed
...:
...: async def set_failed(flow, flow_run, state):
...: async with get_client() as client:
...: print(await client.set_flow_run_state(flow_run.id, Failed()))
...:
...: @flow(on_completion=[set_failed])
...: def f():
...: return None
...:
In [6]: f()
23:11:26.732 | INFO | prefect.engine - Created flow run 'adventurous-slug' for flow 'f'
23:11:27.059 | INFO | Flow run 'adventurous-slug' - Running hook 'set_failed' in response to entering state 'Completed'
state=Failed(message=None, type=FAILED, result=None) status=SetStateStatus.ACCEPT details=StateAcceptDetails(type='accept_details')
23:11:27.245 | INFO | Flow run 'adventurous-slug' - Hook 'set_failed' finished running successfully
23:11:27.246 | INFO | Flow run 'adventurous-slug' - Finished in state Completed()
Nate
07/18/2024, 4:08 AMCompleted
Sophia Ponte
07/19/2024, 11:48 PMNate
07/20/2024, 2:53 AMSophia Ponte
07/20/2024, 2:54 AM