Skip to content

added spawn as global package while importing #2

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 1 commit into from
Sep 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Generated by the gRPC Python protocol compiler plugin. DO NOT EDIT!
"""Client and server classes corresponding to protobuf-defined services."""
import grpc

26 changes: 26 additions & 0 deletions example/protobuf/eigr/functions/spawn/example/state/joe_pb2.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Generated by the gRPC Python protocol compiler plugin. DO NOT EDIT!
"""Client and server classes corresponding to protobuf-defined services."""
import grpc

47 changes: 47 additions & 0 deletions protobuf/eigr/functions/protocol/actors/actor_pb2.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions protobuf/eigr/functions/protocol/actors/actor_pb2_grpc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Generated by the gRPC Python protocol compiler plugin. DO NOT EDIT!
"""Client and server classes corresponding to protobuf-defined services."""
import grpc

48 changes: 48 additions & 0 deletions protobuf/eigr/functions/protocol/actors/protocol_pb2.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions protobuf/eigr/functions/protocol/actors/protocol_pb2_grpc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Generated by the gRPC Python protocol compiler plugin. DO NOT EDIT!
"""Client and server classes corresponding to protobuf-defined services."""
import grpc

26 changes: 26 additions & 0 deletions protobuf/google/protobuf/any_pb2.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions protobuf/google/protobuf/any_pb2_grpc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Generated by the gRPC Python protocol compiler plugin. DO NOT EDIT!
"""Client and server classes corresponding to protobuf-defined services."""
import grpc

39 changes: 22 additions & 17 deletions spawn/sdk.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from dataclasses import dataclass, field

from handler import action_handler
from spawn.handler import action_handler
from internal.controller import SpawnActorController as ActorController

from typing import List, Callable, Any, Mapping, MutableMapping
Expand All @@ -32,21 +32,24 @@ def init(self) -> ActorParams:
@dataclass
class ActorEntity:
init_state: Callable[[str], Any]
command_handlers: MutableMapping[str,
Callable] = field(default_factory=dict)
command_handlers: MutableMapping[str, Callable] = field(default_factory=dict)

def command(self, name: str):
def register_command_handler(function):
"""
Register the function to handle commands
"""
if name in self.command_handlers:
raise Exception("Command handler function {} already defined for command {}".format(
self.command_handlers[name], name))
raise Exception(
"Command handler function {} already defined for command {}".format(
self.command_handlers[name], name
)
)

if function.__code__.co_argcount > 2:
raise Exception(
"At most two parameters, the command and the context, should be accepted by the command function")
"At most two parameters, the command and the context, should be accepted by the command function"
)

self.command_handlers[name] = function
return function
Expand All @@ -65,16 +68,18 @@ def init_actor(self):
@dataclass
class Spawn:
actorController = ActorController(
os.environ.get('PROXY_HOST', 'localhost'),
os.environ.get('PROXY_PORT', '9001'),
os.environ.get("PROXY_HOST", "localhost"),
os.environ.get("PROXY_PORT", "9001"),
)

logging.basicConfig(
format='%(asctime)s - %(filename)s - %(levelname)s: %(message)s', level=logging.INFO)
format="%(asctime)s - %(filename)s - %(levelname)s: %(message)s",
level=logging.INFO,
)
logging.root.setLevel(logging.NOTSET)

__host = '127.0.0.1'
__port = '8090'
__host = "127.0.0.1"
__port = "8090"
__actors: List[ActorEntity] = field(default_factory=list)

def host(self, address: str):
Expand All @@ -94,20 +99,20 @@ def register_actor(self, entity: ActorEntity):

def start(self):
"""Start the user function and HTTP Server."""
address = '{}:{}'.format(os.environ.get(
'HOST', self.__host), os.environ.get('PORT', self.__port))
address = "{}:{}".format(
os.environ.get("HOST", self.__host), os.environ.get("PORT", self.__port)
)

logging.info('Starting Spawn on address %s', address)
logging.info("Starting Spawn on address %s", address)
try:
app = Flask(__name__)
app.register_blueprint(action_handler)
app.run(host=self.__host, port=self.__port,
threaded=True, debug=True)
app.run(host=self.__host, port=self.__port, threaded=True, debug=True)

# Invoke proxy for register ActorsEntity using Spawn protobuf types
self.__register(self.__actors)
except IOError as e:
logging.error('Error on start Spawn %s', e.__cause__)
logging.error("Error on start Spawn %s", e.__cause__)

def __register(self, actors: List[ActorEntity]):
self.actorController.register(actors)