Exemplo n.º 1
0
    def add(self, name, retry_timeout=False, **request_kwargs):
        """Add asset.

        In case of timeout, block till resource is created.
        """
        try:
            return self._add(name, **request_kwargs)

        except exceptions.RequestTimeout as e:
            key = e.key
            is_many = isinstance(
                key, list)  # timeout on many objects is not handled
            if not retry_timeout or is_many:
                raise e

            logger.warning(
                f'Request timeout, blocking till {name} is created: key={key}')
            retry = utils.retry_on_exception(
                exceptions=(exceptions.RequestTimeout),
                timeout=float(retry_timeout),
            )
            # XXX as there is no guarantee that the request has been sent to the ledger
            #     (and will be processed), retry on on the add request and ignore
            #     potential conflicts
            return retry(self._add)(name, **request_kwargs)
Exemplo n.º 2
0
    def add(self,
            name,
            retry_timeout=DEFAULT_RETRY_TIMEOUT,
            exist_ok=False,
            **request_kwargs):
        """Add asset.

        In case of timeout, block till resource is created.

        If `exist_ok` is true, `AlreadyExists` exceptions will be ignored and the
        existing asset will be returned.
        """
        try:
            return self.request(
                'post',
                name,
                **request_kwargs,
            )

        except exceptions.RequestTimeout as e:
            logger.warning('Request timeout, blocking till asset is created')
            key = e.pkhash
            is_many = isinstance(
                key, list)  # timeout on many objects is not handled
            if not retry_timeout or is_many:
                raise e

            retry = utils.retry_on_exception(
                exceptions=(exceptions.NotFound),
                timeout=float(retry_timeout),
            )
            return retry(self.get)(name, key)

        except exceptions.AlreadyExists as e:
            if not exist_ok:
                raise

            key = e.pkhash
            is_many = isinstance(key, list)
            if is_many:
                logger.warning(
                    "Many assets not compatible with 'exist_ok' option")
                raise

            logger.warning(f"{name} already exists: key='{key}'")
            return self.get(name, key)
Exemplo n.º 3
0
 def retry(self, func):
     return utils.retry_on_exception(
         exceptions=(exceptions.RequestTimeout),
         timeout=float(self.retry_timeout),
     )(func)