Commit 1e34eec7 authored by Lluis Gifre Renom's avatar Lluis Gifre Renom
Browse files

Intermediate backup of Device component re-work.

parent e76ec630
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -11,7 +11,7 @@ from common.orm.fields.PrimaryKeyField import PrimaryKeyField
from common.orm.fields.StringField import StringField
from common.orm.model.Model import Model
from context.proto.context_pb2 import ConfigActionEnum
from context.service.database.Tools import fast_hasher, grpc_to_enum, remove_dict_key
from .Tools import fast_hasher, grpc_to_enum, remove_dict_key

LOGGER = logging.getLogger(__name__)

+2 −2
Original line number Diff line number Diff line
@@ -4,8 +4,8 @@ from common.orm.fields.ForeignKeyField import ForeignKeyField
from common.orm.fields.PrimaryKeyField import PrimaryKeyField
from common.orm.fields.StringField import StringField
from common.orm.model.Model import Model
from context.service.database.DeviceModel import DeviceModel
from context.service.database.TopologyModel import TopologyModel
from .DeviceModel import DeviceModel
from .TopologyModel import TopologyModel

LOGGER = logging.getLogger(__name__)

+1 −1
Original line number Diff line number Diff line
@@ -29,7 +29,7 @@ class ContextService:
        self.server = None

    def start(self):
        self.endpoint = '{:s}:{:s}'.format(self.address, str(self.port))
        self.endpoint = '{:s}:{:s}'.format(str(self.address), str(self.port))
        LOGGER.info('Starting Service (tentative endpoint: {:s}, max_workers: {:s})...'.format(
            str(self.endpoint), str(self.max_workers)))

+16 −11
Original line number Diff line number Diff line
@@ -4,17 +4,22 @@ 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 device.Config import GRPC_SERVICE_PORT, GRPC_MAX_WORKERS, GRPC_GRACE_PERIOD
from device.proto.device_pb2_grpc import add_DeviceServiceServicer_to_server
from device.service.DeviceServiceServicerImpl import DeviceServiceServicerImpl
from device.Config import GRPC_SERVICE_PORT, GRPC_MAX_WORKERS, GRPC_GRACE_PERIOD
from device.service.data_cache.DataCache import DataCache
from device.service.driver_api.DriverInstanceCache import DriverInstanceCache

BIND_ADDRESS = '0.0.0.0'
LOGGER = logging.getLogger(__name__)

class DeviceService:
    def __init__(self, database, address=BIND_ADDRESS, port=GRPC_SERVICE_PORT, max_workers=GRPC_MAX_WORKERS,
                 grace_period=GRPC_GRACE_PERIOD):
        self.database = database
    def __init__(
        self, data_cache : DataCache, driver_instance_cache : DriverInstanceCache,
        address=BIND_ADDRESS, port=GRPC_SERVICE_PORT, max_workers=GRPC_MAX_WORKERS, grace_period=GRPC_GRACE_PERIOD):

        self.data_cache = data_cache
        self.driver_instance_cache = driver_instance_cache
        self.address = address
        self.port = port
        self.endpoint = None
@@ -26,14 +31,14 @@ class DeviceService:
        self.server = None

    def start(self):
        self.endpoint = '{}:{}'.format(self.address, self.port)
        LOGGER.debug('Starting Service (tentative endpoint: {}, max_workers: {})...'.format(
            self.endpoint, self.max_workers))
        self.endpoint = '{:s}:{:s}'.format(str(self.address), str(self.port))
        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,))

        self.device_servicer = DeviceServiceServicerImpl(self.database)
        self.device_servicer = DeviceServiceServicerImpl(self.data_cache, self.driver_instance_cache)
        add_DeviceServiceServicer_to_server(self.device_servicer, self.server)

        self.health_servicer = HealthServicer(
@@ -41,15 +46,15 @@ class DeviceService:
        add_HealthServicer_to_server(self.health_servicer, self.server)

        port = self.server.add_insecure_port(self.endpoint)
        self.endpoint = '{}:{}'.format(self.address, port)
        LOGGER.info('Listening on {}...'.format(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 {} seconds)...'.format(self.grace_period))
        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')
Loading