https://prefect.io logo
Title
c

Cristian Toma

02/03/2022, 2:47 PM
Hi, Is there a way to append a string to a flow run name?
k

Kevin Kho

02/03/2022, 2:50 PM
You can use the RenameFlowRun task and get the current one with
prefect.context.get("flow_run_name")
c

Cristian Toma

02/03/2022, 4:22 PM
prefect.context.get("flow_run_name")
can run only inside a task, right? So I need to create a task X and inside of it return the flow name and after in the with Flow section to pass it to the RenameFlowRun task.
k

Kevin Kho

02/03/2022, 4:26 PM
Yes and no. Yes it runs in a task, but you can use it in state handlers so you can have the rename run before the Flow begins. Code below
from prefect import Flow, task
from prefect.tasks.prefect import RenameFlowRun

def rename_handler(obj, 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()