The flow run name can not be set in advance, but it can be changed using the RenameFlowRun task after the flow run has been created.
You can use this task inside the Flow block or through a flow-level state handler. When calling the task from a state handler, make sure to call the task’s .run() method.
Here is a flow-level state handler example:
from prefect import Flow, task
from prefect.tasks.prefect import RenameFlowRun
def rename_handler(flow, new_state, old_state):
if new_state.is_running():
RenameFlowRun().run(flow_run_name="new_name")
return
@task
def first_task():
return 1
with Flow("test-flow", state_handlers=[rename_handler]) as flow:
first_task()
Thanks, @Nikita_Samoylov! It actually doesn’t matter because regardless of whether the obj is a Flow or Task object, it will be passed to the state handler, depending on whether you attach the state handler to a task or a flow. But I agree with you that renaming it to flow makes it more explicitly, so I’ll change it.
How would you do this in Prefect 2? I would like to overwrite the randomly generated combinations but there doesnt seem to be a RenameFlowRun equivalent in 2.0?