def retryOnEC2TransientError(logger=logging.root, timeoutSec=DEFAULT_RETRY_TIMEOUT_SEC): """Create a decorator for retrying a function upon EC2 transient error. :param logger: a python logger object for logging failures; defaults to the builtin root logger :param timeoutSec: How many seconds from time of initial call to stop retrying :type timeoutSec: floating point :returns: a decorator """ def retryFilter(e, *_args, **_kwargs): """Return True to permit a retry, false to re-raise the exception.""" if isinstance(e, boto.exception.BotoServerError): if getattr(e, "error_code", "") in RETRIABLE_ERROR_CODES: return True else: return False return True return retry(timeoutSec=timeoutSec, initialRetryDelaySec=INITIAL_RETRY_BACKOFF_SEC, maxRetryDelaySec=MAX_RETRY_BACKOFF_SEC, retryExceptions=RETRY_EXCEPTIONS, retryFilter=retryFilter, logger=logger, clientLabel="retryOnEC2TransientError")
def _retryOnRequestsErrors(timeoutSec=10, pauseSec=0.2): def retryFilter(exc, _args, _kwargs): if isinstance(exc, requests.exceptions.HTTPError): # Retry on: # 500 Internal Server Error # 502 Server Error: Bad Gateway # 503 Service Unavailable # 504 Gateway Timeout if (exc.response is not None and exc.response.status_code in [500, 502, 503, 504]): return True else: return False return True return retry( timeoutSec=timeoutSec, initialRetryDelaySec=pauseSec, maxRetryDelaySec=pauseSec, retryExceptions=( # requests retries on DNS errors, but not on connection errors requests.exceptions.ConnectionError, requests.exceptions.HTTPError, requests.exceptions.Timeout, ), retryFilter=retryFilter, logger=g_log)
def testRetryRetryExceptionExcluded(self, mockTime, mockSleep): """ Test that retry is not triggered if raised exeception is not in retryExceptions """ time_test_utils.configureTimeAndSleepMocks(mockTime, mockSleep) class TestExceptionA(Exception): pass class TestExceptionB(Exception): pass _retry = error_handling.retry(timeoutSec=1, initialRetryDelaySec=1, maxRetryDelaySec=10, retryExceptions=(TestExceptionA, )) @_retry def testFunction(): raise TestExceptionB("Test exception") with self.assertRaises(TestExceptionB): testFunction() self.assertEqual(mockSleep.call_count, 0)
def retryOnEC2TransientError(logger=logging.root, timeoutSec=DEFAULT_RETRY_TIMEOUT_SEC): """Create a decorator for retrying a function upon EC2 transient error. :param logger: a python logger object for logging failures; defaults to the builtin root logger :param timeoutSec: How many seconds from time of initial call to stop retrying :type timeoutSec: floating point :returns: a decorator """ def retryFilter(e, *_args, **_kwargs): """Return True to permit a retry, false to re-raise the exception.""" if isinstance(e, boto.exception.BotoServerError): if getattr(e, "error_code", "") in RETRIABLE_ERROR_CODES: return True else: return False return True return retry( timeoutSec=timeoutSec, initialRetryDelaySec=INITIAL_RETRY_BACKOFF_SEC, maxRetryDelaySec=MAX_RETRY_BACKOFF_SEC, retryExceptions=RETRY_EXCEPTIONS, retryFilter=retryFilter, logger=logger, clientLabel="retryOnEC2TransientError")
def retrySQL(): def retryFilter(e, *_args, **_kwargs): if isinstance(e, (InternalError, OperationalError)): if e.orig.args and e.orig.args[0] in _ALL_RETRIABLE_ERROR_CODES: return True elif isinstance(e, DBAPIError): if (e.orig.args and inspect.isclass(e.orig.args[0]) and issubclass(e.orig.args[0], socket.error)): return True return False retryExceptions = tuple([ InternalError, OperationalError, DBAPIError, ]) return error_handling.retry(timeoutSec=_RETRY_TIMEOUT, initialRetryDelaySec=0.1, maxRetryDelaySec=10, retryExceptions=retryExceptions, retryFilter=retryFilter, logger=logging.getLogger("sql-retry"))
def _retryOnRequestsErrors(timeoutSec=10, pauseSec=0.2): def retryFilter(exc, _args, _kwargs): if isinstance(exc, requests.exceptions.HTTPError): # Retry on: # 500 Internal Server Error # 502 Server Error: Bad Gateway # 503 Service Unavailable # 504 Gateway Timeout if (exc.response is not None and exc.response.status_code in [500, 502, 503, 504]): return True else: return False return True return retry( timeoutSec=timeoutSec, initialRetryDelaySec=pauseSec, maxRetryDelaySec=pauseSec, retryExceptions=( # requests retries on DNS errors, but not on connection errors requests.exceptions.ConnectionError, requests.exceptions.HTTPError, requests.exceptions.Timeout, ), retryFilter=retryFilter, logger=g_log)
def _retry_on_requests_errors(timeoutSec=10, pauseSec=0.2): return retry( timeoutSec=timeoutSec, initialRetryDelaySec=pauseSec, maxRetryDelaySec=pauseSec, retryExceptions=( # requests retries on DNS errors, but not on connection errors requests.exceptions.ConnectionError, ), logger=g_log)
def testRetryRetryFilter(self, mockTime, mockSleep): """Test that if retryFilter is specified and exception is in retryExceptions, retries iff retryFilter returns true.""" time_test_utils.configureTimeAndSleepMocks(mockTime, mockSleep) # Test with retryFilter returning True _retryTrueFilter = error_handling.retry( timeoutSec=1, initialRetryDelaySec=1, maxRetryDelaySec=10, retryExceptions=(TestParentException, ), retryFilter=lambda _1, _2, _3: True) @_retryTrueFilter def testFunctionTrue(): raise TestChildException("Test exception") with self.assertRaises(TestChildException): testFunctionTrue() self.assertEqual(mockSleep.call_count, 1) # Test with retryFilter returning False mockSleep.reset_mock() _retryFalseFilter = error_handling.retry( timeoutSec=1, initialRetryDelaySec=1, maxRetryDelaySec=10, retryExceptions=(TestParentException, ), retryFilter=lambda _1, _2, _3: False) @_retryFalseFilter def testFunctionFalse(): raise TestChildException("Test exception") with self.assertRaises(TestChildException): testFunctionFalse() self.assertEqual(mockSleep.call_count, 0)
def _retry_on_requests_errors(timeoutSec=10, pauseSec=0.2): return retry( timeoutSec=timeoutSec, initialRetryDelaySec=pauseSec, maxRetryDelaySec=pauseSec, retryExceptions=( # requests retries on DNS errors, but not on connection errors requests.exceptions.ConnectionError, ), logger=g_log)
def retryOnTransientDynamoDBError(logger): """ Decorator helper for retrying dynamodb operations that failed due to transient errors :params logging.Logger logger: Logger instance :returns: Function decorator configured to retry in the event of dynamodb ProvisionedThroughputExceededException exceptions """ return error_handling.retry( timeoutSec=10, initialRetryDelaySec=0.5, maxRetryDelaySec=2, retryExceptions=(ProvisionedThroughputExceededException,), logger=logger )
def testRetryRetryFilter(self, mockTime, mockSleep): """Test that if retryFilter is specified and exception is in retryExceptions, retries iff retryFilter returns true.""" self.mockSleepTime(mockTime, mockSleep) # Test with retryFilter returning True _retryTrueFilter = error_handling.retry( timeoutSec=1, initialRetryDelaySec=1, maxRetryDelaySec=10, retryExceptions=(TestParentException,), retryFilter=lambda _1, _2, _3: True) @_retryTrueFilter def testFunctionTrue(): raise TestChildException("Test exception") with self.assertRaises(TestChildException): testFunctionTrue() self.assertEqual(mockSleep.call_count, 1) # Test with retryFilter returning False mockSleep.reset_mock() _retryFalseFilter = error_handling.retry( timeoutSec=1, initialRetryDelaySec=1, maxRetryDelaySec=10, retryExceptions=(TestParentException,), retryFilter=lambda _1, _2, _3: False) @_retryFalseFilter def testFunctionFalse(): raise TestChildException("Test exception") with self.assertRaises(TestChildException): testFunctionFalse() self.assertEqual(mockSleep.call_count, 0)
def testNoRetryIfCallSucceeds(self, mockTime, mockSleep): """If the initial call succeeds, test that no retries are performed.""" self.mockSleepTime(mockTime, mockSleep) _retry = error_handling.retry( timeoutSec=30, initialRetryDelaySec=2, maxRetryDelaySec=10) testFunction = Mock(__name__="testFunction", autospec=True) _retry(testFunction)() testFunction.assert_called_once_with()
def testNoRetryIfCallSucceeds(self, mockTime, mockSleep): """If the initial call succeeds, test that no retries are performed.""" time_test_utils.configureTimeAndSleepMocks(mockTime, mockSleep) _retry = error_handling.retry(timeoutSec=30, initialRetryDelaySec=2, maxRetryDelaySec=10) testFunction = Mock(__name__="testFunction", autospec=True) _retry(testFunction)() testFunction.assert_called_once_with()
def testReturnsExpectedWithExpectedArgs(self, mockTime, mockSleep): """Test that docorated function receives only expected args and that it returns the expected value on success.""" self.mockSleepTime(mockTime, mockSleep) _retry = error_handling.retry( timeoutSec=30, initialRetryDelaySec=2, maxRetryDelaySec=10) testFunction = Mock(return_value=321, __name__="testFunction", autospec=True) returnValue = _retry(testFunction)(1, 2, a=3, b=4) self.assertEqual(returnValue, 321) testFunction.assert_called_once_with(1, 2, a=3, b=4)
def testRetryRetryExceptionIncluded(self, mockTime, mockSleep): """Test that retry is triggered if raised exception is in retryExceptions.""" self.mockSleepTime(mockTime, mockSleep) _retry = error_handling.retry( timeoutSec=1, initialRetryDelaySec=1, maxRetryDelaySec=10, retryExceptions=(TestParentException,)) @_retry def testFunction(): raise TestChildException("Test exception") with self.assertRaises(TestChildException): testFunction() self.assertEqual(mockSleep.call_count, 1)
def testReturnsExpectedWithExpectedArgs(self, mockTime, mockSleep): """Test that docorated function receives only expected args and that it returns the expected value on success.""" time_test_utils.configureTimeAndSleepMocks(mockTime, mockSleep) _retry = error_handling.retry(timeoutSec=30, initialRetryDelaySec=2, maxRetryDelaySec=10) testFunction = Mock(return_value=321, __name__="testFunction", autospec=True) returnValue = _retry(testFunction)(1, 2, a=3, b=4) self.assertEqual(returnValue, 321) testFunction.assert_called_once_with(1, 2, a=3, b=4)
def testRetryWaitsInitialRetryDelaySec(self, mockTime, mockSleep): """Test that delay times are correct.""" self.mockSleepTime(mockTime, mockSleep) _retry = error_handling.retry( timeoutSec=30, initialRetryDelaySec=2, maxRetryDelaySec=10) testFunction = Mock(side_effect=TestParentException("Test exception"), __name__="testFunction", autospec=True) with self.assertRaises(TestParentException): _retry(testFunction)() self.assertEqual(mockSleep.mock_calls, [call(2), call(4), call(8), call(10), call(10)]) self.assertEqual(testFunction.call_count, 6)
def testRetryRetryExceptionIncluded(self, mockTime, mockSleep): """Test that retry is triggered if raised exception is in retryExceptions.""" time_test_utils.configureTimeAndSleepMocks(mockTime, mockSleep) _retry = error_handling.retry(timeoutSec=1, initialRetryDelaySec=1, maxRetryDelaySec=10, retryExceptions=(TestParentException, )) @_retry def testFunction(): raise TestChildException("Test exception") with self.assertRaises(TestChildException): testFunction() self.assertEqual(mockSleep.call_count, 1)
def testRetryNoTimeForRetries(self, mockTime, mockSleep): """Test that when timeoutSec == 0, function is executed exactly once with no retries, and raises an exception on failure. """ self.mockSleepTime(mockTime, mockSleep) _retry = error_handling.retry( timeoutSec=0, initialRetryDelaySec=0.2, maxRetryDelaySec=10) testFunction = Mock(side_effect=TestParentException("Test exception"), __name__="testFunction", autospec=True) with self.assertRaises(TestParentException): _retry(testFunction)() self.assertFalse(mockSleep.called) testFunction.assert_called_once_with()
def testRetryNoTimeForRetries(self, mockTime, mockSleep): """Test that when timeoutSec == 0, function is executed exactly once with no retries, and raises an exception on failure. """ time_test_utils.configureTimeAndSleepMocks(mockTime, mockSleep) _retry = error_handling.retry(timeoutSec=0, initialRetryDelaySec=0.2, maxRetryDelaySec=10) testFunction = Mock(side_effect=TestParentException("Test exception"), __name__="testFunction", autospec=True) with self.assertRaises(TestParentException): _retry(testFunction)() self.assertFalse(mockSleep.called) testFunction.assert_called_once_with()
def retryDecoratorWrapper(f): class CounterContainer(object): i = 1 # retryFilter used only to implement custom logging def retryFilter(e, args, kwargs): firstLine = (f.__doc__ or "").strip().split("\n")[0] LOGGER.info("Attempt %i of %s: %s", CounterContainer.i, f.__name__, firstLine) CounterContainer.i += 1 return True retryDecorator = error_handling.retry( timeoutSec=duration, initialRetryDelaySec=delay, maxRetryDelaySec=delay, retryExceptions=exceptionTypes, retryFilter=retryFilter, ) return retryDecorator(f)
def testFailsFirstSucceedsLater(self, mockTime, mockSleep): """If initial attempts fail but subsequent attempt succeeds, ensure that expected number of retries is performed and expected value is returned.""" time_test_utils.configureTimeAndSleepMocks(mockTime, mockSleep) _retry = error_handling.retry(timeoutSec=30, initialRetryDelaySec=2, maxRetryDelaySec=10) testFunction = Mock(side_effect=[ TestParentException("Test exception 1"), TestParentException("Test exception 2"), 321 ], __name__="testFunction", autospec=True) returnValue = _retry(testFunction)() self.assertEqual(returnValue, 321) self.assertEqual(testFunction.call_count, 3)
def testFailsFirstSucceedsLater(self, mockTime, mockSleep): """If initial attempts fail but subsequent attempt succeeds, ensure that expected number of retries is performed and expected value is returned.""" self.mockSleepTime(mockTime, mockSleep) _retry = error_handling.retry( timeoutSec=30, initialRetryDelaySec=2, maxRetryDelaySec=10) testFunction = Mock( side_effect=[ TestParentException("Test exception 1"), TestParentException("Test exception 2"), 321 ], __name__="testFunction", autospec=True) returnValue = _retry(testFunction)() self.assertEqual(returnValue, 321) self.assertEqual(testFunction.call_count, 3)
def testRetryWaitsInitialRetryDelaySec(self, mockTime, mockSleep): """Test that delay times are correct.""" time_test_utils.configureTimeAndSleepMocks(mockTime, mockSleep) _retry = error_handling.retry(timeoutSec=30, initialRetryDelaySec=2, maxRetryDelaySec=10) testFunction = Mock(side_effect=TestParentException("Test exception"), __name__="testFunction", autospec=True) with self.assertRaises(TestParentException): _retry(testFunction)() self.assertEqual( mockSleep.mock_calls, [call(2), call(4), call(8), call(10), call(10)]) self.assertEqual(testFunction.call_count, 6)
def retryOnBotoS3TransientError(getLoggerCallback=logging.getLogger, timeoutSec=DEFAULT_RETRY_TIMEOUT_SEC): """Return a decorator for retrying a boto S3 function on transient failures. NOTE: please ensure that the operation being retried is idempotent. getLoggerCallback: user-supplied callback function that takes no args and returns the logger instance to use for logging. timeoutSec: How many seconds from time of initial call to stop retrying (floating point) """ def retryFilter(e, args, kwargs): """Return True to permit a retry, false to re-raise the exception.""" if isinstance(e, BotoServerError): if (getattr(e, "error_code", "") in RETRIABLE_SERVER_ERROR_CODES): return True else: return False return True retryExceptions = tuple([ socket.error, AWSConnectionError, S3DataError, S3CreateError, S3ResponseError, ]) logger = getLoggerCallback() return retry( timeoutSec=timeoutSec, initialRetryDelaySec=0.1, maxRetryDelaySec=10, retryExceptions=retryExceptions, retryFilter=retryFilter, logger=logger, clientLabel="retryOnBotoS3TransientError")
def retrySQL(): def retryFilter(e, *_args, **_kwargs): if isinstance(e, (InternalError, OperationalError)): if e.orig.args and e.orig.args[0] in _ALL_RETRIABLE_ERROR_CODES: return True elif isinstance(e, DBAPIError): if (e.orig.args and inspect.isclass(e.orig.args[0]) and issubclass(e.orig.args[0], socket.error)): return True return False retryExceptions = tuple([ InternalError, OperationalError, DBAPIError, ]) return error_handling.retry( timeoutSec=_RETRY_TIMEOUT, initialRetryDelaySec=0.1, maxRetryDelaySec=10, retryExceptions=retryExceptions, retryFilter=retryFilter, logger=logging.getLogger("sql-retry"))
def testRetryRetryExceptionExcluded(self, mockTime, mockSleep): """ Test that retry is not triggered if raised exeception is not in retryExceptions """ self.mockSleepTime(mockTime, mockSleep) class TestExceptionA(Exception): pass class TestExceptionB(Exception): pass _retry = error_handling.retry( timeoutSec=1, initialRetryDelaySec=1, maxRetryDelaySec=10, retryExceptions=(TestExceptionA,)) @_retry def testFunction(): raise TestExceptionB("Test exception") with self.assertRaises(TestExceptionB): testFunction() self.assertEqual(mockSleep.call_count, 0)
pass class MarketNewsDetailsFetchError(Exception): """ Error fetching Market News Details for an article """ pass # Initialize logging g_log = logging.getLogger("xignite_security_news_agent") # Retry decorator for specific errors _RETRY_ON_REQUESTS_ERROR = retry( timeoutSec=10, initialRetryDelaySec=0.05, maxRetryDelaySec=1, retryExceptions=( # requests retries on DNS errors, but not on connection errors requests.exceptions.ConnectionError, ), logger=g_log) g_poolWorkerHttpSession = requests.Session() def _loadNewsVolumeMetricSpecs(): """ Load metric specs for our securities news volume provider :returns: a sequence of NewsVolumeMetricSpec objects """ return tuple( NewsVolumeMetricSpec(metric=metricName, symbol=resVal["symbol"]) for resVal in getMetricsConfiguration().itervalues()
"""Baseline non-destructive health checks of the running Taurus Engine server. """ import unittest from urlparse import urljoin import xmlrpclib from nta.utils import error_handling # Max time to wait for service to enter RUNNING state _RUNNING_TIMEOUT_SEC = 5 _TAURUS_ENGINE_SUPERVISOR_API_URL = urljoin("http://localhost:9001", "RPC2") _RETRY_SERVICE_RUNNING_CHECK = error_handling.retry(_RUNNING_TIMEOUT_SEC, initialRetryDelaySec=1, maxRetryDelaySec=1) class TaurusEngineHealthCheckTestCase(unittest.TestCase): @staticmethod def _getServiceStateName(serviceName): """Get the state name of a service :param str serviceName: name of service :returns: State name of the service; see http://supervisord.org/subprocess.html#process-states :rtype: str """ server = xmlrpclib.Server(_TAURUS_ENGINE_SUPERVISOR_API_URL)
import unittest import tweepy from nta.utils import error_handling from nta.utils import error_reporting from nta.utils.message_bus_connector import MessageBusConnector from taurus_metric_collectors import collectorsdb from taurus_metric_collectors import metric_utils from taurus_metric_collectors.xignite import xignite_stock_agent _ACCESSIBILITY_TIMEOUT_SEC = 20 _RETRY_ACCESSIBILITY_CHECK = error_handling.retry(_ACCESSIBILITY_TIMEOUT_SEC, initialRetryDelaySec=1, maxRetryDelaySec=1) class TaurusMetricCollectorsResourceAccessibilityTestCase(unittest.TestCase): @_RETRY_ACCESSIBILITY_CHECK def testCollectorsdbIsAccessible(self): # pylint: disable=R0201 transactionContext = collectorsdb.engineFactory().begin() transactionContext.transaction.rollback() @_RETRY_ACCESSIBILITY_CHECK def testMessageBusIsAccessible(self): # pylint: disable=R0201 with MessageBusConnector() as bus: bus.isMessageQeueuePresent("") # Twitter API sometimes throttles for a while, so we use longer retries
from nta.utils.logging_support_raw import LoggingSupport from nta.utils.test_utils import amqp_test_utils _LOGGER = logging.getLogger(__name__) def setUpModule(): LoggingSupport.initTestApp() _RETRY_ON_ASSERTION_ERROR = retry(timeoutSec=20, initialRetryDelaySec=0.5, maxRetryDelaySec=2, retryExceptions=(AssertionError,), logger=_LOGGER) _NUM_TEST_MESSAGES = 3 @amqp_test_utils.RabbitmqVirtualHostPatch( clientLabel="testingAmqpClient", logger=_LOGGER) class SynchronousAmqpClientTest(unittest.TestCase): """ Test for nta.utils.amqp.SynchronousAmqpClient """ def __init__(self, *args, **kwargs):
Emax=126, rounding=None, prec=5, # Reduce precision for overall reduction in payload size traps=[ Clamped, Overflow, Underflow ] ) # Decorator for retrying dynamodb operations that failed due to transient error _RETRY_ON_TRANSIENT_DYNAMODB_ERROR = error_handling.retry( timeoutSec=10, initialRetryDelaySec=0.5, maxRetryDelaySec=2, retryExceptions=(ProvisionedThroughputExceededException,), logger=g_log ) def convertDefineModelResultToMetricItem(modelCommandResult): """ Convert "defineModel" Model Command Result to MetricItem suitable for publishing to dynamodb. :param dict modelCommandResult: See model_command_result_amqp_message.json for schema. :returns: MetricItem instance suitable for publishing to the `taurus.metric` dynamodb table. :rtype: Instance of namedtuple implemented in `MetricDynamoDBDefinition().Item()` constructor
import taurus.engine from taurus.engine.exceptions import ObjectNotFoundError from taurus.engine import logging_support from taurus.engine.runtime.dynamodb.definitions import ( InstanceDataHourlyDynamoDBDefinition, MetricDynamoDBDefinition, MetricDataDynamoDBDefinition, MetricTweetsDynamoDBDefinition) from taurus.engine.runtime.dynamodb.dynamodb_service import DynamoDBService LOGGER = logging.getLogger(__name__) # Decorator for retrying dynamodb operations that failed due to transient error _RETRY_ON_ITEM_NOT_FOUND_DYNAMODB_ERROR = error_handling.retry( timeoutSec=10, initialRetryDelaySec=0.5, maxRetryDelaySec=2, retryExceptions=(ItemNotFound,), logger=LOGGER ) class DynamoDBServiceTest(TestCaseBase): @classmethod def setUpClass(cls): cls.engine = repository.engineFactory(taurus.engine.config) def setUp(self): self.config = taurus.engine.config
pass class ConsumerCancelled(MessageBusConnectorError): """Message consumer was cancelled by remote peer""" pass # Decorator for retrying operations on potentially-transient AMQP errrors _RETRY_ON_AMQP_ERROR = error_handling.retry( timeoutSec=10, initialRetryDelaySec=0.05, maxRetryDelaySec=2, retryExceptions=( amqp.exceptions.AmqpChannelError, amqp.exceptions.AmqpConnectionError, amqp.exceptions.UnroutableError, socket.gaierror, socket.error, select.error, ), logger=g_log) class MessageProperties(amqp.messages.BasicProperties): """ basic.Properties of a message per AMQP 0.9.1 Some attributes are used by AMQP brokers, but most are open to interpretation by applications that receive them. """ pass
pass class ConsumerCancelled(MessageBusConnectorError): """Message consumer was cancelled by remote peer""" pass # Decorator for retrying operations on potentially-transient AMQP errrors _RETRY_ON_AMQP_ERROR = error_handling.retry( timeoutSec=10, initialRetryDelaySec=0.05, maxRetryDelaySec=2, retryExceptions=( amqp.exceptions.AmqpChannelError, amqp.exceptions.AmqpConnectionError, amqp.exceptions.UnroutableError, socket.gaierror, socket.error, select.error, ), logger=g_log) class MessageProperties(amqp.messages.BasicProperties): """ basic.Properties of a message per AMQP 0.9.1 Some attributes are used by AMQP brokers, but most are open to interpretation by applications that receive them. """ pass
import xmlrpclib from nta.utils import error_handling # Max time to wait for service to enter RUNNING state _RUNNING_TIMEOUT_SEC = 5 _METRICS_COLLECTOR_SUPERVISOR_API_URL = urljoin("http://localhost:8001", "RPC2") _RETRY_SERVICE_RUNNING_CHECK = error_handling.retry( _RUNNING_TIMEOUT_SEC, initialRetryDelaySec=1, maxRetryDelaySec=1) class TaurusMetricCollectorsHealthCheckTestCase(unittest.TestCase): @staticmethod def _getServiceStateName(serviceName): """Get the state name of a service :param str serviceName: name of service :returns: State name of the service; see http://supervisord.org/subprocess.html#process-states :rtype: str """
Emax=126, rounding=None, prec=5, # Reduce precision for overall reduction in payload size traps=[ Clamped, Overflow, Underflow ] ) # Decorator for retrying dynamodb operations that failed due to transient error _RETRY_ON_TRANSIENT_DYNAMODB_ERROR = error_handling.retry( timeoutSec=10, initialRetryDelaySec=0.5, maxRetryDelaySec=2, retryExceptions=(ProvisionedThroughputExceededException,), logger=g_log ) def convertDefineModelResultToMetricItem(modelCommandResult): """ Convert "defineModel" Model Command Result to MetricItem suitable for publishing to dynamodb. :param dict modelCommandResult: See model_command_result_amqp_message.json for schema. :returns: MetricItem instance suitable for publishing to the `taurus.metric` dynamodb table. :rtype: Instance of namedtuple implemented in `MetricDynamoDBDefinition().Item()` constructor
from htmengine.adapters.datasource import createDatasourceAdapter import taurus.engine from taurus.engine.exceptions import ObjectNotFoundError from taurus.engine import logging_support from taurus.engine.runtime.dynamodb.definitions import ( InstanceDataHourlyDynamoDBDefinition, MetricDynamoDBDefinition, MetricDataDynamoDBDefinition, MetricTweetsDynamoDBDefinition) from taurus.engine.runtime.dynamodb.dynamodb_service import DynamoDBService LOGGER = logging.getLogger(__name__) # Decorator for retrying dynamodb operations that failed due to transient error _RETRY_ON_ITEM_NOT_FOUND_DYNAMODB_ERROR = error_handling.retry( timeoutSec=10, initialRetryDelaySec=0.5, maxRetryDelaySec=2, retryExceptions=(ItemNotFound, ), logger=LOGGER) class DynamoDBServiceTest(TestCaseBase): @classmethod def setUpClass(cls): cls.engine = repository.engineFactory(taurus.engine.config) def setUp(self): self.config = taurus.engine.config self.plaintextPort = self.config.getint("metric_listener", "plaintext_port") @staticmethod
import tweepy from nta.utils import error_handling from nta.utils import error_reporting from nta.utils.message_bus_connector import MessageBusConnector from taurus_metric_collectors import collectorsdb from taurus_metric_collectors import metric_utils from taurus_metric_collectors.xignite import xignite_stock_agent _ACCESSIBILITY_TIMEOUT_SEC = 20 _RETRY_ACCESSIBILITY_CHECK = error_handling.retry( _ACCESSIBILITY_TIMEOUT_SEC, initialRetryDelaySec=1, maxRetryDelaySec=1) class TaurusMetricCollectorsResourceAccessibilityTestCase(unittest.TestCase): @_RETRY_ACCESSIBILITY_CHECK def testCollectorsdbIsAccessible(self): # pylint: disable=R0201 transactionContext = collectorsdb.engineFactory().begin() transactionContext.transaction.rollback() @_RETRY_ACCESSIBILITY_CHECK def testMessageBusIsAccessible(self): # pylint: disable=R0201
class MarketNewsDetailsFetchError(Exception): """ Error fetching Market News Details for an article """ pass # Initialize logging g_log = logging.getLogger("xignite_security_news_agent") # Retry decorator for specific errors _RETRY_ON_REQUESTS_ERROR = retry( timeoutSec=10, initialRetryDelaySec=0.05, maxRetryDelaySec=1, retryExceptions=( # requests retries on DNS errors, but not on connection errors requests.exceptions.ConnectionError, ), logger=g_log ) g_poolWorkerHttpSession = requests.Session() def _loadNewsVolumeMetricSpecs(): """ Load metric specs for our securities news volume provider :returns: a sequence of NewsVolumeMetricSpec objects """
) from nta.utils.amqp.queue import QueueDeclarationResult from nta.utils.amqp.synchronous_amqp_client import SynchronousAmqpClient from nta.utils.logging_support_raw import LoggingSupport from nta.utils.test_utils import amqp_test_utils _LOGGER = logging.getLogger(__name__) def setUpModule(): LoggingSupport.initTestApp() _RETRY_ON_ASSERTION_ERROR = retry(timeoutSec=20, initialRetryDelaySec=0.5, maxRetryDelaySec=2, retryExceptions=(AssertionError, ), logger=_LOGGER) _NUM_TEST_MESSAGES = 3 @amqp_test_utils.RabbitmqVirtualHostPatch(clientLabel="testingAmqpClient", logger=_LOGGER) class SynchronousAmqpClientTest(unittest.TestCase): """ Test for nta.utils.amqp.SynchronousAmqpClient """ def __init__(self, *args, **kwargs): super(SynchronousAmqpClientTest, self).__init__(*args, **kwargs) self.connParams = None self.client = None