Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_stop_after_delay(self):
r = Retrying(stop=tenacity.stop_after_delay(1000))
self.assertFalse(r.stop(2, 999))
self.assertTrue(r.stop(2, 1000))
self.assertTrue(r.stop(2, 1001))
stop=tenacity.stop_after_delay(
int(self.ngs_config['ngs_commit_timeout'])),
# Wait for the configured interval between attempts.
wait=tenacity.wait_fixed(
int(self.ngs_config['ngs_commit_interval'])),
)
def commit():
try:
net_connect.commit()
except ValueError as e:
# Netmiko raises ValueError on commit failure, and appends the
# CLI output to the exception message.
# Certain strings indicate a temporary failure, or a harmless
# warning. In these cases we should retry the operation. We
# don't ignore warning messages, in case there is some other
# less benign cause for the failure.
stop=tenacity.stop_after_delay(check_timeout))
def _get_hosts_retry(target, listener_type):
return method(target, listener_type)
def __init__(self, conf):
super(S3Storage, self).__init__(conf)
self.s3, self._region_name, self._bucket_prefix = (
s3.get_connection(conf)
)
self._bucket_name = '%s-aggregates' % self._bucket_prefix
if conf.s3_check_consistency_timeout > 0:
self._consistency_stop = tenacity.stop_after_delay(
conf.s3_check_consistency_timeout)
else:
self._consistency_stop = None
stop=tenacity.stop_after_delay(30),
reraise=True)
def _get_creds(client, task_id, environment_id):
result = m_cli.actions.get_result(environment_id,
task_id)['result']
return result
def send_request(self, socket, request):
if hasattr(request, 'timeout'):
_stop = tenacity.stop_after_delay(request.timeout)
elif request.retry is not None and request.retry > 0:
# no rpc_response_timeout option if notification
_stop = tenacity.stop_after_attempt(request.retry)
else:
# well, now what?
_stop = tenacity.stop_after_delay(60)
@tenacity.retry(retry=tenacity.retry_if_exception_type(zmq.Again),
stop=_stop)
def send_retrying():
if request.msg_type in zmq_names.MULTISEND_TYPES:
for _ in range(socket.connections_count()):
self.sender.send(socket, request)
else:
self.sender.send(socket, request)
return send_retrying()
if ts.samples:
data = [{'timestamp': s.timestamp_ms / 1000.0,
'value': s.value} for s in ts.samples]
measures_by_rid[original_rid][name] = validate(
MeasuresListSchema, data)
creator = pecan.request.auth_helper.get_current_user(pecan.request)
measures_to_batch = {}
for (job, instance), measures in measures_by_rid.items():
original_rid = '%s@%s' % (job, instance)
rid = ResourceUUID(original_rid, creator=creator)
metric_names = list(measures.keys())
timeout = pecan.request.conf.api.operation_timeout
metrics = get_or_create_resource_and_metrics.retry_with(
stop=tenacity.stop_after_delay(timeout))(
creator, rid, original_rid, metric_names,
dict(job=job, instance=instance),
"prometheus", self.PROMETHEUS_RESOURCE_TYPE)
for metric in metrics:
enforce("post measures", metric)
measures_to_batch.update(
dict((metric.id, measures[metric.name]) for metric in
metrics if metric.name in measures))
pecan.request.incoming.add_measures_batch(measures_to_batch)
pecan.response.status = 202
stop=stop_after_delay(120))
def init_db_pool(db_min_connections, db_max_connections):
"""
Initializes the database connection pool required by the application to connect to the database.
"""
db = config.DATABASE
host = config.DATABASE_SERVER
user = config.DATABASE_USER
pw = config.DATABASE_PASSWORD
global connection_pool
if connection_pool is None:
logger.info("Initializing the database connection pool. db: '%s', user: '%s', host: '%s'.", db, user, host)
try:
connection_pool = ThreadedConnectionPool(db_min_connections, db_max_connections, database=db, user=user,
password=pw, host=host)
except psycopg2.Error as e:
def _wait_for_init_container(self, context, container, timeout=3600):
def retry_if_result_is_false(result):
return result is False
def check_init_container_stopped():
status = self._show_container(context, container).status
if status == consts.STOPPED:
return True
elif status == consts.RUNNING:
return False
else:
raise exception.ZunException(
_("Container has unexpected status: %s") % status)
r = tenacity.Retrying(
stop=tenacity.stop_after_delay(timeout),
wait=tenacity.wait_exponential(),
retry=tenacity.retry_if_result(retry_if_result_is_false))
r.call(check_init_container_stopped)
def __init__(self, retry_period, wrapped_connection):
self.wrapped_connection = wrapped_connection
self.retry_kwargs = dict(
retry=(retry_if_exception_type(redis.exceptions.ConnectionError)
| retry_if_exception_type(redis.exceptions.TimeoutError)),
reraise=True,
wait=wait_exponential(multiplier=1, max=self.RETRY_MAX_WAIT),
before_sleep=self._log_retry_attempt
)
if retry_period >= 0:
self.retry_kwargs.update(dict(stop=stop_after_delay(retry_period)))