当前位置: 首页>>代码示例>>Python>>正文


Python Dependency.critical方法代码示例

本文整理汇总了Python中common.utilities.inversion_of_control.Dependency.critical方法的典型用法代码示例。如果您正苦于以下问题:Python Dependency.critical方法的具体用法?Python Dependency.critical怎么用?Python Dependency.critical使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在common.utilities.inversion_of_control.Dependency的用法示例。


在下文中一共展示了Dependency.critical方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: ArcGISConnection

# 需要导入模块: from common.utilities.inversion_of_control import Dependency [as 别名]
# 或者: from common.utilities.inversion_of_control.Dependency import critical [as 别名]
class ArcGISConnection(object):
    """
    This class is in charge of keeping track of an ArcGIS server and making calls to it
    """

    def __init__(self, server_ip_address):
        self._rest_provider = Dependency("RestProvider", HasMethods("download_file")).value
        self._config = Dependency("Config", HasAttributes("ArcGIS_timeout", "ArcGIS_max_errors")).value
        self._logger = Dependency("LogManager", HasMethods("error")).value
        self._server_ip_address = server_ip_address
        self._timeout = self._config.ArcGIS_timeout
        self._max_error_count = self._config.ArcGIS_max_errors

    def download_file(self, url):

        try:

            # append the server ip to the url
            url = self._append_ip_address_to_url(url)

            # download the file
            return self._rest_provider.download_file(url)

        except Exception as ex:

            # this has been timing out a lot, so I'm adding some logging...
            self._logger.critical("Exception on ESRI Server: " % str(self._server_ip_address))
            self._logger.critical(str(ex))
            self._logger.critical(traceback.format_exc())
            raise

    def generate_report(self, request_format, url):
        # append the server ip to the url
        url = self._append_ip_address_to_url(url)

        # the ArcGIS server has weird errors. This ensures that we loop several times until a good response is returned
        successful_response = False
        request_counter = 0
        response = None

        while not successful_response:

            # try to get the request and handle errors properly (so that we can retry)
            try:
                response = self._rest_provider.make_post_request(url, request_format, time_out = self._timeout)

            except Exception as e:
                # if there's an error, log it and try again
                response = None
                error_text = str(e)
                self._logger.critical("error send ArcGIS report (exception): %s" + str(error_text))

                # if it's a timeout, register the server ip address, so that it keeps count
                # treat token required as a time out, so that those servers are taken out of the pool
                if "Request timed out" in error_text or "timeout" in error_text or "Token Required" in error_text:
                    ArcGISConnectionManager.instance.register_timeout(self._server_ip_address)

            # once we get a response back, check it's type
            if response is not None:

                # this signals a successful response
                gp19_success_response_text = '{"paramName":"OutputStatus","dataType":"GPBoolean","value":true}'
                if response.text.find('arcgisoutput') != -1 or response.text.find('stops') != -1 \
                    or response.text.find("rings") != -1 or response.text.find(gp19_success_response_text) != -1:

                    # mark for exit and reset the timeout count
                    successful_response = True
                    ArcGISConnectionManager.instance.reset_timeout_count_on_successful_connection(self._server_ip_address)

                # if it's a timeout, register the server ip address
                # treat token required as a time out, so that those servers are taken out of the pool
                elif "Request timed out" in response.text or "timeout" in response.text or "Token Required" in response.text:
                    ArcGISConnectionManager.instance.register_timeout(self._server_ip_address)
                elif 'No solution found.' in response.text and 'useHierarchy' in request_format and request_format['useHierarchy'] == 'true':
                    request_format['useHierarchy'] = 'false'
                else:
                    self._logger.critical("error send ArcGIS report (no arcgisoutput): %s" + response.text)

            else:
                self._logger.critical("error send ArcGIS report (response is None)")

            if request_counter >= self._max_error_count:
                raise Exception('too many requests - %s' % url)

            # increment the request counter
            request_counter += 1

        return response

    def _append_ip_address_to_url(self, url):
        if url is not None and url[:4] != 'http':
            url = ''.join(['http://', self._server_ip_address, '/arcgis', url])
        return url
开发者ID:erezrubinstein,项目名称:aa,代码行数:95,代码来源:ArcGIS_connection_manager.py

示例2: TestSQLLoggingHandler

# 需要导入模块: from common.utilities.inversion_of_control import Dependency [as 别名]
# 或者: from common.utilities.inversion_of_control.Dependency import critical [as 别名]
class TestSQLLoggingHandler(unittest.TestCase):

    def setUp(self):
        # register mocks
        register_mock_dependencies("DEBUG")

        # get other dependencies
        self.data_repository = Dependency("DataRepository").value

        # create config and change values before registering sql handler
        self._config = Dependency("Config").value
        # set the flush to be .5 seconds so that we can test quickly
        self._config.sql_logging_insert_timer = .1
        self._config.app_version = "9.9.9.9"
        self._config.environment = "unit test"

        # create logger with only SQLLoggingHandler
        self.logger = Dependency("LogManager").value
        self.logger.clear_logging_handlers()
        self.sql_handler = self.logger.add_sql_handler()

    def tearDown(self):
        self.sql_handler.wait_for_threads_to_finish()
        dependencies.clear()

    def test_basic_message(self):
        self.logger.debug("test message")

        # you need to sleep for one second so that the logger flushes correctly
        sleep(.3)

        # make sure only one record is present
        self.assertEqual(len(self.data_repository.logging_records), 1)

        # make sure values are correct
        record = self.data_repository.logging_records[0]
        self.assertEqual(record.log_entry_type_id, 5)
        self.assertEqual(record.version, "9.9.9.9")
        self.assertEqual(record.environment, "unit test")
        # process id is 234234234_process
        self.assertRegexpMatches(record.process_id, "\d*_.*")
        self.assertEqual(record.message.strip(), "test message")
        # compare timestamp without seconds
        utc = str(datetime.utcnow())
        self.assertEqual(utc.split()[0], record.time.split()[0])
        self.assertEqual(record.function_name, "test_sql_logging_handler.py__test_basic_message")
        self.assertIsNone(record.elapsed_time)

    def test_basic_message_with_elapsed_time(self):
        self.logger.debug("test message", elapsed_time=20.1)

        # you need to sleep for one second so that the logger flushes correctly
        sleep(.3)

        # make sure only one record is present
        self.assertEqual(len(self.data_repository.logging_records), 1)

        # make sure values are correct
        record = self.data_repository.logging_records[0]
        self.assertEqual(record.log_entry_type_id, 5)
        self.assertEqual(record.version, "9.9.9.9")
        self.assertEqual(record.environment, "unit test")
        # process id is 234234234_process
        self.assertRegexpMatches(record.process_id, "\d*_.*")
        self.assertEqual(record.message.strip(), "test message")
        # compare timestamp without seconds
        utc = str(datetime.utcnow())
        self.assertEqual(utc.split()[0], record.time.split()[0])
        self.assertEqual(record.function_name, "test_sql_logging_handler.py__test_basic_message_with_elapsed_time")
        self.assertEqual(record.elapsed_time, 20.1)

    def test_all_message_levels(self):
        """
        This test combines critical, error, warning, info, and debug together.
        This is done to save time since we have to wait for the flushing operation
        """
        self.logger.debug("test debug", elapsed_time=1.1)
        self.logger.info("test info", elapsed_time=2.2)
        self.logger.warning("test warning", elapsed_time=3.3)
        self.logger.error("test error", elapsed_time=4.4)
        self.logger.critical("test critical", elapsed_time=5.5)

        # you need to sleep for one second so that the logger flushes correctly
        sleep(.3)

        # make sure we have 5 records
        self.assertEqual(len(self.data_repository.logging_records), 5)

        # make sure every record's type is correct
        self.assertEqual(self.data_repository.logging_records[0].log_entry_type_id, 5)
        self.assertEqual(self.data_repository.logging_records[0].message.strip(), "test debug")
        self.assertEqual(self.data_repository.logging_records[0].elapsed_time, 1.1)
        self.assertEqual(self.data_repository.logging_records[1].log_entry_type_id, 4)
        self.assertEqual(self.data_repository.logging_records[1].message.strip(), "test info")
        self.assertEqual(self.data_repository.logging_records[1].elapsed_time, 2.2)
        self.assertEqual(self.data_repository.logging_records[2].log_entry_type_id, 3)
        self.assertEqual(self.data_repository.logging_records[2].message.strip(), "test warning")
        self.assertEqual(self.data_repository.logging_records[2].elapsed_time, 3.3)
        self.assertEqual(self.data_repository.logging_records[3].log_entry_type_id, 2)
        self.assertEqual(self.data_repository.logging_records[3].message.strip(), "test error")
#.........这里部分代码省略.........
开发者ID:erezrubinstein,项目名称:aa,代码行数:103,代码来源:test_sql_logging_handler.py

示例3: ArcGISConnectionManager

# 需要导入模块: from common.utilities.inversion_of_control import Dependency [as 别名]
# 或者: from common.utilities.inversion_of_control.Dependency import critical [as 别名]
class ArcGISConnectionManager(object):
    """
    This is a singleton ArcGIS Connection Manager.
    It is in-charge of doing load balancing between a set of servers.
    """

    # static members
    instance = None
    __singleton_lock = Lock()

    def __init__(self):
        # singleton check and set up
        with self.__singleton_lock:
            if ArcGISConnectionManager.instance is None:
                # set local config values
                self._log = Dependency("LogManager").value
                config = Dependency("Config", HasAttributes("ArcGIS_server_ips", "ArcGIS_max_timeouts")).value
                self.ip_addresses = config.ArcGIS_server_ips[:]
                self.max_timeouts = config.ArcGIS_max_timeouts
                self.remove_server_after_max_timeouts = config.ArcGIS_remove_server_after_max_timeouts

                # keep the algorithm in a class, which makes it easier to exchange
                self.routing_algorithm = RoundRobinRoutingAlgorithm(self.ip_addresses)

                # variables for registering errors and timeouts per ip
                self._timeout_count = {}
                for ip_address in config.ArcGIS_server_ips:
                    self._timeout_count[ip_address] = 0

                # singleton instantiation
                ArcGISConnectionManager.instance = self


    def register_timeout(self, ip_address):
        """
        This registers and increments a time out count per ip.  It removes IPs from the list that have "too many" timeouts
        """
        self._timeout_count[ip_address] += 1

        # if there are more time outs, than allowed, remove the ip and re-create the round robin
        if self._timeout_count[ip_address] > self.max_timeouts:

            # if we allow removing ips, after certain timeouts, remove the ips and reset the routing algorithm
            if self.remove_server_after_max_timeouts:

                self.ip_addresses.remove(ip_address)
                self.routing_algorithm = RoundRobinRoutingAlgorithm(self.ip_addresses)
                message = "Timeout limit exceeded.  Removing ip address (%s)." % ip_address
                self._log.critical(message)

            else:

                message = "Timeout limit exceeded."

            # raise exception to exit from after removing the ip
            raise Exception(message)


    def reset_timeout_count_on_successful_connection(self, ip_address):
        """
        This method should be called on a successful call, which would then reset the timeout counter.
        It's used to signal that everything seems to be OK now
        """
        self._timeout_count[ip_address] = 0


    def get_connection(self):
        ip_address = self.routing_algorithm.get_next_ip_address()
        return ArcGISConnection(ip_address)
开发者ID:erezrubinstein,项目名称:aa,代码行数:71,代码来源:ArcGIS_connection_manager.py


注:本文中的common.utilities.inversion_of_control.Dependency.critical方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。