66from datetime import timedelta
77from importlib .metadata import version
88from typing import Iterable , List , Optional
9-
109from packaging .specifiers import SpecifierSet
11- from prefect import Flow , get_client , serve
10+ from prefect import Flow , serve
1211from prefect .blocks .notifications import MicrosoftTeamsWebhook
1312from prefect .client .schemas .objects import FlowRun
1413from prefect .settings import PREFECT_API_URL
1514from prefect .states import State
1615from 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 ---------------------
2230class 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]:
8896class 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
171172def 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