andor
November 7, 2022, 3:39pm
1
I am just starting to get acquainted with Prefect 2, but I could not really track what is happening, because coolname s are not very informative, just a bunch of random strings. I prefer to have names including a characteristic parameter (e.g. name of processed item) and a sequence number and/or some kind of date-time, whichever makes it unique.
Prefect 1.x has RenameFlowRun
and task run names could be templated with task_run_name
. Is there anything in Prefect 2.x that can be used to achieve the same easily?
Most of the time, I have to process collection of items (queries, files, urls, etc.), which are dynamically generated. I guess mapping was made for these, but it seems that there is no way to differentiate among the items in their naming. Is it so, or am I using it wrong?
This issue was closed because technically this is already possible:
opened 11:02AM - 06 Jul 22 UTC
closed 12:35AM - 11 Oct 22 UTC
docs
from:slack
v2
status:roadmap
priority:high
v1-parity
## Opened from the [Prefect Public Slack Community](https://prefect.io/slack)
*… *andreas.ntonas**: Hi! In Prefect 2.0, when creating a flow_run for a deployment using <https://orion-docs.prefect.io/api-ref/prefect/client/#prefect.client.OrionClient.create_flow_run_from_deployment|API's> `prefect.client.create_flow_run_from_deployment()` is it possible to set the name for the flow_run? I know that there is such an option when calling `create_flow_run(name="My flow run name")` that takes as input a flow_model, what about when creating a run from a deployment though?
**anna**: In theory, it is possible but we currently don't expose any functionality to do that. Let me open an issue to check if we can expose that
<@ULVA73B9P> open "Allow setting custom flow run name when creating a flow run from a deployment"
Original thread can be found [here](https://prefect-community.slack.com/archives/CL09KU1K7/p1657103899443469?thread_ts=1657103899.443469&cid=CL09KU1K7).
if this doesn’t satisfy your use case, do you want to open a feature request on GitHub?
andor
November 7, 2022, 5:16pm
3
Thanks for your reply Anna!
It seems then, that there’s no easy way. I am going to open a feature request as you’ve suggested.
Also, I’ve tried to use update_flow_run
, but no use. It looks like that it does not affect the running flow.
In the code below, set_flowrun_name
task should rename the flow run to “whatever”, isn’t it?
from prefect import flow, task, context
from prefect.orion.schemas.actions import FlowRunUpdate
from prefect.orion.api.flow_runs import update_flow_run
from pprint import pprint
@task
def get_names():
return ["Amber", "Barbara", "Charlie"]
@task(tags=['max_two_tasks'])
def say_hello(name):
print(f"Hello {name}!")
@task
def dump_ctx():
ctx = context.get_run_context()
pprint(ctx)
@task
def set_flowrun_name():
ctx = context.get_run_context()
update_flow_run(FlowRunUpdate(name='whatever'), ctx.task_run.flow_run_id)
@flow(name="hello_name")
def hello_flow():
flow_run_name = set_flowrun_name()
names = get_names()
hellos = [say_hello.with_options(name=f'hello-{name}')(name) for name in names]
dump_ctx()
if __name__ == '__main__':
hello_flow()
1 Like
I’m not sure whether it’s that easy to do it client-side - I suggest opening an issue and putting all the relevant information there, and then linking the issue here for posterity to follow up
Thanks so much!
ssilent
November 16, 2022, 4:09pm
5
Thank you for your code andor.
Here is the solution:
from prefect.orion.database.dependencies import provide_database_interface
@task
def set_flowrun_name():
ctx = context.get_run_context()
update_flow_run(FlowRunUpdate(name='whatever'), ctx.task_run.flow_run_id, provide_database_interface())
andor
November 17, 2022, 12:11pm
6
Great! Thanks @ssilent !
For reference, I have sent a feature request about this, which was added to the roadmap.
opened 06:10PM - 07 Nov 22 UTC
enhancement
status:roadmap
priority:medium
### First check
- [X] I added a descriptive title to this issue.
- [X] I used t… he GitHub search to find a similar request and didn't find it.
- [X] I searched the Prefect documentation for this feature.
### Prefect Version
2.x
### Describe the current behavior
Tasks & flow runs get random names, which makes it hard to track correct behavior, and identify problem sources. Although the metadata of those runs contain the required information (e.g. name of processed item, time of execution, sequence number), those should be checked out separately.
### Describe the proposed behavior
With Prefect v1 one could use `RenameFlowRun` and `task_run_name` to rename a flow or task run by a template. Prefect v2 provides `update_flow_run` for the former, but it's usage is quite complicated for this basic task. Also, for tasks `.with_options(name=...)` could not be applied when mapping is used.
I propose to have `task_run_name` again for tasks, and `flow_run_name` for flows, which would define a naming template for the respective entities.
### Example Use
Processing collections of items could be a frequent use case. Usually each item has an own name or identifier. For example a directory of files, a list of queries, a batch of urls, a set of clients read from a database, where the list of items to be processed is not known in advance. A natural way of processing these would be to use mapping or a loop, but random (cool)names doesn't help to know what's going on, but a `task_run_name` template could.
Flow runs may be bound to a period of time (e.g. Monday, August, 45th week), or a sequence number (1,2,3,4,5...). If there was a problem, and those values could show up in the name, then it would be immediately obvious what was affected. A `flow_run_name` parameter of the flow containing a naming template could help with this.
### Additional context
_No response_
1 Like