Python tenacity.stop_after_delay() Examples

The following are 12 code examples of tenacity.stop_after_delay(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module tenacity , or try the search function .
Example #1
Source File: driver.py    From zun with Apache License 2.0 6 votes vote down vote up
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(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) 
Example #2
Source File: driver.py    From zun with Apache License 2.0 6 votes vote down vote up
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) 
Example #3
Source File: shell.py    From vaultlocker with Apache License 2.0 6 votes vote down vote up
def _do_it_with_persistence(func, args, config):
    """Exec func with retries based on provided cli flags

    :param: func: function to attempt to execute
    :param: args: argparser generated cli arguments
    :param: config: configparser object of vaultlocker config
    """
    @tenacity.retry(
        wait=tenacity.wait_fixed(1),
        reraise=True,
        stop=(
            tenacity.stop_after_delay(args.retry) if args.retry > 0
            else tenacity.stop_after_attempt(1)
            ),
        retry=(
            tenacity.retry_if_exception(hvac.exceptions.VaultNotInitialized) |
            tenacity.retry_if_exception(hvac.exceptions.VaultDown)
            )
        )
    def _do_it():
        client = _vault_client(config)
        func(args, client, config)
    _do_it() 
Example #4
Source File: api.py    From gnocchi with Apache License 2.0 5 votes vote down vote up
def post(self):
        buf = snappy.uncompress(pecan.request.body)
        f = remote_pb2.WriteRequest()
        f.ParseFromString(buf)
        measures_by_rid = collections.defaultdict(dict)
        for ts in f.timeseries:
            attrs = dict((l.name, l.value) for l in ts.labels)
            original_rid = (attrs.get("job", "none"),
                            attrs.get("instance", "none"))
            name = attrs['__name__']
            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 
Example #5
Source File: s3.py    From gnocchi with Apache License 2.0 5 votes vote down vote up
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 
Example #6
Source File: locking.py    From networking-generic-switch with Apache License 2.0 5 votes vote down vote up
def __enter__(self):
        self.lock = False
        if not self.coordinator:
            return self

        LOG.debug("Trying to acquire lock for %s", self.locks_prefix)
        names = itertools.cycle(self.lock_names)
        retry_kwargs = {'wait': tenacity.wait_random(min=0, max=1),
                        'reraise': True}
        if self.timeout:
            retry_kwargs['stop'] = tenacity.stop_after_delay(self.timeout)

        @tenacity.retry(**retry_kwargs)
        def grab_lock_from_pool():
            name = next(names)
            # NOTE(pas-ha) currently all tooz backends support locking API.
            # In case this changes, this should be wrapped to not respin
            # lock grabbing on NotImplemented exception.
            lock = self.coordinator.get_lock(name.encode())
            locked = lock.acquire(blocking=False)
            if not locked:
                raise coordination.LockAcquireFailed(
                    "Failed to acquire lock %s" % name)
            return lock

        try:
            self.lock = grab_lock_from_pool()
        except Exception:
            msg = ("Failed to acquire any of %s locks for %s "
                   "for a netmiko action in %s seconds. "
                   "Try increasing acquire_timeout." % (
                       self.locks_pool_size, self.locks_prefix,
                       self.timeout))
            LOG.error(msg, exc_info=True)
            raise
        return self 
Example #7
Source File: __init__.py    From networking-generic-switch with Apache License 2.0 5 votes vote down vote up
def _get_connection(self):
        """Context manager providing a netmiko SSH connection object.

        This function hides the complexities of gracefully handling retrying
        failed connection attempts.
        """
        retry_exc_types = (paramiko.SSHException, EOFError)

        # Use tenacity to handle retrying.
        @tenacity.retry(
            # Log a message after each failed attempt.
            after=tenacity.after_log(LOG, logging.DEBUG),
            # Reraise exceptions if our final attempt fails.
            reraise=True,
            # Retry on SSH connection errors.
            retry=tenacity.retry_if_exception_type(retry_exc_types),
            # Stop after the configured timeout.
            stop=tenacity.stop_after_delay(
                int(self.ngs_config['ngs_ssh_connect_timeout'])),
            # Wait for the configured interval between attempts.
            wait=tenacity.wait_fixed(
                int(self.ngs_config['ngs_ssh_connect_interval'])),
        )
        def _create_connection():
            return netmiko.ConnectHandler(**self.config)

        # First, create a connection.
        try:
            net_connect = _create_connection()
        except tenacity.RetryError as e:
            LOG.error("Reached maximum SSH connection attempts, not retrying")
            raise exc.GenericSwitchNetmikoConnectError(
                config=device_utils.sanitise_config(self.config), error=e)
        except Exception as e:
            LOG.error("Unexpected exception during SSH connection")
            raise exc.GenericSwitchNetmikoConnectError(
                config=device_utils.sanitise_config(self.config), error=e)

        # Now yield the connection to the caller.
        with net_connect:
            yield net_connect 
Example #8
Source File: test_tenacity.py    From tenacity with Apache License 2.0 5 votes vote down vote up
def test_stop_any(self):
        stop = tenacity.stop_any(
            tenacity.stop_after_delay(1),
            tenacity.stop_after_attempt(4))

        def s(*args):
            return stop(make_retry_state(*args))
        self.assertFalse(s(1, 0.1))
        self.assertFalse(s(2, 0.2))
        self.assertFalse(s(2, 0.8))
        self.assertTrue(s(4, 0.8))
        self.assertTrue(s(3, 1.8))
        self.assertTrue(s(4, 1.8)) 
Example #9
Source File: test_tenacity.py    From tenacity with Apache License 2.0 5 votes vote down vote up
def test_stop_all(self):
        stop = tenacity.stop_all(
            tenacity.stop_after_delay(1),
            tenacity.stop_after_attempt(4))

        def s(*args):
            return stop(make_retry_state(*args))
        self.assertFalse(s(1, 0.1))
        self.assertFalse(s(2, 0.2))
        self.assertFalse(s(2, 0.8))
        self.assertFalse(s(4, 0.8))
        self.assertFalse(s(3, 1.8))
        self.assertTrue(s(4, 1.8)) 
Example #10
Source File: test_tenacity.py    From tenacity with Apache License 2.0 5 votes vote down vote up
def test_stop_or(self):
        stop = tenacity.stop_after_delay(1) | tenacity.stop_after_attempt(4)

        def s(*args):
            return stop(make_retry_state(*args))
        self.assertFalse(s(1, 0.1))
        self.assertFalse(s(2, 0.2))
        self.assertFalse(s(2, 0.8))
        self.assertTrue(s(4, 0.8))
        self.assertTrue(s(3, 1.8))
        self.assertTrue(s(4, 1.8)) 
Example #11
Source File: test_tenacity.py    From tenacity with Apache License 2.0 5 votes vote down vote up
def test_stop_and(self):
        stop = tenacity.stop_after_delay(1) & tenacity.stop_after_attempt(4)

        def s(*args):
            return stop(make_retry_state(*args))
        self.assertFalse(s(1, 0.1))
        self.assertFalse(s(2, 0.2))
        self.assertFalse(s(2, 0.8))
        self.assertFalse(s(4, 0.8))
        self.assertFalse(s(3, 1.8))
        self.assertTrue(s(4, 1.8)) 
Example #12
Source File: test_tenacity.py    From tenacity with Apache License 2.0 5 votes vote down vote up
def test_stop_after_delay(self):
        r = Retrying(stop=tenacity.stop_after_delay(1))
        self.assertFalse(r.stop(make_retry_state(2, 0.999)))
        self.assertTrue(r.stop(make_retry_state(2, 1)))
        self.assertTrue(r.stop(make_retry_state(2, 1.001)))