The gRPC Backend

Python CI actions Coverage Status

The Template gRPC API

Because we are using AVRO instead of protobufs, we cannot take advantage of the automatic API generation that gRPC offers. To help defray some of the cost of manually creating the API code, we have included some boilerplate code that initializes an INIT method, as well as a MONITOR method.

The API classes live in grpc_modules/template_grpc.py Each endpoint needs a Stub, a Servicer, a function that adds the endpoint to the gRPC server, as well as a main class that instantiates the gRPC stream. A discussion of how these are instantiated in the server can be found in The Template Test Environment in the section describing the test gRPC Server.

class TemplateInitStub(object):
   """The Stub for connecting to the Template init service."""
   def __init__(self, channel, avroserialhelper):
      """Constructor.
      Args
      channel A grpc.Channel.
      """
      self.initTemplate = channel.unary_stream(
         "/templateaapi.TemplateInit/initTemplate",
         request_serializer=avroserialhelper.avro_serializer,
         response_deserializer=avroserialhelper.avro_deserializer,
      )
class TemplateInitServicer(object):
   """The servicer definition for initiating jobs with the Template pipeline."""
   def initTemplate(self, request, context):
      context.set_code(grpc.StatusCode.UNIMPLEMENTED)
      context.set_details("Method not implemented!")
      raise NotImplementedError("Method not implemented!")
def add_TemplateInitServicer_to_server(servicer, server, avroserialhelper):
   """The actual methods for sending and receiving RPC calls."""
   rpc_method_handlers = {
      "initTemplate": grpc.unary_stream_rpc_method_handler(
            servicer.initTemplate,
            request_deserializer=avroserialhelper.avro_deserializer,
            response_serializer=avroserialhelper.avro_serializer,
      ),
   }
   generic_handler = grpc.method_handlers_generic_handler(
      "templateaapi.TemplateInit", rpc_method_handlers
   )
   server.add_generic_rpc_handlers((generic_handler,))
class TemplateInit(object):
   """The definition of the Template gRPC API and stream connections."""
   @staticmethod
   def initTemplate(
      request,
      target,
      options=(),
      channel_credentials=None,
      call_credentials=None,
      insecure=False,
      compression=None,
      wait_for_ready=None,
      timeout=None,
      metadata=None,
   ):
      return grpc.experimental.unary_stream(
            request,
            target,
            "/templateaapi.TemplateInit/initTemplate",
            options,
            channel_credentials,
            insecure,
            call_credentials,
            compression,
            wait_for_ready,
            timeout,
            metadata,
      )