Scheduled maintenance on Saturday, 27 September 2025, from 07:00 AM to 4:00 PM GMT (09:00 AM to 6:00 PM CEST) - some services may be unavailable -

Skip to content
Snippets Groups Projects
ContextService.py 2.52 KiB
Newer Older
  • Learn to ignore specific revisions
  • import logging
    from concurrent import futures
    from grpc_health.v1.health import HealthServicer, OVERALL_HEALTH
    from grpc_health.v1.health_pb2 import HealthCheckResponse
    from grpc_health.v1.health_pb2_grpc import add_HealthServicer_to_server
    
    from context.Config import GRPC_SERVICE_PORT, GRPC_MAX_WORKERS, GRPC_GRACE_PERIOD
    
    from context.proto.context_pb2_grpc import add_ContextServiceServicer_to_server
    from .ContextServiceServicerImpl import ContextServiceServicerImpl
    
    
    BIND_ADDRESS = '0.0.0.0'
    LOGGER = logging.getLogger(__name__)
    
    class ContextService:
    
    Lluis Gifre Renom's avatar
    Lluis Gifre Renom committed
        def __init__(
            self, database, messagebroker, address=BIND_ADDRESS, port=GRPC_SERVICE_PORT, max_workers=GRPC_MAX_WORKERS,
            grace_period=GRPC_GRACE_PERIOD):
    
    
            self.database = database
    
    Lluis Gifre Renom's avatar
    Lluis Gifre Renom committed
            self.messagebroker = messagebroker
    
            self.address = address
            self.port = port
            self.endpoint = None
            self.max_workers = max_workers
            self.grace_period = grace_period
            self.context_servicer = None
            self.health_servicer = None
            self.pool = None
            self.server = None
    
        def start(self):
    
    Lluis Gifre Renom's avatar
    Lluis Gifre Renom committed
            self.endpoint = '{:s}:{:s}'.format(str(self.address), str(self.port))
    
    Lluis Gifre Renom's avatar
    Lluis Gifre Renom committed
            LOGGER.info('Starting Service (tentative endpoint: {:s}, max_workers: {:s})...'.format(
    
                str(self.endpoint), str(self.max_workers)))
    
    
            self.pool = futures.ThreadPoolExecutor(max_workers=self.max_workers)
            self.server = grpc.server(self.pool) # , interceptors=(tracer_interceptor,))
    
    
    Lluis Gifre Renom's avatar
    Lluis Gifre Renom committed
            self.context_servicer = ContextServiceServicerImpl(self.database, self.messagebroker)
    
            add_ContextServiceServicer_to_server(self.context_servicer, self.server)
    
            self.health_servicer = HealthServicer(
                experimental_non_blocking=True, experimental_thread_pool=futures.ThreadPoolExecutor(max_workers=1))
            add_HealthServicer_to_server(self.health_servicer, self.server)
    
            port = self.server.add_insecure_port(self.endpoint)
    
            self.endpoint = '{:s}:{:s}'.format(str(self.address), str(port))
            LOGGER.info('Listening on {:s}...'.format(str(self.endpoint)))
    
            self.server.start()
            self.health_servicer.set(OVERALL_HEALTH, HealthCheckResponse.SERVING) # pylint: disable=maybe-no-member
    
            LOGGER.debug('Service started')
    
        def stop(self):
    
            LOGGER.debug('Stopping service (grace period {:s} seconds)...'.format(str(self.grace_period)))
    
            self.health_servicer.enter_graceful_shutdown()
            self.server.stop(self.grace_period)
            LOGGER.debug('Service stopped')