Skip to content

Commit 0abacb4

Browse files
committed
Refactor. Move parts
1 parent c497326 commit 0abacb4

13 files changed

Lines changed: 78 additions & 147 deletions

File tree

example/spawn_example.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Copyright 2022 Eigr.
33
Licensed under the Apache License, Version 2.0.
44
"""
5-
from spawn.sdk import Spawn
5+
from spawn.eigr.functions.actors.api.sdk import Spawn
66
from example.joe import JoeActor
77
from example.domain.domain_pb2 import Reply, Request
88

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
from dataclasses import dataclass
2-
from typing import Dict
1+
from dataclasses import dataclass, field
2+
from typing import MutableMapping
33

44

55
@dataclass
66
class Context:
77
state: object
88
caller: str = None
9-
metadata: Dict[str, str] = None
10-
tags: Dict[str, str] = None
9+
metadata: MutableMapping[str, str] = field(default_factory=dict)
10+
tags: MutableMapping[str, str] = field(default_factory=dict)

spawn/eigr/functions/actors/api/decorators/action.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33

44
from spawn.eigr.functions.actors.api.context import Context
55
from spawn.eigr.functions.actors.api.metadata import Metadata
6+
from spawn.eigr.functions.actors.api.settings import ActorSettings
67
from spawn.eigr.functions.actors.api.value import Value
78
from spawn.eigr.functions.actors.api.workflows.broadcast import Broadcast
89
from spawn.eigr.functions.actors.api.workflows.effect import Effect
910
from spawn.eigr.functions.actors.api.decorators.actor import Actor
1011

11-
from spawn.eigr.functions.actors.core import ActionInfo, ActorSettings, Actors
12+
from spawn.eigr.functions.actors.internal.core import ActionInfo, Actors
1213

1314
logging.basicConfig(level=logging.DEBUG)
1415
_logger = logging.getLogger(__name__)

spawn/eigr/functions/actors/api/decorators/actor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import functools
22

3-
from spawn.eigr.functions.actors.core import ActorInfo, Actors
3+
from spawn.eigr.functions.actors.internal.core import ActorInfo, Actors
44

55

66
def Actor(cls):
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
from dataclasses import dataclass, field
88

9-
from spawn.entity import ActorEntity
109
from spawn.handler import action_handler
1110
from spawn.controller import SpawnActorController as ActorController
1211

@@ -30,9 +29,9 @@ class Spawn:
3029

3130
__host = os.environ.get("HOST", "0.0.0.0")
3231
__port = os.environ.get("PORT", "8091")
33-
__actors: List[ActorEntity] = field(default_factory=list)
3432
__app = Flask(__name__)
35-
__is_debug_enable = json.loads(os.environ.get("SDK_DEBUG_ENABLE", "false").lower())
33+
__is_debug_enable = json.loads(
34+
os.environ.get("SDK_DEBUG_ENABLE", "false").lower())
3635
__actorController = ActorController(
3736
os.environ.get("PROXY_HOST", "localhost"),
3837
os.environ.get("PROXY_PORT", "9002"),
@@ -65,7 +64,8 @@ def start(self):
6564
"""Start the user function and HTTP Server."""
6665
address = "{}:{}".format(self.__host, self.__port)
6766

68-
server = threading.Thread(target=lambda: self.__start_server(action_handler))
67+
server = threading.Thread(
68+
target=lambda: self.__start_server(action_handler))
6969
logging.info("Starting Spawn on address %s", address)
7070
try:
7171
server.start()
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from enum import Enum
2+
from typing import Generic, TypeVar
3+
4+
T = TypeVar('T')
5+
6+
7+
class Kind(str, Enum):
8+
NAMED = 'NAMED'
9+
UNNAMED = 'UNNAMED'
10+
POOLED = 'POOLED'
11+
12+
13+
class ActorSettings:
14+
def __init__(
15+
self,
16+
name: str,
17+
kind: Kind = Kind.NAMED,
18+
stateful: bool = True,
19+
state_type: Generic[T] = None,
20+
channel: str = None,
21+
deactivate_timeout: int = 30,
22+
snapshot_timeout: int = 10):
23+
self.name = name
24+
self.kind = kind
25+
self.stateful = stateful
26+
self.state_type = state_type
27+
self.channel = channel
28+
self.deactivate_timeout = deactivate_timeout
29+
self.snapshot_timeout = snapshot_timeout

spawn/eigr/functions/actors/core.py

Lines changed: 0 additions & 58 deletions
This file was deleted.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
"""
2+
Copyright 2022 Eigr.
3+
Licensed under the Apache License, Version 2.0.
4+
"""

spawn/controller.py renamed to spawn/eigr/functions/actors/internal/controller.py

File renamed without changes.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from typing import List, MutableMapping
2+
3+
from spawn.eigr.functions.actors.api.settings import ActorSettings
4+
5+
6+
class ActionInfo:
7+
def __init__(self, name, input=None, output=None):
8+
self.name = name
9+
self.input = input
10+
self.output = output
11+
12+
13+
class ActorInfo:
14+
def __init__(self, name: str, settings: ActorSettings, actions: List[ActionInfo] = None) -> None:
15+
self.name = name
16+
self.settings = settings
17+
self.actions = actions
18+
19+
20+
class Actors:
21+
_instance = None
22+
actors = MutableMapping[str, ActorInfo]
23+
24+
def __init__(self):
25+
if not self.actors:
26+
self.actors = {}
27+
28+
def __new__(cls, *args, **kwargs):
29+
if not isinstance(cls._instance, cls):
30+
cls._instance = object.__new__(cls)
31+
32+
return cls._instance

0 commit comments

Comments
 (0)