55from flask import Flask , request , send_file
66
77from dataclasses import dataclass , field
8- from typing import List
8+ from typing import MutableMapping
99
1010from spawn .eigr .functions .actors .api .actor import Actor
1111from spawn .eigr .functions .actors .internal .controller import ActorController
1212
13- from spawn .eigr .functions .protocol .actors .protocol_pb2 import ActorInvocation , ActorInvocationResponse , Context
1413from google .protobuf .any_pb2 import Any as ProtoAny
1514
1615import io
@@ -27,20 +26,7 @@ def action():
2726 data = request .data
2827 logging .info ('Received Actor action request: %s' , data )
2928
30- # Decode request payload data here and call python real actors methods.
31- databytes = bytes (data )
32- actor_invocation = ActorInvocation ()
33- actor_invocation .ParseFromString (databytes )
34- logging .debug ('Actor invocation data: %s' , actor_invocation )
35-
36- # Update Context
37- updated_context = Context ()
38-
39- # Then send ActorInvocationResponse back to the caller
40- actor_invocation_response = ActorInvocationResponse ()
41- actor_invocation_response .actor_name = actor_invocation .actor_name
42- actor_invocation_response .actor_system = actor_invocation .actor_system
43- actor_invocation_response .updated_context .CopyFrom (updated_context )
29+ actor_invocation_response = controller .handle_invoke (data )
4430
4531 return send_file (
4632 io .BytesIO (actor_invocation_response .SerializeToString ()),
@@ -60,9 +46,13 @@ class Spawn:
6046
6147 __app = None
6248 __controller = None
63- __host = os .environ .get ("HOST" , "0.0.0.0" )
64- __port = os .environ .get ("PORT" , "8091" )
65- __actor_entities : List [Actor ] = field (default_factory = list )
49+ __host = os .environ .get ("USER_FUNCTION_HOST" , "0.0.0.0" )
50+ __port = os .environ .get ("USER_FUNCTION_PORT" , "8091" )
51+ __proxy_host = os .environ .get ("PROXY_HTTP_HOST" , "0.0.0.0" )
52+ __proxy_port = os .environ .get ("PROXY_HTTP_PORT" , "9001" )
53+ __system : str = None
54+ __actor_entities : MutableMapping [str ,
55+ Actor ] = field (default_factory = dict )
6656
6757 # @staticmethod
6858 # def invoke(name: str, command: str, arg: Any, output_type: Any) -> Any:
@@ -77,21 +67,39 @@ def host(self, address: str):
7767 self .__host = address
7868 return self
7969
80- def port (self , port : str ):
70+ def port (self , port : int ):
8171 """Set the Network Port address."""
82- self .__port = port
72+ self .__port = str ( port )
8373 return self
8474
85- def register_actor (self , actor : Actor ):
75+ def proxy_host (self , host : str ):
76+ """Set the Spawn Proxy Host Address"""
77+ self .__proxy_host = host
78+ return self
79+
80+ def proxy_port (self , port : int ):
81+ self .__proxy_port = str (port )
82+ return self
83+
84+ def actor_system (self , system : str = None ):
85+ """Set the ActorSystem"""
86+ self .__system = system
87+ return self
88+
89+ def add_actor (self , actor : Actor ):
8690 """Registry the user Actor entity."""
87- self .__actor_entities . append ( actor )
91+ self .__actor_entities [ actor . settings . name ] = actor
8892 return self
8993
9094 def start (self ):
9195 """Start the user function and HTTP Server."""
96+ if not self .__system :
97+ raise Exception (
98+ "ActorSystem cannot be None. Use actor_system function to set an ActorSystem" )
99+
92100 address = "{}:{}" .format (self .__host , self .__port )
93101 self .__controller = ActorController (
94- self .__host , self .__port , self .__actor_entities )
102+ self .__proxy_host , self .__proxy_port , self . __system , self .__actor_entities )
95103
96104 self .__app = create_app (controller = self .__controller )
97105
0 commit comments