Beispiel #1
0
    def dispatch(self, service_name: str, args, kwargs):
        if service_name not in self._service_route_table:
            raise ValueError(f'{service_name} has not been registered yet')

        _instance, _class, _method = self._service_route_table[service_name]

        if not _instance:
            _instance = _class()

        task_name = ''
        deserialized_args = list()
        for arg in args:
            task = meta_pb2.Task()
            msg_len = task.ParseFromString(arg)
            deserialized_task = ErTask.from_proto(task)
            if not task_name:
                task_name = deserialized_task._name
            deserialized_args.append(deserialized_task)

        L.debug(
            f"[CS] calling: [{service_name}], task_name={task_name}, request={deserialized_args}, len={len(args)}"
        )

        start = time.time()
        try:
            call_result = _method(_instance, *deserialized_args)
        except Exception as e:
            L.exception(
                f'Failed to dispatch to [{service_name}], task_name: {task_name}, request: {deserialized_args}'
            )
            raise e
        elapsed = time.time() - start
        if L.isEnabledFor(logging.TRACE):
            L.trace(
                f"[CS] called (elapsed={elapsed}): [{service_name}]: task_name={task_name}, request={deserialized_args}, result={call_result}"
            )
        else:
            L.debug(
                f"[CS] called (elapsed={elapsed}): [{service_name}], task_name={task_name}, request={deserialized_args}"
            )

        # todo:2: defaulting to pb message. need changes when other types of result is present
        return [call_result.to_proto().SerializeToString()]
Beispiel #2
0
 def from_proto_string(pb_string):
     pb_message = meta_pb2.Task()
     msg_len = pb_message.ParseFromString(pb_string)
     return ErTask.from_proto(pb_message)
Beispiel #3
0
 def to_proto(self):
     return meta_pb2.Task(id=self._id,
                          name=self._name,
                          inputs=_elements_to_proto(self._inputs),
                          outputs=_elements_to_proto(self._outputs),
                          job=_to_proto(self._job))