def send_later(self, route: str, headers: dict = None, body: any = None, reply_to: str = None, me=True, seconds: float = 1.0) -> None: self.util.validate_service_name(route, True) if isinstance(seconds, float) or isinstance(seconds, int): if seconds > 0: if headers is None and body is None: raise ValueError( 'Unable to send because both headers and body are missing' ) event = EventEnvelope().set_to(route) if headers is not None: if not isinstance(headers, dict): raise ValueError('headers must be dict') for h in headers: event.set_header(str(h), str(headers[h])) if body is not None: event.set_body(body) if reply_to is not None: if not isinstance(reply_to, str): raise ValueError('reply_to must be str') # encode 'me' in the "call back" if replying to this instance event.set_reply_to(reply_to, me) self.platform.send_event_later(event, seconds) else: raise ValueError('delay in seconds must be larger than zero') else: raise ValueError('delay in seconds must be int or float')
def send_event_later(self, event: EventEnvelope, seconds: float = 1.0, me=True): if event.get_reply_to() is not None: event.set_reply_to(event.get_reply_to(), me) self.platform.send_event_later(event, seconds)
def request(self, event: EventEnvelope, timeout_seconds: float): timeout_value = self.util.get_float(timeout_seconds) if timeout_value <= 0: raise ValueError( 'timeout value in seconds must be positive number') if not isinstance(event, EventEnvelope): raise ValueError('event object must be an EventEnvelope') # restore distributed tracing info from current thread trace_info = self.get_trace() if trace_info: if trace_info.get_route() is not None and event.get_from() is None: event.set_from(trace_info.get_route()) if trace_info.get_id() is not None and trace_info.get_path( ) is not None: event.set_trace(trace_info.get_id(), trace_info.get_path()) # emulate RPC inbox = Inbox(self) temp_route = inbox.get_route() inbox_queue = inbox.get_queue() try: route = event.get_to() event.set_reply_to(temp_route, me=True) if route in self._function_queues: self._loop.call_soon_threadsafe(self._send, route, event.to_map()) else: if self._cloud.is_connected(): self._cloud.send_payload({ 'type': 'event', 'event': event.to_map() }) else: raise ValueError(f'route {route} not found') # wait until response event is delivered to the inbox return inbox_queue.get(True, timeout_value) except Empty: raise TimeoutError( f'Route {event.get_to()} timeout for {round(timeout_value, 3)} seconds' ) finally: inbox.close()
def send_event(self, event: EventEnvelope, me=True): if event.get_reply_to() is not None: event.set_reply_to(event.get_reply_to(), me) self.platform.send_event(event)