Esempio n. 1
0
def init_model_evaluator(settings, tabpy_state, python_service):
    '''
    This will go through all models that the service currently have and
    initialize them.
    '''
    logger.info("Initializing models...")

    existing_pos = tabpy_state.get_endpoints()

    for (object_name, obj_info) in (existing_pos.items()):
        object_version = obj_info['version']
        object_type = obj_info['type']
        object_path = get_query_object_path(
            settings[SettingsParameters.StateFilePath], object_name,
            object_version)

        logger.info(f'Load endpoint: {object_name}, '
                    f'version: {object_version}, '
                    f'type: {object_type}')
        if object_type == 'alias':
            msg = LoadObject(object_name, obj_info['target'], object_version,
                             False, 'alias')
        else:
            local_path = object_path
            msg = LoadObject(object_name, local_path, object_version, False,
                             object_type)
        python_service.manage_request(msg)
Esempio n. 2
0
def init_model_evaluator(settings):
    '''
    This will go through all models that the service currently have and
    initialize them.
    '''
    tabpy = settings['tabpy']
    py_handler = settings['py_handler']

    existing_pos = tabpy.get_endpoints()

    for (object_name, obj_info) in (existing_pos.items() if sys.version_info >
                                    (3, 0) else existing_pos.iteritems()):
        object_version = obj_info['version']
        object_type = obj_info['type']
        object_path = get_query_object_path(settings['state_file_path'],
                                            object_name, object_version)

        log_info('Load endpoint: %s, version: %s, type: %s' %
                 (object_name, object_version, object_type))
        if object_type == 'alias':
            msg = LoadObject(object_name, obj_info['target'], object_version,
                             False, 'alias')
        else:
            local_path = object_path
            msg = LoadObject(object_name, local_path, object_version, False,
                             object_type)
        py_handler.manage_request(msg)
Esempio n. 3
0
def on_state_change(settings,
                    tabpy_state,
                    python_service,
                    logger=logging.getLogger(__name__)):
    try:
        logger.log(logging.INFO, "Loading state from state file")
        config = util._get_state_from_file(
            settings[SettingsParameters.StateFilePath], logger=logger)
        new_ps_state = TabPyState(config=config, settings=settings)

        (has_changes,
         changes) = _get_latest_service_state(settings, tabpy_state,
                                              new_ps_state, python_service)
        if not has_changes:
            logger.info("Nothing changed, return.")
            return

        new_endpoints = new_ps_state.get_endpoints()
        for object_name in changes['endpoints']:
            (object_type, object_version,
             object_path) = changes['endpoints'][object_name]

            if not object_path and not object_version:  # removal
                logger.info(f'Removing object: URI={object_name}')

                python_service.manage_request(DeleteObjects([object_name]))

                cleanup_endpoint_files(object_name,
                                       settings[SettingsParameters.UploadDir],
                                       logger=logger)

            else:
                endpoint_info = new_endpoints[object_name]
                is_update = object_version > 1
                if object_type == 'alias':
                    msg = LoadObject(object_name, endpoint_info['target'],
                                     object_version, is_update, 'alias')
                else:
                    local_path = object_path
                    msg = LoadObject(object_name, local_path, object_version,
                                     is_update, object_type)

                python_service.manage_request(msg)
                wait_for_endpoint_loaded(python_service, object_name)

                # cleanup old version of endpoint files
                if object_version > 2:
                    cleanup_endpoint_files(
                        object_name,
                        settings[SettingsParameters.UploadDir],
                        logger=logger,
                        retain_versions=[object_version, object_version - 1])

    except Exception as e:
        err_msg = format_exception(e, 'on_state_change')
        logger.log(logging.ERROR,
                   f'Error submitting update model request: error={err_msg}')
Esempio n. 4
0
def on_state_change(settings):
    try:
        py_handler = settings['py_handler']

        logger.info("Loading state from state file")
        config = util._get_state_from_file(settings['state_file_path'])
        new_ps_state = TabPyState(config=config, settings=settings)

        (has_changes,
         changes) = _get_latest_service_state(settings, new_ps_state)
        if not has_changes:
            logger.info("Nothing changed, return.")
            return

        new_endpoints = new_ps_state.get_endpoints()
        for object_name in changes['endpoints']:
            (object_type, object_version,
             object_path) = changes['endpoints'][object_name]

            if not object_path and not object_version:  # removal
                logger.info("Removing object: URI={}".format(object_name))

                py_handler.manage_request(DeleteObjects([object_name]))

                cleanup_endpoint_files(object_name, settings['upload_dir'])

            else:
                endpoint_info = new_endpoints[object_name]
                is_update = object_version > 1
                if object_type == 'alias':
                    msg = LoadObject(object_name, endpoint_info['target'],
                                     object_version, is_update, 'alias')
                else:
                    local_path = object_path
                    msg = LoadObject(object_name, local_path, object_version,
                                     is_update, object_type)

                py_handler.manage_request(msg)
                wait_for_endpoint_loaded(py_handler, object_name)

                # cleanup old version of endpoint files
                if object_version > 2:
                    cleanup_endpoint_files(
                        object_name, settings['upload_dir'],
                        [object_version, object_version - 1])

    except Exception as e:
        err_msg = format_exception(e, 'on_state_change')
        logger.error(
            "Error submitting update model request: error={}".format(err_msg))