def serve(self):

        # We need to build a health service to work with go-plugin
        logging.basicConfig(level=logging.DEBUG)

        if self.isRunning():
            print("1|1|tcp|127.0.0.1:1234|grpc")  #DO NOT REMOTE THIS LINE
            sys.stdout.flush()  #DO NOT REMOTE THIS LINE
            logging.info("Already running / Not starting new server")
            return

        health = HealthServicer()
        health.set(
            "plugin",
            health_pb2.HealthCheckResponse.ServingStatus.Value('SERVING'))

        # Start the server.
        server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))

        self.server = server
        self.e = threading.Event()

        #print (inspect.getsourcefile(server.start))

        pyvcloudprovider_pb2_grpc.add_PyVcloudProviderServicer_to_server(
            PyVcloudProviderServicer(self), server)

        disk_pb2_grpc.add_IndependentDiskServicer_to_server(
            IndependentDiskServicer(self), server)

        org_pb2_grpc.add_OrgServicer_to_server(OrgServicer(self), server)

        user_pb2_grpc.add_UserServicer_to_server(UserServicer(self), server)

        vdc_pb2_grpc.add_VdcServicer_to_server(VdcServicer(self), server)

        vapp_vm_pb2_grpc.add_VappVmServicer_to_server(
            VappVmServicer(self), server)

        health_pb2_grpc.add_HealthServicer_to_server(health, server)
        server.add_insecure_port('127.0.0.1:1234')
        ## check before start if server is running at 1234
        ## if server running connect to the server and execute stop
        server.start()

        # Output information
        print("1|1|tcp|127.0.0.1:1234|grpc")
        sys.stdout.flush()

        self.e.wait(
        )  # This event if expected from the pluger_stop functionality
        self.server.stop(0)
Пример #2
0
    def setUp(self):
        servicer = health.HealthServicer()
        servicer.set('', health_pb2.HealthCheckResponse.SERVING)
        servicer.set('grpc.test.TestServiceServing',
                     health_pb2.HealthCheckResponse.SERVING)
        servicer.set('grpc.test.TestServiceUnknown',
                     health_pb2.HealthCheckResponse.UNKNOWN)
        servicer.set('grpc.test.TestServiceNotServing',
                     health_pb2.HealthCheckResponse.NOT_SERVING)
        self._server = test_common.test_server()
        port = self._server.add_insecure_port('[::]:0')
        health_pb2_grpc.add_HealthServicer_to_server(servicer, self._server)
        self._server.start()

        channel = grpc.insecure_channel('localhost:%d' % port)
        self._stub = health_pb2_grpc.HealthStub(channel)
Пример #3
0
        def start_server(self, non_blocking=False, thread_pool=None):
            self._thread_pool = thread_pool
            self._servicer = health.HealthServicer(
                experimental_non_blocking=non_blocking,
                experimental_thread_pool=thread_pool)
            self._servicer.set('', health_pb2.HealthCheckResponse.SERVING)
            self._servicer.set(_SERVING_SERVICE,
                               health_pb2.HealthCheckResponse.SERVING)
            self._servicer.set(_UNKNOWN_SERVICE,
                               health_pb2.HealthCheckResponse.UNKNOWN)
            self._servicer.set(_NOT_SERVING_SERVICE,
                               health_pb2.HealthCheckResponse.NOT_SERVING)
            self._server = test_common.test_server()
            port = self._server.add_insecure_port('[::]:0')
            health_pb2_grpc.add_HealthServicer_to_server(
                self._servicer, self._server)
            self._server.start()

            self._channel = grpc.insecure_channel('localhost:%d' % port)
            self._stub = health_pb2_grpc.HealthStub(self._channel)
Пример #4
0
 def Start(self,
           analytic_port=50051,
           max_workers=10,
           concurrency_safe=False):
     self.concurrency_safe = concurrency_safe
     server = grpc.server(
         futures.ThreadPoolExecutor(max_workers=max_workers),
         options=(('grpc.so_reuseport', 0), ))
     provenance_pb2_grpc.add_ProvenanceServicer_to_server(
         _ProvenanceServicer(self), server)
     health_pb2_grpc.add_HealthServicer_to_server(self._health_servicer,
                                                  server)
     if not server.add_insecure_port('[::]:{:d}'.format(analytic_port)):
         raise RuntimeError(
             "can't bind to port {}: already in use".format(analytic_port))
     server.start()
     self._health_servicer.set('', health_pb2.HealthCheckResponse.SERVING)
     print("Analytic server started on port {} with PID {}".format(
         analytic_port, os.getpid()),
           file=sys.stderr)
     return server
Пример #5
0
def start(dummy_mode):
  server = grpc.server(futures.ThreadPoolExecutor(max_workers=10),
                       interceptors=(tracer_interceptor,))
  service = None
  if dummy_mode:
    service = DummyEmailService()
  else:
    raise Exception('non-dummy mode not implemented yet')

  demo_pb2_grpc.add_EmailServiceServicer_to_server(service, server)
  health_pb2_grpc.add_HealthServicer_to_server(service, server)

  port = os.environ.get('PORT', "8080")
  logger.info("listening on port: "+port)
  server.add_insecure_port('[::]:'+port)
  server.start()
  try:
    while True:
      time.sleep(3600)
  except KeyboardInterrupt:
    server.stop(0)
def start(dummy_mode):
    server = grpc.server(futures.ThreadPoolExecutor(
        max_workers=10))  #, interceptors=(tracer_interceptor,))
    service = None
    if dummy_mode:
        service = DummyEmailService()
    else:
        raise Exception('non-dummy mode not implemented yet')

    demo_pb2_grpc.add_EmailServiceServicer_to_server(service, server)
    health_pb2_grpc.add_HealthServicer_to_server(service, server)

    port = os.environ.get('PORT', "8080")
    logger.info("listening on port: " + port)
    server.add_insecure_port('[::]:' + port)
    server.start()
    try:
        while True:
            time.sleep(3600)
    except KeyboardInterrupt:
        server.stop(0)
Пример #7
0
def serve():
    # We need to build a health service to work with go-plugin
    health = HealthServicer()
    health.set("plugin", health_pb2.HealthCheckResponse.ServingStatus.Value('SERVING'))

    # Start the server.
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
    kv_pb2_grpc.add_KVServicer_to_server(KVServicer(), server)
    health_pb2_grpc.add_HealthServicer_to_server(health, server)
    server.add_insecure_port('127.0.0.1:1234')
    server.start()

    # Output information
    print("1|1|tcp|127.0.0.1:1234|grpc")
    sys.stdout.flush()

    try:
        while True:
            time.sleep(60 * 60 * 24)
    except KeyboardInterrupt:
        server.stop(0)
Пример #8
0
def main():
    health = HealthServicer()
    health.set("plugin", health_pb2.HealthCheckResponse.ServingStatus.Value('SERVING'))

    plugin = PluginServicer()

    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
    dashboard_pb2_grpc.add_PluginServicer_to_server(plugin, server)
    health_pb2_grpc.add_HealthServicer_to_server(health, server)

    port = get_open_port()

    server.add_insecure_port('[::]:' + port)
    server.start()

    print("1|1|tcp|127.0.0.1:"+port+"|grpc")
    sys.stdout.flush()

    try:
        server.wait_for_termination()
    except KeyboardInterrupt:
        pass
Пример #9
0
def create_server(server_address):
    interceptors = [ExceptionToStatusInterceptor()]
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10),
                         interceptors=interceptors)
    job_pb2_grpc.add_WorkspaceServicesServicer_to_server(
        WorkspaceServicer(), server)
    job_pb2_grpc.add_ModelApisServicesServicer_to_server(
        ModelApisServicer(), server)
    job_pb2_grpc.add_EnvironmentServicesServicer_to_server(
        EnvironmentServicer(), server)
    job_pb2_grpc.add_RunServicesServicer_to_server(RunServicer(), server)
    job_pb2_grpc.add_ExperimentServicesServicer_to_server(
        ExperimentServicer(), server)
    # Create a health check servicer. We use the non-blocking implementation
    # to avoid thread starvation.
    health_servicer = health.HealthServicer(
        experimental_non_blocking=True,
        experimental_thread_pool=futures.ThreadPoolExecutor(max_workers=1))
    health_pb2_grpc.add_HealthServicer_to_server(health_servicer, server)

    port = server.add_insecure_port(server_address)
    return server, port
Пример #10
0
async def launch_server(bind_address, port, server_id):
    """Start async grpcio server sub process"""
    server = grpc.aio.server(futures.ThreadPoolExecutor(max_workers=5, ))
    takeorders_pb2_grpc.add_OrderServicer_to_server(
        StoreService.Store(server_id, port, API_URL, API_KEY), server)

    health_servicer = health.HealthServicer(
        experimental_non_blocking=True,
        experimental_thread_pool=futures.ThreadPoolExecutor(max_workers=1))
    health_pb2_grpc.add_HealthServicer_to_server(health_servicer, server)
    services = tuple(service.full_name for service in takeorders_pb2.DESCRIPTOR.services_by_name.values()) \
               + (reflection.SERVICE_NAME, health.SERVICE_NAME)
    reflection.enable_server_reflection(services, server)
    server.add_insecure_port(bind_address)

    await server.start()

    # Mark all services as healthy.
    overall_server_health = ""
    for service in services + (overall_server_health, ):
        health_servicer.set(service, health_pb2.HealthCheckResponse.SERVING)
    await server.wait_for_termination()
Пример #11
0
def grpc_serve():
    # the +5 you see below re: max_workers is a hack to avoid thread starvation
    # working on a proper workaround
    server = grpc.server(
        futures.ThreadPoolExecutor(max_workers=multiprocessing.cpu_count() +
                                   5),
        interceptors=(PromServerInterceptor(), ))  # interceptor for metrics

    # Add the application servicer to the server.
    whereami_pb2_grpc.add_WhereamiServicer_to_server(WhereamigRPC(), server)

    # Create a health check servicer. We use the non-blocking implementation
    # to avoid thread starvation.
    health_servicer = health.HealthServicer(
        experimental_non_blocking=True,
        experimental_thread_pool=futures.ThreadPoolExecutor(max_workers=1))
    health_pb2_grpc.add_HealthServicer_to_server(health_servicer, server)

    # Create a tuple of all of the services we want to export via reflection.
    services = tuple(
        service.full_name
        for service in whereami_pb2.DESCRIPTOR.services_by_name.values()) + (
            reflection.SERVICE_NAME, health.SERVICE_NAME)

    # Start an end point to expose metrics at host:$grpc_metrics_port/metrics
    start_http_server(grpc_metrics_port)  # starts a flask server for metrics

    # Add the reflection service to the server.
    reflection.enable_server_reflection(services, server)
    server.add_insecure_port('[::]:' + str(grpc_serving_port))
    server.start()

    # Mark all services as healthy.
    overall_server_health = ""
    for service in services + (overall_server_health, ):
        health_servicer.set(service, health_pb2.HealthCheckResponse.SERVING)

    # Park the main application thread.
    server.wait_for_termination()
Пример #12
0
def serve():
    # We need to build a health service to work with go-plugin
    health = HealthServicer()
    health.set("plugin",
               health_pb2.HealthCheckResponse.ServingStatus.Value('SERVING'))

    # Start the server.
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
    kv_pb2_grpc.add_KVServicer_to_server(KVServicer(), server)
    health_pb2_grpc.add_HealthServicer_to_server(health, server)
    server.add_insecure_port(':1234')
    server.start()

    # Output information
    print("1|1|tcp|127.0.0.1:1234|grpc")
    sys.stdout.flush()

    try:
        while True:
            time.sleep(60 * 60 * 24)
    except KeyboardInterrupt:
        server.stop(0)
Пример #13
0
    async def async_setup(self):
        """Wait for the GRPC server to start"""
        self._grpc_server = grpc.aio.server(options=[
            ('grpc.max_send_message_length', -1),
            ('grpc.max_receive_message_length', -1),
        ])

        jina_pb2_grpc.add_JinaSingleDataRequestRPCServicer_to_server(
            self, self._grpc_server)
        jina_pb2_grpc.add_JinaDataRequestRPCServicer_to_server(
            self, self._grpc_server)
        jina_pb2_grpc.add_JinaDiscoverEndpointsRPCServicer_to_server(
            self, self._grpc_server)
        jina_pb2_grpc.add_JinaInfoRPCServicer_to_server(
            self, self._grpc_server)
        service_names = (
            jina_pb2.DESCRIPTOR.services_by_name['JinaSingleDataRequestRPC'].
            full_name,
            jina_pb2.DESCRIPTOR.services_by_name['JinaDataRequestRPC'].
            full_name,
            jina_pb2.DESCRIPTOR.services_by_name['JinaDiscoverEndpointsRPC'].
            full_name,
            jina_pb2.DESCRIPTOR.services_by_name['JinaInfoRPC'].full_name,
            reflection.SERVICE_NAME,
        )
        # Mark all services as healthy.
        health_pb2_grpc.add_HealthServicer_to_server(self._health_servicer,
                                                     self._grpc_server)

        for service in service_names:
            self._health_servicer.set(service,
                                      health_pb2.HealthCheckResponse.SERVING)
        reflection.enable_server_reflection(service_names, self._grpc_server)

        bind_addr = f'0.0.0.0:{self.args.port}'
        self._grpc_server.add_insecure_port(bind_addr)
        self.logger.debug(f'start listening on {bind_addr}')
        await self._grpc_server.start()
Пример #14
0
    def __init__(self,
                 proc: EventProcessor,
                 address: str,
                 port: int = 0,
                 *,
                 register: bool = False,
                 events_address: Optional[str] = None,
                 processor_id: Optional[str] = None,
                 workers: Optional[int] = None,
                 params: Optional[Mapping[str, Any]] = None):
        self.pr = proc
        self.address = address
        self._port = port
        self.processor_id = processor_id or proc.metadata['name']
        self.params = params or {}
        self.events_address = events_address

        self._health_servicer = health.HealthServicer()
        self._health_servicer.set('', 'SERVING')
        self._servicer = _ProcessorServicer(
            config=Config(),
            pr=proc,
            address=address,
            health_servicer=self._health_servicer,
            register=register,
            processor_id=processor_id,
            params=params,
            events_address=events_address)
        workers = workers or 10
        thread_pool = ThreadPoolExecutor(max_workers=workers)
        self._server = grpc.server(thread_pool)
        health_pb2_grpc.add_HealthServicer_to_server(self._health_servicer,
                                                     self._server)
        processing_pb2_grpc.add_ProcessorServicer_to_server(
            self._servicer, self._server)
        self._port = self._server.add_insecure_port("{}:{}".format(
            self.address, self.port))
        self._stopped_event = threading.Event()
Пример #15
0
def _configure_maintenance_server(server: grpc.Server,
                                  maintenance_port: int) -> None:
    listen_address = f"{_LISTEN_HOST}:{maintenance_port}"
    server.add_insecure_port(listen_address)

    # Create a health check servicer. We use the non-blocking implementation
    # to avoid thread starvation.
    health_servicer = health.HealthServicer(
        experimental_non_blocking=True,
        experimental_thread_pool=futures.ThreadPoolExecutor(
            max_workers=_THREAD_POOL_SIZE))

    # Create a tuple of all of the services we want to export via reflection.
    services = tuple(
        service.full_name
        for service in helloworld_pb2.DESCRIPTOR.services_by_name.values()) + (
            reflection.SERVICE_NAME, health.SERVICE_NAME)

    # Mark all services as healthy.
    health_pb2_grpc.add_HealthServicer_to_server(health_servicer, server)
    for service in services:
        health_servicer.set(service, health_pb2.HealthCheckResponse.SERVING)
    reflection.enable_server_reflection(services, server)
Пример #16
0
def start(dummy_mode):
    # Create gRPC server channel to receive requests from checkout (client).
    interceptor = server_interceptor(trace.get_tracer_provider())
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10),
                         interceptors=(interceptor, ))

    service = None
    if dummy_mode:
        service = DummyEmailService()
    else:
        raise Exception("non-dummy mode not implemented yet")

    demo_pb2_grpc.add_EmailServiceServicer_to_server(service, server)
    health_pb2_grpc.add_HealthServicer_to_server(service, server)

    port = os.environ.get("PORT", "8080")
    logger.info("listening on port: " + port)
    server.add_insecure_port("[::]:" + port)
    server.start()
    try:
        while True:
            time.sleep(3600)
    except KeyboardInterrupt:
        server.stop(0)
Пример #17
0
def serve(client_id, host='127.0.0.1', port='1234'):

    # create client registry server
    reg_server = RegistryServer()
    reg_server.start()

    reg_server.wait_complete()

    # create client registry
    registry = RegistryServer.GET_CLIENT_REGISTRY()

    # create a call counter for call to API
    call_counter = CallCounter()

    # only import command executor for server startup
    from apicmd import NetAppCommandExecutor
    # create new NetApp Command Executor
    executor = NetAppCommandExecutor()
    # create the servicer
    servicer = NetAppApiServicer(registry, call_counter, executor)

    # generate gRPC connection message and store in registry
    grpc_msg = "1|1|tcp|" + host + ":" + port + "|grpc"
    if not registry.set_grpc_msg(grpc_msg):
        LOGGER.error('could not set gRPC message in registry')

        registry.shutdown()

        reg_server.wait_complete()

        reg_server.terminate()
        reg_server.join()
        sys.exit(1)

    # register client
    if not register_client(registry, client_id):
        LOGGER.error('register server start client in registry')

        registry.shutdown()

        reg_server.wait_complete()

        reg_server.terminate()
        reg_server.join()
        sys.exit(1)

    # We need to build a health service to work with go-plugin
    health = HealthServicer()
    health.set("plugin",
               health_pb2.HealthCheckResponse.ServingStatus.Value('SERVING'))

    # Start the server.
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
    grpcapi_pb2_grpc.add_GRPCNetAppApiServicer_to_server(servicer, server)
    health_pb2_grpc.add_HealthServicer_to_server(health, server)
    server.add_insecure_port(host + ':' + port)
    server.start()

    LOGGER.debug('started GRPC server @ %s:%s', host, port)

    # let GRPC know we are here and good...
    notify_grpc(client_id, registry)

    # set registry status to running
    if not registry.set_api_status(ApiStatus.running):
        LOGGER.error('could not set api status in registry')

        server.stop(0)
        registry.shutdown()

        reg_server.wait_complete()

        reg_server.terminate()
        reg_server.join()
        sys.exit(1)

    # create running status file
    # source: https://stackoverflow.com/a/12654798
    with open(RUNNING_FILE, 'a'):
        os.utime(RUNNING_FILE, None)

    LOGGER.debug('running file created')

    try:
        # check if counter thinks we should continue
        # either by init or calls received/pending
        while call_counter.stay_alive():
            # get the number of calls received + reset
            call_cnt = call_counter.get_call_cnt()
            LOGGER.debug("active calls, was needed %d times", call_cnt)

            # wait for CHECK_TIMEOUT to receive calls
            time.sleep(CHECK_TIMEOUT)

    except KeyboardInterrupt:
        pass

    # create stopping status file
    with open(STOPPING_FILE, 'a'):
        os.utime(STOPPING_FILE, None)

    LOGGER.debug("left while loop, issuing server.stop()")
    unregister_client(registry, client_id)
    server.stop(0)
    os.remove(RUNNING_FILE)  # making sure we are not running
    registry.shutdown()

    # sleep until server indicates shutdown complete
    reg_server.wait_complete()

    reg_server.terminate()
    reg_server.join()

    os.remove(STOPPING_FILE)  # making sure we also declare stopped

    LOGGER.debug("exiting netapp API serve()")
Пример #18
0
 def register_services(self):
     calculate_pb2_grpc.add_CalculateServicer_to_server(
         CalculatorServicerImpl(), self.app)
     health_pb2_grpc.add_HealthServicer_to_server(HealthServicerImpl(),
                                                  self.app)
Пример #19
0
 def _add_to_server(self, server):
     health_pb2_grpc.add_HealthServicer_to_server(self, server)
     return self
Пример #20
0
    def serve(self, wait=False):
        """
        Start serving the plugin grpc services, and return control
        to the called. If ``wait=True``, block until the server is stopped.

        If ``False`` is returned, caller can check `.error_msg` to read the
        last error message.

        Args:
            wait (bool): Block until server stops

        Returns:
            bool: Return True on successful start
        """
        self.stop()
        self._error = ''

        if not self.check_magic_key():
            return False

        self._server = server = self.server()

        # We need to build a health service to work with go-plugin
        health = HealthServicer()
        health.set(
            self.GRPC_SERVICE_NAME,
            health_pb2.HealthCheckResponse.ServingStatus.Value('SERVING'))
        health_pb2_grpc.add_HealthServicer_to_server(health, server)

        # enable controller
        _controller_pb2_grpc.add_GRPCControllerServicer_to_server(
            ServerController(server), server)

        # instrument the server to capture the registration of the plugin
        # services, so that we can automatically add them for reflection
        _add_generic_rpc_handlers = server.add_generic_rpc_handlers
        plugin_service_names = set()

        def add_generic_rpc_handlers(self, handlers):
            plugin_service_names.update({h.service_name() for h in handlers})
            return _add_generic_rpc_handlers(handlers)

        server.add_generic_rpc_handlers = add_generic_rpc_handlers.__get__(
            server, server.__class__)

        # Register all plugins
        plugins = self._cfg.plugins
        for name in plugins:
            plugin = plugins[name]
            plugin.server_register(server)

        # reset the handler and set up reflection
        server.add_generic_rpc_handlers = _add_generic_rpc_handlers
        if plugin_service_names:
            names = list(plugin_service_names)
            logging.info(
                "plugin server installing grpc reflection for plugins: %s",
                names)
            names.append(reflection.SERVICE_NAME)
            reflection.enable_server_reflection(names, server)

        # configure server endpoint
        if os.name == 'posix':
            fd, sock_path = tempfile.mkstemp(suffix=".sock", prefix="plugin_")
            os.close(fd)
            os.unlink(sock_path)
            endpoint = os.path.abspath(sock_path)
            server.add_insecure_port("unix:" + endpoint)
            network = 'unix'
        else:
            port = 0
            port_opts = {}
            try:
                port_opts['min_port'] = int(
                    os.environ.get('PLUGIN_MIN_PORT', ''))
            except ValueError:
                pass
            try:
                port_opts['max_port'] = int(
                    os.environ.get('PLUGIN_MAX_PORT', ''))
            except ValueError:
                pass
            if port_opts:
                port = pygo_plugin.utils.find_free_port(**port_opts)
            port = server.add_insecure_port('127.0.0.1:{}'.format(port))
            network = 'tcp'
            endpoint = '127.0.0.1:%d' % port

        server.start()

        # Output information
        handshake = "{proto_ver}|{app_proto_ver}|{network}|{endpoint}|{protocol}".format(
            proto_ver=_GO_PLUGIN_PROTOCOL_VER,
            app_proto_ver=self._cfg.handshake_config.protocol_version,
            network=network,
            endpoint=endpoint,
            protocol='grpc',
        )
        # logging.info(handshake)
        print(handshake)
        sys.stdout.flush()

        if wait:
            server.wait_for_termination()

        return True
Пример #21
0
def serve(max_workers=10, port=7777):
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=max_workers))
    grpc_bt_grpc.add_ForecastServicer_to_server(ForecastServicer(), server)
    heartb_pb2_grpc.add_HealthServicer_to_server(HealthServicer(), server)
    server.add_insecure_port("[::]:{}".format(port))
    return server
Пример #22
0
    def __init__(
        self,
        host="localhost",
        port=None,
        socket=None,
        max_workers=None,
        loadable_target_origin=None,
        heartbeat=False,
        heartbeat_timeout=30,
        lazy_load_user_code=False,
        ipc_output_file=None,
        fixed_server_id=None,
        entry_point=None,
        container_context=None,
    ):
        check.opt_str_param(host, "host")
        check.opt_int_param(port, "port")
        check.opt_str_param(socket, "socket")
        check.opt_int_param(max_workers, "max_workers")
        check.opt_inst_param(loadable_target_origin, "loadable_target_origin",
                             LoadableTargetOrigin)
        check.invariant(
            port is not None if seven.IS_WINDOWS else True,
            "You must pass a valid `port` on Windows: `socket` not supported.",
        )
        check.invariant(
            (port or socket) and not (port and socket),
            "You must pass one and only one of `port` or `socket`.",
        )
        check.invariant(
            host is not None if port else True,
            "Must provide a host when serving on a port",
        )
        check.bool_param(heartbeat, "heartbeat")
        check.int_param(heartbeat_timeout, "heartbeat_timeout")
        self._ipc_output_file = check.opt_str_param(ipc_output_file,
                                                    "ipc_output_file")
        check.opt_str_param(fixed_server_id, "fixed_server_id")

        check.invariant(heartbeat_timeout > 0,
                        "heartbeat_timeout must be greater than 0")
        check.invariant(
            max_workers is None or max_workers > 1 if heartbeat else True,
            "max_workers must be greater than 1 or set to None if heartbeat is True. "
            "If set to None, the server will use the gRPC default.",
        )

        self.server = grpc.server(
            ThreadPoolExecutor(max_workers=max_workers),
            compression=grpc.Compression.Gzip,
            options=[
                ("grpc.max_send_message_length", max_send_bytes()),
                ("grpc.max_receive_message_length", max_rx_bytes()),
            ],
        )
        self._server_termination_event = threading.Event()

        try:
            self._api_servicer = DagsterApiServer(
                server_termination_event=self._server_termination_event,
                loadable_target_origin=loadable_target_origin,
                heartbeat=heartbeat,
                heartbeat_timeout=heartbeat_timeout,
                lazy_load_user_code=lazy_load_user_code,
                fixed_server_id=fixed_server_id,
                entry_point=entry_point,
                container_context=container_context,
            )
        except Exception:
            if self._ipc_output_file:
                with ipc_write_stream(self._ipc_output_file) as ipc_stream:
                    ipc_stream.send(
                        GrpcServerLoadErrorEvent(
                            error_info=serializable_error_info_from_exc_info(
                                sys.exc_info())))
            raise

        # Create a health check servicer
        self._health_servicer = health.HealthServicer()
        health_pb2_grpc.add_HealthServicer_to_server(self._health_servicer,
                                                     self.server)

        add_DagsterApiServicer_to_server(self._api_servicer, self.server)

        if port:
            server_address = host + ":" + str(port)
        else:
            server_address = "unix:" + os.path.abspath(socket)

        # grpc.Server.add_insecure_port returns:
        # - 0 on failure
        # - port number when a port is successfully bound
        # - 1 when a UDS is successfully bound
        res = self.server.add_insecure_port(server_address)
        if socket and res != 1:
            if self._ipc_output_file:
                with ipc_write_stream(self._ipc_output_file) as ipc_stream:
                    ipc_stream.send(GrpcServerFailedToBindEvent())
            raise CouldNotBindGrpcServerToAddress(socket)
        if port and res != port:
            if self._ipc_output_file:
                with ipc_write_stream(self._ipc_output_file) as ipc_stream:
                    ipc_stream.send(GrpcServerFailedToBindEvent())
            raise CouldNotBindGrpcServerToAddress(port)
        logger.error(traceback.print_exc())
        pass

    port = os.environ.get('PORT', "8080")
    catalog_addr = os.environ.get('PRODUCT_CATALOG_SERVICE_ADDR', '')
    if catalog_addr == "":
        raise Exception('PRODUCT_CATALOG_SERVICE_ADDR environment variable not set')
    logger.info("product catalog address: " + catalog_addr)
    channel = grpc.insecure_channel(catalog_addr)
    product_catalog_stub = demo_pb2_grpc.ProductCatalogServiceStub(channel)

    # create gRPC server
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10)) # ,interceptors=(tracer_interceptor,))

    # add class to gRPC server
    service = RecommendationService()
    demo_pb2_grpc.add_RecommendationServiceServicer_to_server(service, server)
    health_pb2_grpc.add_HealthServicer_to_server(service, server)

    # start server
    logger.info("listening on port: " + port)
    server.add_insecure_port('[::]:'+port)
    server.start()

    # keep alive
    try:
         while True:
            time.sleep(10000)
    except KeyboardInterrupt:
            server.stop(0)
Пример #24
0
        for logger_name in 'urllib3', 'google_cloud_storage.auth.transport.requests', 'paramiko', 'cassandra':
            logging.getLogger(logger_name).setLevel(logging.WARN)


def shutdown(signum, frame):
    logging.info("shutting down")
    server.stop(0)


if len(sys.argv) > 2:
    config_file_path = sys.argv[2]
else:
    config_file_path = "/etc/medusa/medusa.ini"

config = create_config(config_file_path)
configure_console_logging(config.logging)

server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))

medusa_pb2_grpc.add_MedusaServicer_to_server(MedusaService(config), server)
health_pb2_grpc.add_HealthServicer_to_server(
    grpc_health.v1.health.HealthServicer(), server)

logging.info('Starting server. Listening on port 50051.')
server.add_insecure_port('[::]:50051')
server.start()

signal.signal(signal.SIGTERM, shutdown)

server.wait_for_termination()
Пример #25
0
def serve(jobs):
    # Cache the jobs list for later processing.
	# We first have to translate given jobs to different structure.
    for job in jobs:
        # Create proto object
        p = plugin_pb2.Job()

        # Manual interaction
        if job.interaction != None:
            p.interaction.description = job.interaction.description
            p.interaction.type = job.interaction.inputType
            p.interaction.value = job.interaction.value

        # Arguments
        args = []
        if job.args:
            for arg in job.args:
                a = plugin_pb2.Argument()
                a.description = arg.description
                a.type = arg.inputType.value
                a.key = arg.key
                a.value = arg.value

                args.append(a)

        # Set the rest of the fields
        jobTitle = bytes(job.title) if six.PY2 else bytes(job.title, 'utf8')
        p.unique_id = fnv1a_32(jobTitle)
        p.title = job.title
        p.description = job.description
        p.args.extend(args)

        # Resolve dependencies
        if job.dependsOn:
            for depJob in job.dependsOn:
                for currJob in jobs:
                    if depJob.lower() == currJob.title.lower():
                        title = bytes(currJob.title) if six.PY2 else bytes(currJob.title, 'utf8')
                        p.dependson.append(fnv1a_32(title))
                        foundDep = True
                        break

                if not foundDep:
                    raise Exception("job '" + job.title + "' has dependency '" + depJob + "' which is not declared")

        # job wrapper object for this job
        w = JobWrapper(job.handler, p)
        cachedJobs.append(w)

    # Check if two jobs have the same title which is restricted
    for x, job in enumerate(cachedJobs):
        for y, innerJob in enumerate(cachedJobs):
            if x != y and job.job.unique_id == innerJob.job.unique_id:
                raise Exception("duplicate job found (two jobs with same title)")

    # get certificate path from environment variables
    certPath = os.environ['GAIA_PLUGIN_CERT']
    keyPath = os.environ['GAIA_PLUGIN_KEY']
    caCertPath = os.environ['GAIA_PLUGIN_CA_CERT']

    # check if all certs are available
    if not os.path.isfile(certPath):
        raise Exception("cannot find path to certificate")
    if not os.path.isfile(keyPath):
        raise Exception("cannot find path to key")
    if not os.path.isfile(caCertPath):
        raise Exception("cannot find path to root certificate")

    # Open files
    private_key = open(keyPath).read()
    certificate_chain = open(certPath).read()
    root_cert = open(caCertPath).read()

    if six.PY3:
        private_key = bytes(private_key, 'utf8')
        certificate_chain = bytes(certificate_chain, 'utf8')
        root_cert = bytes(root_cert, 'utf8')

    # We need to build a health service to work with go-plugin
    health = HealthServicer()
    health.set("plugin", health_pb2.HealthCheckResponse.ServingStatus.Value('SERVING'))

    # Start the server.
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=20))
    private_key_certificate_chain_pairs = ( (private_key, certificate_chain), )
    server_credentials = grpc.ssl_server_credentials(private_key_certificate_chain_pairs, root_cert, True)
    plugin_pb2_grpc.add_PluginServicer_to_server(GRPCServer(), server)
    health_pb2_grpc.add_HealthServicer_to_server(health, server)
    port = server.add_secure_port('127.0.0.1:0', server_credentials)
    server.start()

    # Output information
    print("1|2|tcp|127.0.0.1:" + str(port) + "|grpc")
    sys.stdout.flush()

    try:
        while True:
            time.sleep(_ONE_DAY_IN_SECONDS)
    except KeyboardInterrupt:
        server.stop(0)
Пример #26
0
    def __init__(
        self,
        host="localhost",
        port=None,
        socket=None,
        max_workers=1,
        loadable_target_origin=None,
        heartbeat=False,
        heartbeat_timeout=30,
        lazy_load_user_code=False,
        ipc_output_file=None,
        fixed_server_id=None,
    ):
        check.opt_str_param(host, "host")
        check.opt_int_param(port, "port")
        check.opt_str_param(socket, "socket")
        check.int_param(max_workers, "max_workers")
        check.opt_inst_param(loadable_target_origin, "loadable_target_origin", LoadableTargetOrigin)
        check.invariant(
            port is not None if seven.IS_WINDOWS else True,
            "You must pass a valid `port` on Windows: `socket` not supported.",
        )
        check.invariant(
            (port or socket) and not (port and socket),
            "You must pass one and only one of `port` or `socket`.",
        )
        check.invariant(
            host is not None if port else True, "Must provide a host when serving on a port",
        )
        check.bool_param(heartbeat, "heartbeat")
        check.int_param(heartbeat_timeout, "heartbeat_timeout")
        self._ipc_output_file = check.opt_str_param(ipc_output_file, "ipc_output_file")
        check.opt_str_param(fixed_server_id, "fixed_server_id")

        check.invariant(heartbeat_timeout > 0, "heartbeat_timeout must be greater than 0")
        check.invariant(
            max_workers > 1 if heartbeat else True,
            "max_workers must be greater than 1 if heartbeat is True",
        )

        self.server = grpc.server(ThreadPoolExecutor(max_workers=max_workers))
        self._server_termination_event = threading.Event()

        self._api_servicer = DagsterApiServer(
            server_termination_event=self._server_termination_event,
            loadable_target_origin=loadable_target_origin,
            heartbeat=heartbeat,
            heartbeat_timeout=heartbeat_timeout,
            lazy_load_user_code=lazy_load_user_code,
            fixed_server_id=fixed_server_id,
        )

        # Create a health check servicer
        self._health_servicer = health.HealthServicer()
        health_pb2_grpc.add_HealthServicer_to_server(self._health_servicer, self.server)

        add_DagsterApiServicer_to_server(self._api_servicer, self.server)

        if port:
            server_address = host + ":" + str(port)
        else:
            server_address = "unix:" + os.path.abspath(socket)

        # grpc.Server.add_insecure_port returns:
        # - 0 on failure
        # - port number when a port is successfully bound
        # - 1 when a UDS is successfully bound
        res = self.server.add_insecure_port(server_address)
        if socket and res != 1:
            if self._ipc_output_file:
                with ipc_write_stream(self._ipc_output_file) as ipc_stream:
                    ipc_stream.send(GrpcServerFailedToBindEvent())
            raise CouldNotBindGrpcServerToAddress(socket)
        if port and res != port:
            if self._ipc_output_file:
                with ipc_write_stream(self._ipc_output_file) as ipc_stream:
                    ipc_stream.send(GrpcServerFailedToBindEvent())
            raise CouldNotBindGrpcServerToAddress(port)
Пример #27
0
def serve(host='[::]',
          port=5000,
          max_workers=4,
          max_receive_message_length=None,
          max_send_message_length=None):

    if max_receive_message_length is None:
        max_receive_message_length = DEFAULT_MAX_RECEIVE_MESSAGE_LENGTH

    if max_send_message_length is None:
        max_send_message_length = DEFAULT_MAX_SEND_MESSAGE_LENGTH

    servicer = health.HealthServicer()

    servicer.set('', health_pb2.HealthCheckResponse.SERVING)

    # Asset
    servicer.set('get_asset', health_pb2.HealthCheckResponse.SERVING)
    servicer.set('add_asset', health_pb2.HealthCheckResponse.SERVING)
    servicer.set('update_asset', health_pb2.HealthCheckResponse.SERVING)
    servicer.set('remove_asset', health_pb2.HealthCheckResponse.SERVING)
    servicer.set('exists_asset', health_pb2.HealthCheckResponse.SERVING)
    servicer.set('get_asset_info', health_pb2.HealthCheckResponse.SERVING)
    servicer.set('get_task_state', health_pb2.HealthCheckResponse.SERVING)
    servicer.set('get_bucket', health_pb2.HealthCheckResponse.SERVING)
    servicer.set('query', health_pb2.HealthCheckResponse.SERVING)

    # Bucket
    servicer.set('add_bucket', health_pb2.HealthCheckResponse.SERVING)
    servicer.set('update_bucket', health_pb2.HealthCheckResponse.SERVING)
    servicer.set('remove_bucket', health_pb2.HealthCheckResponse.SERVING)
    servicer.set('exists_bucket', health_pb2.HealthCheckResponse.SERVING)
    servicer.set('get_assets', health_pb2.HealthCheckResponse.SERVING)

    options = [('grpc.max_receive_message_length', max_receive_message_length),
               ('grpc.max_send_message_length', max_send_message_length)]
    logging.info('Starting GRPC server with this options: %s', options)

    server = grpc.server(futures.ThreadPoolExecutor(max_workers=max_workers),
                         options=options)

    opac_pb2.add_AssetServiceServicer_to_server(Asset(), server)
    opac_pb2.add_BucketServiceServicer_to_server(AssetBucket(), server)

    # Health service
    health_pb2_grpc.add_HealthServicer_to_server(servicer, server)

    # Set port and Start Server
    server.add_insecure_port('{0}:{1}'.format(host, port))
    logging.info(
        'Started GRPC server on host: {0}, port: {1}, accept connections!'.
        format(host, port))
    server.start()

    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        logging.info('User stopping server...')
        server.stop(0)
        logging.info('Server stopped; exiting.')
    except Exception as e:
        logging.info('Caught exception "%s"; stopping server...', e)
        server.stop(0)
        logging.info('Server stopped; exiting.')
Пример #28
0
        raise Exception(
            'PRODUCT_CATALOG_SERVICE_ADDR environment variable not set')
    logger.info("product catalog address: " + catalog_addr)

    client_tracer_interceptor = client_interceptor.OpenCensusClientInterceptor(
        tracer=tracer, host_port=catalog_addr)
    channel = grpc.insecure_channel(catalog_addr)
    channel = grpc.intercept_channel(channel, client_tracer_interceptor)
    product_catalog_stub = demo_pb2_grpc.ProductCatalogServiceStub(channel)

    # create gRPC server
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10),
                         interceptors=(tracer_interceptor, ))

    # add class to gRPC server
    service = RecommendationService()
    demo_pb2_grpc.add_RecommendationServiceServicer_to_server(service, server)
    health_pb2_grpc.add_HealthServicer_to_server(service, server)

    # start server
    logger.info("listening on port: " + port)
    server.add_insecure_port('[::]:' + port)
    server.start()

    # keep alive
    try:
        while True:
            time.sleep(10000)
    except KeyboardInterrupt:
        server.stop(0)
Пример #29
0
def serve(
    handshake,
    proto_versions,
    tls_credentials=None,
    signal_handlers=True,
):
    """
    Start up a plugin server for the given protocol versions and announce it
    on stdout using the rpcplugin handshake.

    This function blocks until the client asks the server to exit.
    """

    if handshake == None or handshake.cookie_key == None or handshake.cookie_value == None:
        raise ValueError(
            "handshake must be set and must have cookie key and value", )

    if proto_versions == None or len(proto_versions) == 0:
        raise ValueError(
            "proto_versions must be a map with at least one protocol version defined"
        )

    got_cookie_value = os.environ.get(handshake.cookie_key, "")
    if got_cookie_value != handshake.cookie_value:
        raise Exception("calling program is not an rpcplugin host", )

    try:
        (v, impl) = _negotiate_protocol_version(proto_versions)
    except TypeError:
        raise Exception(
            "cannot support any protocol versions offered by the plugin client"
        )

    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))

    # Plugin healthcheck service is mandatory for rpcplugin servers
    health = HealthServicer()
    health.set(
        "plugin",
        health_pb2.HealthCheckResponse.ServingStatus.Value('SERVING'),
    )

    health_pb2_grpc.add_HealthServicer_to_server(health, server)
    impl(server)

    auto_cert_str = ""
    if tls_credentials == None:
        (creds, cert_str) = _auto_tls()
        tls_credentials = creds
        auto_cert_str = cert_str

    transports = os.environ.get("PLUGIN_TRANSPORTS", "tcp").split(",")
    port = None
    for transport in transports:
        if transport == "tcp":
            port = server.add_secure_port('0.0.0.0:0', tls_credentials)
            break
    if port is None:
        raise Exception(
            "cannot support any transports offered by the plugin client")

    if signal_handlers:
        # Ignore interrupt signals, because they're probably being sent to
        # the whole process group and we want the host program to handle
        # them, and decide for itself when it's time for this plugin server
        # to exit.
        signal.signal(signal.SIGINT, signal.SIG_IGN)

    server.start()

    # This handshake line tells the client where to connect and what protocol
    # to talk when it does.
    sys.stdout.write("1|%d|tcp|127.0.0.1:%d|grpc|%s\n" %
                     (v, port, str(auto_cert_str)))
    sys.stdout.flush()

    while True:
        time.sleep(60 * 60 * 24)