|
6 | 6 |
|
7 | 7 | from spawn.eigr.functions.actors.api.actor import Actor as ActorEntity |
8 | 8 | from spawn.eigr.functions.actors.api.actor import ActorHandler |
| 9 | +from spawn.eigr.functions.actors.api.context import Context as ActorContext |
| 10 | +from spawn.eigr.functions.actors.api.value import Value, ReplyKind |
9 | 11 | from spawn.eigr.functions.actors.api.settings import Kind as ActorKind |
10 | | -from spawn.eigr.functions.actors.api.actor import TimerFunction |
11 | 12 |
|
12 | 13 | from spawn.eigr.functions.protocol.actors.actor_pb2 import ( |
13 | 14 | Actor, |
|
29 | 30 | ActorInvocation, |
30 | 31 | ActorInvocationResponse, |
31 | 32 | Context, |
| 33 | + Noop, |
32 | 34 | RegistrationRequest, |
33 | 35 | RegistrationResponse, |
34 | 36 | ServiceInfo, |
35 | 37 | ) |
36 | 38 |
|
| 39 | +from google.protobuf import symbol_database as _symbol_database |
| 40 | +from google.protobuf.any_pb2 import Any as AnyProto |
| 41 | + |
37 | 42 | import logging |
38 | 43 | import platform |
39 | 44 | import requests |
40 | 45 |
|
41 | 46 | from typing import Any, MutableMapping |
42 | 47 |
|
| 48 | +_sym_db = _symbol_database.Default() |
| 49 | + |
43 | 50 | _DEFAULT_HEADERS = { |
44 | 51 | "Accept": "application/octet-stream", |
45 | 52 | "Content-Type": "application/octet-stream", |
46 | 53 | } |
47 | 54 |
|
48 | 55 | _REGISTER_URI = "/api/v1/system" |
49 | 56 |
|
| 57 | +TYPE_URL_PREFIX = 'type.googleapis.com/' |
| 58 | + |
| 59 | + |
| 60 | +def get_payload(input): |
| 61 | + input_type: str = input.payload.type_url |
| 62 | + if input_type.startswith(TYPE_URL_PREFIX): |
| 63 | + input_type = input_type[len(TYPE_URL_PREFIX):] |
| 64 | + input_class = _sym_db.GetSymbol(input_type) |
| 65 | + input = input_class() |
| 66 | + input.ParseFromString(input.payload.value) |
| 67 | + return input |
| 68 | + |
| 69 | + |
| 70 | +def pack(input): |
| 71 | + any = AnyProto() |
| 72 | + any.Pack(input) |
| 73 | + return any |
| 74 | + |
| 75 | + |
| 76 | +def handle_response(system, actor_name, result): |
| 77 | + print("Result ----- {}".format(result)) |
| 78 | + actor_invocation_response = ActorInvocationResponse() |
| 79 | + actor_invocation_response.actor_name = actor_name |
| 80 | + actor_invocation_response.actor_system = system |
| 81 | + |
| 82 | + updated_context = Context() |
| 83 | + |
| 84 | + if result.get_metadata() != None and len(result.get_metadata().get_metadata()) > 0: |
| 85 | + print("Metadata -----------".format(result.metadata)) |
| 86 | + updated_context.metadata = result.get_metadata.get_metadata() |
| 87 | + |
| 88 | + if result.get_metadata() != None and len(result.get_metadata().get_tags()) > 0: |
| 89 | + updated_context.tags = result.get_metadata().get_tags() |
| 90 | + |
| 91 | + updated_context.state.CopyFrom(pack(result.get_state())) |
| 92 | + |
| 93 | + actor_invocation_response.updated_context.CopyFrom(updated_context) |
| 94 | + |
| 95 | + if result.get_reply_kind() == ReplyKind.NO_REPLY: |
| 96 | + actor_invocation_response.noop = Noop() |
| 97 | + elif result.get_reply_kind == ReplyKind.REPLY: |
| 98 | + actor_invocation_response.value = pack(result.get_response()) |
| 99 | + |
| 100 | + return actor_invocation_response |
| 101 | + |
50 | 102 |
|
51 | 103 | class ActorController: |
52 | 104 | _instance = None |
@@ -81,17 +133,23 @@ def handle_invoke(self, data) -> ActorInvocationResponse: |
81 | 133 | entity = self.actors[actor_name] if not actor_parent else self.actors[actor_parent] |
82 | 134 |
|
83 | 135 | handler = ActorHandler(entity) |
| 136 | + current_context = actor_invocation.current_context |
| 137 | + |
| 138 | + input = None if actor_invocation.WhichOneof( |
| 139 | + "payload") == "noop" else get_payload(actor_invocation.value) |
| 140 | + |
| 141 | + ctx = ActorContext(state=None, caller=actor_invocation.caller.name, |
| 142 | + metadata=current_context.metadata, tags=current_context.tags) |
84 | 143 |
|
85 | | - # Update Context |
86 | | - updated_context = Context() |
| 144 | + result = handler.handle_action( |
| 145 | + action_name=actor_invocation.action_name, input=input, ctx=ctx) |
87 | 146 |
|
88 | | - # Then send ActorInvocationResponse back to the caller |
89 | | - actor_invocation_response = ActorInvocationResponse() |
90 | | - actor_invocation_response.actor_name = actor_name |
91 | | - actor_invocation_response.actor_system = actor_system |
92 | | - actor_invocation_response.updated_context.CopyFrom(updated_context) |
| 147 | + if not isinstance(result, Value): |
| 148 | + raise Exception( |
| 149 | + "Action did not return a valid type in its response. Valid Value found {}".format(type(result))) |
93 | 150 |
|
94 | | - return actor_invocation_response |
| 151 | + # Handle result value |
| 152 | + return handle_response(actor_system, actor_name, result) |
95 | 153 |
|
96 | 154 | def register(self): |
97 | 155 | logging.info("Registering Actors on the Proxy %s", self.actors) |
|
0 commit comments