|
1 | 1 |
|
2 | 2 | from spawn.eigr.functions.actors.internal.client import SpawnClient |
| 3 | +from spawn.eigr.functions.actors.internal.controller import pack, get_payload |
3 | 4 |
|
4 | 5 | from spawn.eigr.functions.protocol.actors.actor_pb2 import ( |
5 | 6 | Actor, |
6 | 7 | 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 |
18 | 9 | ) |
19 | 10 |
|
20 | 11 | from spawn.eigr.functions.protocol.actors.protocol_pb2 import ( |
21 | 12 | SpawnRequest, |
22 | 13 | SpawnResponse, |
23 | 14 | InvocationRequest, |
24 | 15 | InvocationResponse, |
25 | | - RegistrationRequest, |
26 | | - RegistrationResponse |
| 16 | + RequestStatus, |
| 17 | + Status |
27 | 18 | ) |
28 | 19 |
|
29 | 20 |
|
@@ -52,5 +43,43 @@ def __init__(self, client: SpawnClient, system: str, actor: str, parent: str = N |
52 | 43 | spawn(self.__spawn_client, self.actor_system, |
53 | 44 | self.actor_name, self.actor_parent, self.revision) |
54 | 45 |
|
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 |
0 commit comments