Skip to content

Commit 1ab7e05

Browse files
committed
fix: rename script file due to prevent import issues, add local test to be moved to tests
1 parent 460efcc commit 1ab7e05

3 files changed

Lines changed: 52 additions & 30 deletions

File tree

‎examples/prefect/deploy_flow.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from prefect import flow
77
from prefect.artifacts import create_table_artifact
88

9-
from osw.utils.prefect import DeployConfig, DeployParam, deploy, tagsStrToList
9+
from osw.utils.workflow import DeployConfig, DeployParam, deploy, tagsStrToList
1010

1111
# Set environment variables
1212
environ["PREFECT_DEPLOYMENT_NAME"] = "osw-python-deploy-example"
@@ -45,7 +45,9 @@ def example_flow_to_deploy():
4545
version=environ.get("PREFECT_DEPLOYMENT_VERSION"),
4646
tags=tagsStrToList(environ.get("PREFECT_DEPLOYMENT_TAGS")),
4747
interval=timedelta(
48-
minutes=int(environ.get("PREFECT_DEPLOYMENT_INTERVAL_MIN"))
48+
minutes=int(
49+
environ.get("PREFECT_DEPLOYMENT_INTERVAL_MIN")
50+
)
4951
), # either interval or cron
5052
# cron=environ.get("PREFECT_DEPLOYMENT_CRON"),
5153
)

‎examples/prefect/notify_teams.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from prefect import flow
66
from pydantic import SecretStr
77

8-
from osw.utils.prefect import NotifyTeams, NotifyTeamsParam
8+
from osw.utils.workflow import NotifyTeams, NotifyTeamsParam
99

1010
# Prerequisite: Set environment variable TEAMS_WEBHOOK_URL
1111
# in CLI: export TEAMS_WEBHOOK_URL="https://prod..."
Lines changed: 47 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,27 @@
66
from datetime import timedelta
77
from importlib.metadata import version
88
from typing import Iterable, List, Optional
9-
109
from packaging.specifiers import SpecifierSet
11-
from prefect import Flow, get_client, serve
10+
from prefect import Flow, serve
1211
from prefect.blocks.notifications import MicrosoftTeamsWebhook
1312
from prefect.client.schemas.objects import FlowRun
1413
from prefect.settings import PREFECT_API_URL
1514
from prefect.states import State
1615
from pydantic import BaseModel, SecretStr
1716

18-
# from prefect.settings import PREFECT_API_URL
17+
# ------------------------------ TEST ------------------------------
18+
19+
20+
from prefect import flow
21+
22+
23+
@flow
24+
def example_flow_to_deploy():
25+
"""Example flow to be deployed"""
26+
print(f"Execution of example: {example_flow_to_deploy.__name__}!")
1927

2028

21-
# ------------------------------ NOTIFICATIONS ------------------------------
29+
# ------------------------------ NOTIFICATIONS ---------------------
2230
class NotifyTeamsParam(BaseModel):
2331
"""Parameter set for notifying Microsoft Teams using class NotifyTeams"""
2432

@@ -74,9 +82,9 @@ def notify_teams(
7482
# print(f"Flow run name: {flow_run.name}")
7583
# print(f"Flow run ID: {flow_run.id}")
7684

77-
MicrosoftTeamsWebhook(url=self.teams_webhook_url.get_secret_value()).notify(
78-
body=(_flow_run + _deployment + _ts + _tags + _message)
79-
)
85+
MicrosoftTeamsWebhook(
86+
url=self.teams_webhook_url.get_secret_value()
87+
).notify(body=(_flow_run + _deployment + _ts + _tags + _message))
8088

8189

8290
# ------------------------------- DEPLOYMENTS -------------------------------
@@ -88,10 +96,12 @@ def tagsStrToList(tags: str) -> List[str]:
8896
class DeployConfig(BaseModel):
8997
"""Prefect deployment configuration"""
9098

91-
flow: Flow
99+
flow: Flow # to be excluded in `flow.to_deployment()` function
92100
name: str | None = None
93101
description: str | None = None
94-
interval: Iterable[int | float | timedelta] | int | float | timedelta | None = None
102+
interval: (
103+
Iterable[int | float | timedelta] | int | float | timedelta | None
104+
) = None
95105
cron: Iterable[str] | str | None = None
96106
version: str | None = None
97107
tags: List[str] | None = None
@@ -138,6 +148,7 @@ async def _deploy(param: DeployParam):
138148
if deploy_config.name is None or deploy_config.name == "":
139149
deploy_config.name = flow.name + "-deployment"
140150
config = await flow.to_deployment(
151+
# Entpacken und ungleiche Parameter exkludieren (ggf. ext funktion schreiben mit inspect.signature -> fkt + dict input -> dict mit keys der args output)
141152
name=deploy_config.name,
142153
tags=deploy_config.tags,
143154
cron=deploy_config.cron,
@@ -149,33 +160,42 @@ async def _deploy(param: DeployParam):
149160

150161
deployments.append(config)
151162

152-
# fetch flow uuid
153-
async with get_client() as client:
154-
response = await client.read_flow_by_name(flow.name)
155-
print(response.json())
156-
flow_uuid = response.id
157-
print("Flow UUID:", flow_uuid)
158-
159-
# prefect_domain = (
160-
# environ.get("PREFECT_API_URL").split("//")[-1].split("/")[0]
161-
# ) # noqa
162-
# print("Prefect domain:", prefect_domain)
163-
# start agent to serve deployment
164-
# await deploy_config.flow.serve(name=deployment_name)
165163
if version("prefect") in SpecifierSet(">=3.0"):
166-
return deployments
164+
print(f"prefect version IF: {version('prefect')}")
165+
# return deployments
166+
serve(*deployments)
167167
else:
168+
print(f"prefect version ELSE: {version('prefect')}")
168169
await serve(*deployments)
169170

170171

171172
def deploy(param: DeployParam):
172173
"""Function to serve configured flows as deployments by python version."""
173174
if sys.version_info >= (3, 11):
175+
print(f"python version IF: {sys.version_info}")
174176
# python >= 3.11
175177
with asyncio.Runner() as runner:
176-
deployments = runner.run(_deploy(param))
178+
runner.run(_deploy(param))
177179
else:
178180
# python < 3.11
179-
deployments = asyncio.run(_deploy(param))
180-
if version("prefect") in SpecifierSet(">=3.0"):
181-
serve(*deployments)
181+
print(f"python version ELSE: {sys.version_info}")
182+
asyncio.run(_deploy(param))
183+
184+
185+
if __name__ == "__main__":
186+
187+
deploy(
188+
DeployParam(
189+
deployments=[
190+
DeployConfig(
191+
flow=example_flow_to_deploy,
192+
name="osw-python-deploy-example",
193+
description="Deployment of notify_teams.py",
194+
version="0.0.1",
195+
tags=["osw-python", "example-deploy-flow"],
196+
interval=timedelta(seconds=20),
197+
)
198+
],
199+
# remove_existing_deployments=True,
200+
)
201+
)

0 commit comments

Comments
 (0)