Skip to content

Commit a6ad896

Browse files
committed
Feat. Added sync invocation support
1 parent cfbcb75 commit a6ad896

11 files changed

Lines changed: 109 additions & 187 deletions

File tree

example/domain/domain_pb2.py

Lines changed: 15 additions & 136 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/joe.py

Lines changed: 6 additions & 3 deletions
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 domain.domain_pb2 import JoeState, Request, Reply
5+
from domain.domain_pb2 import State, Request, Reply
66
from spawn.eigr.functions.actors.api.actor import Actor
77
from spawn.eigr.functions.actors.api.settings import ActorSettings
88
from spawn.eigr.functions.actors.api.context import Context
@@ -24,7 +24,7 @@ def hi(ctx: Context) -> Value:
2424
broadcast.value = request
2525

2626
if not ctx.state:
27-
new_state = JoeState()
27+
new_state = State()
2828
new_state.languages.append("python")
2929
else:
3030
new_state = ctx.state
@@ -39,4 +39,7 @@ def hi(ctx: Context) -> Value:
3939
def set_language(request: Request, ctx: Context) -> Value:
4040
reply = Reply()
4141
reply.response = "erlang"
42-
return Value().of(reply, ctx.state).reply()
42+
print("Current State -> " + str(ctx.state))
43+
new_state = State()
44+
new_state.languages.append("python")
45+
return Value().value(reply).state(new_state).reply()
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
syntax = "proto3";
2+
3+
package domain;
4+
5+
message State {
6+
repeated string languages = 1;
7+
}
8+
9+
message Request {
10+
string language = 1;
11+
}
12+
13+
message Reply {
14+
string response = 1;
15+
}

example/protobuf/example/domain/domain.proto

Lines changed: 0 additions & 19 deletions
This file was deleted.

example/spawn_example.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"""
55
from spawn.eigr.functions.actors.api.reference import ActorRef
66
from spawn.eigr.functions.actors.api.sdk import Spawn
7+
78
from example.joe import actor as joe_actor
89
from example.unnamed_actor import abstract
910
from example.domain.domain_pb2 import Request
@@ -20,8 +21,9 @@
2021
mike_actor: ActorRef = Spawn.create_actor_ref(
2122
system="spawn-system",
2223
actor_name="mike",
23-
parent="abs_actor",
24-
state_revision=1
24+
parent="abs_actor"
2525
)
2626

27-
# mike_actor.invoke(request)
27+
(status, result) = mike_actor.invoke("setLanguage", request)
28+
print("Invocation Result Status: " + status)
29+
print("Invocation Result Value: " + str(result.response))

example/unnamed_actor.py

Lines changed: 5 additions & 2 deletions
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 domain.domain_pb2 import Request, Reply
5+
from domain.domain_pb2 import State, Request, Reply
66
from spawn.eigr.functions.actors.api.actor import Actor
77
from spawn.eigr.functions.actors.api.settings import ActorSettings, Kind
88
from spawn.eigr.functions.actors.api.context import Context
@@ -16,4 +16,7 @@
1616
def set_language(request: Request, ctx: Context) -> Value:
1717
reply = Reply()
1818
reply.response = "erlang"
19-
return Value().of(reply, ctx.state).reply()
19+
print("Current State -> " + str(ctx.state))
20+
new_state = State()
21+
new_state.languages.append("python")
22+
return Value().value(reply).state(new_state).reply()

scripts/compile-pb.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ set -o errexit
55
set -o pipefail
66

77
# follow the basic steps here: https://grpc.io/docs/tutorials/basic/python/
8-
protoc -I ../protobuf/ --python_out=../spawn eigr/functions/protocol/actors/actor.proto
9-
protoc -I ../protobuf/ --python_out=../spawn eigr/functions/protocol/actors/protocol.proto
8+
#protoc -I ../protobuf/ --python_out=../spawn eigr/functions/protocol/actors/actor.proto
9+
#protoc -I ../protobuf/ --python_out=../spawn eigr/functions/protocol/actors/protocol.proto
1010

11-
#protoc -I ../example/protobuf/ --python_out=../example ../example/protobuf/domain/domain.proto
11+
protoc -I ../example/protobuf/ --python_out=../example ../example/protobuf/domain/domain.proto

spawn/eigr/functions/actors/api/reference.py

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,20 @@
11

22
from spawn.eigr.functions.actors.internal.client import SpawnClient
3+
from spawn.eigr.functions.actors.internal.controller import pack, get_payload
34

45
from spawn.eigr.functions.protocol.actors.actor_pb2 import (
56
Actor,
67
ActorId,
7-
ActorState,
8-
Metadata,
9-
ActorSettings,
10-
Action,
11-
FixedTimerAction,
12-
ActorSnapshotStrategy,
13-
ActorDeactivationStrategy,
14-
ActorSystem,
15-
Kind,
16-
Registry,
17-
TimeoutStrategy,
8+
ActorSystem
189
)
1910

2011
from spawn.eigr.functions.protocol.actors.protocol_pb2 import (
2112
SpawnRequest,
2213
SpawnResponse,
2314
InvocationRequest,
2415
InvocationResponse,
25-
RegistrationRequest,
26-
RegistrationResponse
16+
RequestStatus,
17+
Status
2718
)
2819

2920

@@ -52,5 +43,43 @@ def __init__(self, client: SpawnClient, system: str, actor: str, parent: str = N
5243
spawn(self.__spawn_client, self.actor_system,
5344
self.actor_name, self.actor_parent, self.revision)
5445

55-
def invoke(self, request: any):
56-
pass
46+
def invoke(self, action: str, request: any = None):
47+
req: InvocationRequest = self.__build_request(action, request)
48+
resp: InvocationResponse = self.__spawn_client.invoke(
49+
self.actor_system, self.actor_name, req)
50+
51+
return self.__build_result(resp)
52+
53+
def __build_request(self, action: str, request: any):
54+
req: InvocationRequest = InvocationRequest()
55+
system = ActorSystem()
56+
system.name = self.actor_system
57+
58+
actor_id = ActorId()
59+
actor_id.name = self.actor_name
60+
actor_id.system = self.actor_system
61+
62+
actor = Actor()
63+
actor.id.CopyFrom(actor_id)
64+
65+
req.system.CopyFrom(system)
66+
req.actor.CopyFrom(actor)
67+
req.action_name = action
68+
69+
if request != None:
70+
req.value.CopyFrom(pack(request))
71+
72+
return req
73+
74+
def __build_result(self, resp: InvocationResponse) -> any:
75+
sts: RequestStatus = resp.status
76+
77+
if sts.status == Status.OK:
78+
output = None if resp.WhichOneof(
79+
"payload") == "noop" else get_payload(resp.value)
80+
81+
return "ok", output
82+
elif sts.status == Status.ACTOR_NOT_FOUND:
83+
return "actor_not_found", None
84+
else:
85+
return "error", None

spawn/eigr/functions/actors/api/sdk.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ def create_app(controller: ActorController):
2626

2727
@app.route('/api/v1/actors/actions', methods=["POST"])
2828
def action():
29-
print(request)
3029
data = request.data
3130

3231
actor_invocation_response = controller.handle_invoke(data)

spawn/eigr/functions/actors/api/value.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ def pipe(self, pipe: Pipe):
8888
return self
8989

9090
def reply(self):
91+
self.__reply_kind = ReplyKind.REPLY
9192
return self
9293

9394
def noreply(self):

0 commit comments

Comments
 (0)