Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic context via prefect.runtime #8790

Merged
merged 17 commits into from Mar 14, 2023
Merged

Dynamic context via prefect.runtime #8790

merged 17 commits into from Mar 14, 2023

Conversation

cicdw
Copy link
Member

@cicdw cicdw commented Mar 14, 2023

Many users rely on various pieces of run / deployment configuration for dynamically specifying pieces of code external to a flow function; for example, one might want to configure a task runner based on either tags or parameters passed to a deployment run.

Today, users must query the API themselves for information outside of a flow function; within a flow / task function, they can use get_run_context but that is intended for internal purposes more than as a user facing entity (there are many attributes here that would be confusing for users to access).

This PR introduces a new submodule, prefect.runtime, for accessing such information in an intuitive and graceful way - when accessed outside of a known context, all attributes return empty values. This submodule can easily be expanded upon following the patterns developed here in ways that are memory efficient and independent of implementation details of the engine. Note that the attribute access implemented here was inspired by PEP 562.

Example

Imagine using a RayTaskRunner and wanting to tag the Ray cluster based on customer name, which is also a parameter to your flow:

from prefect.runtime import deployment

customer_tag = deployment.parameters.get("customer_name", "dev")

@flow(
    task_runner=RayTaskRunner(init_kwargs={
        "customer_tag": customer_tag
    })
)
def main(*, customer_name: str):
    # [...]

Previously this was not achievable in a first class way.

Checklist

  • This pull request references any related issue by including "closes <link to issue>"
    • If no issue exists and your change is not a small fix, please create an issue first.
  • This pull request includes tests or only affects documentation.
  • This pull request includes a label categorizing the change e.g. fix, feature, enhancement

@cicdw cicdw added the feature A new feature label Mar 14, 2023
@cicdw cicdw requested a review from a team as a code owner March 14, 2023 02:11
@netlify
Copy link

netlify bot commented Mar 14, 2023

Deploy Preview for prefect-docs ready!

Name Link
🔨 Latest commit 4f0fc43
🔍 Latest deploy log https://app.netlify.com/sites/prefect-docs/deploys/6410913b20a02c000827b0ae
😎 Deploy Preview https://deploy-preview-8790--prefect-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

src/prefect/runtime/deployment.py Dismissed Show dismissed Hide dismissed
src/prefect/runtime/deployment.py Dismissed Show dismissed Hide dismissed
src/prefect/runtime/deployment.py Dismissed Show dismissed Hide dismissed
src/prefect/runtime/flow_run.py Dismissed Show dismissed Hide dismissed
src/prefect/runtime/flow_run.py Dismissed Show dismissed Hide dismissed
src/prefect/runtime/deployment.py Fixed Show fixed Hide fixed
Copy link
Member

@desertaxle desertaxle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks cool! Just a few small suggestions/questions from me

src/prefect/runtime/deployment.py Outdated Show resolved Hide resolved
src/prefect/runtime/deployment.py Outdated Show resolved Hide resolved
src/prefect/runtime/deployment.py Outdated Show resolved Hide resolved
src/prefect/runtime/deployment.py Show resolved Hide resolved
cicdw and others added 3 commits March 14, 2023 07:00
re-use get flow run ID method

Co-authored-by: Alexander Streed <desertaxle@users.noreply.github.com>
@cicdw cicdw requested a review from a team as a code owner March 14, 2023 14:32
Copy link
Member

@desertaxle desertaxle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Copy link
Contributor

@billpalombi billpalombi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm so psyched that you knocked this out so quickly. Thanks for updating the API reference. We'll want other docs on how to use this. I'll start thinking about that today.

@cicdw cicdw merged commit 5afa02d into main Mar 14, 2023
40 checks passed
@cicdw cicdw deleted the dynamic-context branch March 14, 2023 19:36
@jbnitorum
Copy link

This is a much more elegant solution than get_run_context. Can I suggest adding more available attributes? Specifically flow_name, flow_run_name, and deployment_name. Obviously we can get these using the API once we have the already available IDs but it would be much more straight forward if they were available as attributes.

For people transitioning from v1 (like us) it would make things easier if the v1 context items (https://docs-v1.prefect.io/api/latest/utilities/context.html#context-2) were all available as attributes in prefect.runtime in V2. Or at least all the ones that are still applicable in v2.

@zanieb
Copy link
Contributor

zanieb commented Mar 20, 2023

@jbnitorum yep we will continue to add attributes following this first pass; if you want to open an issue it might help facilitate other contributions.

@sckott
Copy link

sckott commented Mar 21, 2023

+1 on flow_name

@zanieb
Copy link
Contributor

zanieb commented Mar 21, 2023

#8864 adds some more attributes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature A new feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants