Ofek K
08/21/2024, 2:15 PMMarvin
08/21/2024, 2:16 PMCrashed
or Failed
state and handle the exit actions within the hook.
Here’s an example of how you might implement such a hook:
import os
from prefect import flow, task
from prefect.client import get_client
import prefect.runtime
async def handle_flow_crashed(flow, flow_run, state):
"""
State change hook that is triggered when the flow enters a crashed state.
This can be used to log information, clean up resources, or set an exit code.
"""
# Implement your logic here
print(f"Flow {flow_run.flow_id} has crashed. Setting exit code 1.")
os._exit(1) # Immediately terminate the process with exit code 1
@flow(state_handlers=[handle_flow_crashed])
def my_flow():
# Your flow logic here
pass
if __name__ == "__main__":
my_flow()
3. Running Your Flow: Deploy and run your flow. The state change hook will handle setting the exit code when the flow crashes or fails.
### Links to Documentation
1. State Change Hooks Guide
This approach should help you manage your flow runs more gracefully when using spot instances on GCP.
Is there anything else you would like to know or any other problems you are facing with Prefect?