1010from typing import Any
1111
1212
13+ def run (
14+ func_path : str | None = None ,
15+ args : list | None = None ,
16+ kwargs : dict [str , Any ] | None = None ,
17+ ) -> dict [str , str ]:
18+ mode = os .environ .get ("MODE" , "dev" )
19+ print (f"## Runner running in { mode } mode ##" )
20+ func_path , args , kwargs = _resolve_inputs (func_path , args , kwargs , mode )
21+
22+ if not func_path :
23+ raise ValueError ("func_path must be provided" )
24+
25+ func = _import_function (func_path )
26+
27+ print (f"Running { func_path } with args: { args } and kwargs :{ kwargs } " )
28+ result = func (* args , ** kwargs )
29+ print ("Function result:" , result )
30+
31+ _write_result (result , mode )
32+
33+ return result
34+
35+
1336def _extract_params_from_env (prefix = "PARAMS_" ) -> dict [str , str ]:
1437 return {
1538 k [len (prefix ) :].lower (): v
@@ -18,47 +41,32 @@ def _extract_params_from_env(prefix="PARAMS_") -> dict[str, str]:
1841 }
1942
2043
21- def run (
22- func_path : str | None = None ,
23- args : list | None = None ,
24- kwargs : dict [str , Any ] | None = None ,
25- ) -> dict [str , str ]:
26- mode = os .environ .get ("MODE" , "dev" )
27- print (f"## Runner running in { mode } mode ##" )
44+ def _resolve_inputs (func_path : str , args : list [Any ], kwargs : dict [Any ], mode : str ):
2845 if mode == "dev" :
29- print ("args" , args )
30- print ("kwargs" , kwargs )
31- else :
32- func_path = os .environ .get ("FUNC_PATH" , "" )
46+ return func_path , args or [], kwargs or {}
47+ else : # all other modes (dev_docker, prod_local and prod)
48+ func_path = os .environ .get ("FUNC_PATH" , func_path )
3349 args = json .loads (os .environ .get ("FUNC_ARGS" , "[]" ))
3450 kwargs = json .loads (os .environ .get ("FUNC_KWARGS" , "{}" ))
35- params : dict = _extract_params_from_env ()
36- kwargs ["params" ] = params
37- print ("args" , args )
38- print ("kwargs" , kwargs )
51+ kwargs ["params" ] = _extract_params_from_env ()
52+ return func_path , args , kwargs
3953
40- if not func_path :
41- raise ValueError ("func_path must be provided" )
4254
43- module_path , func_name = func_path . rsplit ( ":" , 1 )
55+ def _import_function ( func_path : str ):
4456 import importlib
4557
58+ module_path , func_name = func_path .rsplit (":" , 1 )
4659 module = importlib .import_module (module_path )
47- func = getattr (module , func_name )
60+ return getattr (module , func_name )
4861
49- print (f"Running { func_path } with args: { args } and kwargs :{ kwargs } " )
50- result = func (* args , ** kwargs )
51- print ("Function result:" , result )
62+
63+ def _write_result (result , mode ):
5264 if mode == "prod" or mode == "prod_local" :
53- # This is needed when we use KubernetesPodOperator and want to
54- # share information via XCOM.
5565 _write_xcom_result (result )
5666 if mode == "dev_docker" :
5767 with open ("/tmp/script.out" , "wb+" ) as tmp :
5868 pickle .dump (result , tmp )
5969
60- return result
61-
6270
6371def _write_xcom_result (result : Any ) -> None :
6472 try :
@@ -68,12 +76,6 @@ def _write_xcom_result(result: Any) -> None:
6876 with open (f"{ xcom_dir } /return.json" , "w" ) as f :
6977 json .dump (result , f )
7078
71- path = "/airflow/xcom/return.json"
72- print ("[DEBUG] File exists:" , os .path .exists (path ))
73- print ("[DEBUG] File size:" , os .path .getsize (path ))
74- with open (path , "r" ) as f :
75- print ("[DEBUG] File contents:" , f .read ())
76-
7779 print ("Result written to XCom successfully" )
7880 except Exception as e :
7981 print (f"Failed to write XCom result: { e } " )
0 commit comments