Beispiel #1
0
class IconService(object):

    """IconScore service for stand alone start.
    It provides gRPC interface for peer_service to communicate with icon service.
    Its role is the bridge between loopchain and IconServiceEngine.
    """

    def __init__(self):
        self._icon_score_queue_name = None
        self._amqp_target = None
        self._inner_service = None

    def serve(self, config: 'IconConfig'):
        async def _serve():
            await self._inner_service.connect(exclusive=True)
            Logger.info(f'Start IconService Service serve!', ICON_SERVICE)

        channel = config[ConfigKey.CHANNEL]
        amqp_key = config[ConfigKey.AMQP_KEY]
        amqp_target = config[ConfigKey.AMQP_TARGET]
        score_root_path = config[ConfigKey.SCORE_ROOT_PATH]
        db_root_path = config[ConfigKey.STATE_DB_ROOT_PATH]

        self._set_icon_score_stub_params(channel, amqp_key, amqp_target)

        Logger.info(f'==========IconService Service params==========', ICON_SERVICE)
        Logger.info(f'score_root_path : {score_root_path}', ICON_SERVICE)
        Logger.info(f'icon_score_state_db_root_path  : {db_root_path}', ICON_SERVICE)
        Logger.info(f'amqp_target  : {amqp_target}', ICON_SERVICE)
        Logger.info(f'amqp_key  :  {amqp_key}', ICON_SERVICE)
        Logger.info(f'icon_score_queue_name  : {self._icon_score_queue_name}', ICON_SERVICE)
        Logger.info(f'==========IconService Service params==========', ICON_SERVICE)

        self._inner_service = IconScoreInnerService(amqp_target, self._icon_score_queue_name, conf=config)

        loop = MessageQueueService.loop
        loop.create_task(_serve())
        loop.add_signal_handler(signal.SIGINT, self.close)
        loop.add_signal_handler(signal.SIGTERM, self.close)

        try:
            loop.run_forever()
        finally:
            """
            If the function is called when the operation is not an endless loop 
            in an asynchronous function, the await is terminated immediately.
            """
            loop.run_until_complete(loop.shutdown_asyncgens())
            loop.close()

    def close(self):
        self._inner_service.clean_close()

    def _set_icon_score_stub_params(self, channel: str, amqp_key: str, amqp_target: str):
        self._icon_score_queue_name = \
            ICON_SCORE_QUEUE_NAME_FORMAT.format(channel_name=channel, amqp_key=amqp_key)
        self._amqp_target = amqp_target
Beispiel #2
0
    def serve(self, config: 'IconConfig'):
        async def _serve():
            await self._inner_service.connect(exclusive=True)
            Logger.info(f'Start IconService Service serve!', ICON_SERVICE_STANDALONE)

        channel = config[ConfigKey.CHANNEL]
        amqp_key = config[ConfigKey.AMQP_KEY]
        amqp_target = config[ConfigKey.AMQP_TARGET]
        score_root_path = config[ConfigKey.SCORE_ROOT_PATH]
        db_root_patn = config[ConfigKey.STATE_DB_ROOT_PATH]

        self._set_icon_score_stub_params(channel, amqp_key, amqp_target)

        Logger.info(f'==========IconService Service params==========', ICON_SERVICE_STANDALONE)
        Logger.info(f'score_root_path : {score_root_path}', ICON_SERVICE_STANDALONE)
        Logger.info(f'icon_score_state_db_root_path  : {db_root_patn}', ICON_SERVICE_STANDALONE)
        Logger.info(f'amqp_target  : {amqp_target}', ICON_SERVICE_STANDALONE)
        Logger.info(f'amqp_key  :  {amqp_key}', ICON_SERVICE_STANDALONE)
        Logger.info(f'icon_score_queue_name  : {self._icon_score_queue_name}', ICON_SERVICE_STANDALONE)
        Logger.info(f'==========IconService Service params==========', ICON_SERVICE_STANDALONE)

        self._inner_service = IconScoreInnerService(amqp_target, self._icon_score_queue_name, conf=config)

        loop = MessageQueueService.loop
        loop.create_task(_serve())
        loop.run_forever()
Beispiel #3
0
    def serve(self, config: 'IconConfig'):
        async def _serve():
            await self._inner_service.connect(exclusive=True)
            Logger.info(f'Start IconService Service serve!', ICON_SERVICE)

        channel = config[ConfigKey.CHANNEL]
        amqp_key = config[ConfigKey.AMQP_KEY]
        amqp_target = config[ConfigKey.AMQP_TARGET]
        score_root_path = config[ConfigKey.SCORE_ROOT_PATH]
        db_root_path = config[ConfigKey.STATE_DB_ROOT_PATH]

        self._set_icon_score_stub_params(channel, amqp_key, amqp_target)

        Logger.info(f'==========IconService Service params==========', ICON_SERVICE)
        Logger.info(f'score_root_path : {score_root_path}', ICON_SERVICE)
        Logger.info(f'icon_score_state_db_root_path  : {db_root_path}', ICON_SERVICE)
        Logger.info(f'amqp_target  : {amqp_target}', ICON_SERVICE)
        Logger.info(f'amqp_key  :  {amqp_key}', ICON_SERVICE)
        Logger.info(f'icon_score_queue_name  : {self._icon_score_queue_name}', ICON_SERVICE)
        Logger.info(f'==========IconService Service params==========', ICON_SERVICE)

        self._inner_service = IconScoreInnerService(amqp_target, self._icon_score_queue_name, conf=config)

        loop = MessageQueueService.loop
        loop.create_task(_serve())
        loop.add_signal_handler(signal.SIGINT, self.close)
        loop.add_signal_handler(signal.SIGTERM, self.close)

        try:
            loop.run_forever()
        finally:
            """
            If the function is called when the operation is not an endless loop 
            in an asynchronous function, the await is terminated immediately.
            """
            loop.run_until_complete(loop.shutdown_asyncgens())
            loop.close()
Beispiel #4
0
    def serve(self, config: 'IconConfig'):
        async def _serve():
            await self._inner_service.connect(exclusive=True)
            Logger.info(f'Start IconService Service serve!', ICON_SERVICE)

        channel = config[ConfigKey.CHANNEL]
        amqp_key = config[ConfigKey.AMQP_KEY]
        amqp_target = config[ConfigKey.AMQP_TARGET]
        score_root_path = config[ConfigKey.SCORE_ROOT_PATH]
        db_root_path = config[ConfigKey.STATE_DB_ROOT_PATH]

        self._set_icon_score_stub_params(channel, amqp_key, amqp_target)

        Logger.info(f'==========IconService Service params==========',
                    ICON_SERVICE)
        Logger.info(f'score_root_path : {score_root_path}', ICON_SERVICE)
        Logger.info(f'icon_score_state_db_root_path  : {db_root_path}',
                    ICON_SERVICE)
        Logger.info(f'amqp_target  : {amqp_target}', ICON_SERVICE)
        Logger.info(f'amqp_key  :  {amqp_key}', ICON_SERVICE)
        Logger.info(f'icon_score_queue_name  : {self._icon_score_queue_name}',
                    ICON_SERVICE)
        Logger.info(f'==========IconService Service params==========',
                    ICON_SERVICE)

        # Before creating IconScoreInnerService instance,
        # loop SHOULD be set as a current event loop for the current thread.
        # Otherwise connection between iconservice and rc will be failed.
        loop = MessageQueueService.loop
        asyncio.set_event_loop(loop)

        try:
            self._inner_service = IconScoreInnerService(
                amqp_target, self._icon_score_queue_name, conf=config)
        except FatalException as e:
            Logger.exception(e, ICON_EXCEPTION_LOG_TAG)
            Logger.error(e, ICON_EXCEPTION_LOG_TAG)
            self._inner_service.clean_close()
        finally:
            Logger.debug(
                "icon service will be closed while open the icon service engine. "
                "check if the config is valid")

        loop.create_task(_serve())
        loop.add_signal_handler(signal.SIGINT, self.close)
        loop.add_signal_handler(signal.SIGTERM, self.close)

        try:
            loop.run_forever()
        except FatalException as e:
            Logger.exception(e, ICON_EXCEPTION_LOG_TAG)
            Logger.error(e, ICON_EXCEPTION_LOG_TAG)
            self._inner_service.clean_close()
        finally:
            """
            If the function is called when the operation is not an endless loop 
            in an asynchronous function, the await is terminated immediately.
            """
            Logger.debug("loop has been stopped and will be closed")
            loop.run_until_complete(loop.shutdown_asyncgens())
            loop.close()
class IconService(object):

    """IconScore service for stand alone start.
    It provides gRPC interface for peer_service to communicate with icon service.
    Its role is the bridge between loopchain and IconServiceEngine.
    """

    def __init__(self):
        self._icon_score_queue_name = None
        self._amqp_target = None
        self._inner_service = None

    def serve(self, config: 'IconConfig'):
        async def _serve():
            await self._inner_service.connect(exclusive=True)
            Logger.info(f'Start IconService Service serve!', _TAG)

        channel = config[ConfigKey.CHANNEL]
        amqp_key = config[ConfigKey.AMQP_KEY]
        amqp_target = config[ConfigKey.AMQP_TARGET]
        score_root_path = config[ConfigKey.SCORE_ROOT_PATH]
        db_root_path = config[ConfigKey.STATE_DB_ROOT_PATH]
        version: str = get_version()

        self._set_icon_score_stub_params(channel, amqp_key, amqp_target)

        Logger.info(f'==========IconService Service params==========', _TAG)

        Logger.info(f'version : {version}', _TAG)
        Logger.info(f'score_root_path : {score_root_path}', _TAG)
        Logger.info(f'icon_score_state_db_root_path  : {db_root_path}', _TAG)
        Logger.info(f'amqp_target  : {amqp_target}', _TAG)
        Logger.info(f'amqp_key  :  {amqp_key}', _TAG)
        Logger.info(f'icon_score_queue_name  : {self._icon_score_queue_name}', _TAG)
        Logger.info(f'==========IconService Service params==========', _TAG)

        # Before creating IconScoreInnerService instance,
        # loop SHOULD be set as a current event loop for the current thread.
        # Otherwise connection between iconservice and rc will be failed.
        loop = MessageQueueService.loop
        asyncio.set_event_loop(loop)

        try:
            self._inner_service = IconScoreInnerService(amqp_target, self._icon_score_queue_name, conf=config)
        except FatalException as e:
            Logger.exception(f"{e}", _TAG)
            Logger.error(f"{e}", _TAG)
            self._inner_service.clean_close()

        loop.create_task(_serve())
        loop.add_signal_handler(signal.SIGINT, self.signal_handler, signal.SIGINT)
        loop.add_signal_handler(signal.SIGTERM, self.signal_handler, signal.SIGTERM)

        try:
            loop.run_forever()
        except FatalException as e:
            Logger.exception(f"{e}", _TAG)
            Logger.error(f"{e}", _TAG)
            self._inner_service.clean_close()
        finally:
            """
            If the function is called when the operation is not an endless loop 
            in an asynchronous function, the await is terminated immediately.
            """
            Logger.info(f"loop has been stopped and will be closed.")

            loop.run_until_complete(loop.shutdown_asyncgens())

            self.cancel_tasks(loop)

            # close icon service components
            self._inner_service.clean_close()

            loop.close()

    @staticmethod
    def cancel_tasks(loop):
        pending = asyncio.Task.all_tasks(loop)
        Logger.info(f"cancel pending {len(pending)} tasks in event loop.")
        for task in pending:
            if task.done():
                continue
            task.cancel()
            try:
                loop.run_until_complete(task)
            except asyncio.CancelledError as e:
                Logger.info(f"cancel pending task: {task}, error: {e}")

    @staticmethod
    def signal_handler(signum: int):
        Logger.debug(f"Get signal {signum}")
        asyncio.get_event_loop().stop()

    def _set_icon_score_stub_params(self, channel: str, amqp_key: str, amqp_target: str):
        self._icon_score_queue_name = \
            ICON_SCORE_QUEUE_NAME_FORMAT.format(channel_name=channel, amqp_key=amqp_key)
        self._amqp_target = amqp_target