-
Couldn't load subscription status.
- Fork 2
feat: add inital deploy and notify teams for prefect including examples #105
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
460efcc
feat: add inital deploy and notify teams for prefect including examples
raederan 1ab7e05
fix: rename script file due to prevent import issues, add local test …
raederan 9381dc3
add add support for python 3.8 pydantic syntax and tests for prefect …
raederan cfa7ad7
fix trigger tests by naming testfile including key test
raederan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| """Example usage of deploy function""" | ||
|
|
||
| from datetime import timedelta | ||
| from os import environ | ||
|
|
||
| from prefect import flow | ||
| from prefect.artifacts import create_table_artifact | ||
|
|
||
| from osw.utils.prefect import DeployConfig, DeployParam, deploy, tagsStrToList | ||
|
|
||
| # Set environment variables | ||
| environ["PREFECT_DEPLOYMENT_NAME"] = "osw-python-deploy-example" | ||
| environ["PREFECT_DEPLOYMENT_DESCRIPTION"] = "Deployment of notify_teams.py" | ||
| environ["PREFECT_DEPLOYMENT_VERSION"] = "0.0.1" | ||
| environ["PREFECT_DEPLOYMENT_TAGS"] = "osw-python,example-deploy-flow" | ||
| environ["PREFECT_DEPLOYMENT_INTERVAL_MIN"] = "1" | ||
| # environ["PREFECT_DEPLOYMENT_CRON"] = "* * * * *" | ||
|
|
||
|
|
||
| @flow(log_prints=True) | ||
| def example_flow_to_deploy(): | ||
| """Example flow to be deployed""" | ||
| print(f"Execution of example: {example_flow_to_deploy.__name__}!") | ||
| # set example table artifact | ||
| create_table_artifact( | ||
| key="example-table", | ||
| table=[ | ||
| {"name": "Alice", "age": 24}, | ||
| {"name": "Bob", "age": 25}, | ||
| ], | ||
| description="Example table artifact", | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| """Deploy the example flow""" | ||
| # Example using environment variables | ||
| deploy( | ||
| DeployParam( | ||
| deployments=[ | ||
| DeployConfig( | ||
| flow=example_flow_to_deploy, | ||
| name=environ.get("PREFECT_DEPLOYMENT_NAME"), | ||
| description=environ.get("PREFECT_DEPLOYMENT_DESCRIPTION"), | ||
| version=environ.get("PREFECT_DEPLOYMENT_VERSION"), | ||
| tags=tagsStrToList(environ.get("PREFECT_DEPLOYMENT_TAGS")), | ||
| interval=timedelta( | ||
| minutes=int(environ.get("PREFECT_DEPLOYMENT_INTERVAL_MIN")) | ||
| ), # either interval or cron | ||
| # cron=environ.get("PREFECT_DEPLOYMENT_CRON"), | ||
| ) | ||
| ], | ||
| # remove_existing_deployments=True, | ||
| ) | ||
| ) | ||
|
|
||
| # Clear secret environment variables | ||
| del environ["PREFECT_DEPLOYMENT_NAME"] | ||
| del environ["PREFECT_DEPLOYMENT_DESCRIPTION"] | ||
| del environ["PREFECT_DEPLOYMENT_VERSION"] | ||
| del environ["PREFECT_DEPLOYMENT_TAGS"] | ||
| del environ["PREFECT_DEPLOYMENT_INTERVAL_MIN"] | ||
| # del environ["PREFECT_DEPLOYMENT_CRON"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| """Example of sending notifications to MS Teams on prefect flow failures""" | ||
|
|
||
| from os import environ | ||
|
|
||
| from prefect import flow | ||
| from pydantic import SecretStr | ||
|
|
||
| from osw.utils.prefect import NotifyTeams, NotifyTeamsParam | ||
|
|
||
| # Prerequisite: Set environment variable TEAMS_WEBHOOK_URL | ||
| # in CLI: export TEAMS_WEBHOOK_URL="https://prod..." | ||
| # in python uncomment below, DO NOT PUSH SECRETS TO GIT | ||
|
|
||
| # environ["TEAMS_WEBHOOK_URL"] = "https://prod..." | ||
|
|
||
|
|
||
| # Decorator must be configured with on_failure argument | ||
| @flow( | ||
| # Microsoft Teams notification on failure -> | ||
| # on_failure use `notify_teams` function without brackets as list element | ||
| on_failure=[ | ||
| NotifyTeams( | ||
| NotifyTeamsParam( | ||
| teams_webhook_url=SecretStr(environ.get("TEAMS_WEBHOOK_URL")), | ||
| # OPTIONAL, will be empty if no deploment is assigned | ||
| deployment_name="osw-python-notify-teams-example", | ||
| ) | ||
| ).notify_teams | ||
| ], | ||
| log_prints=True, | ||
| ) | ||
| def example_error_flow(): | ||
| """Test flow that always fails""" | ||
|
|
||
| raise ValueError( | ||
| "oops! LOREM IPSUM DOLOR SIT AMET CONSECTETUR ADIPISICING ELIT " * 1 | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| example_error_flow() | ||
| # Clear secret environment variable | ||
| del environ["TEAMS_WEBHOOK_URL"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This example should also be used as a pytest