Metadata-Version: 2.4
Name: yretry
Version: 1.0.1
Summary: Retries library
Home-page: https://github.com/infraguys/yretry
Author: InfraGuys
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Operating System :: POSIX :: Linux
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 2
Classifier: Programming Language :: Python :: 2.7
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.6
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.12
Description-Content-Type: text/x-rst
License-File: LICENSE
Requires-Dist: pbr<=5.8.1,>=1.10.0
Requires-Dist: requests<3.0.0,>=2.32.4
Requires-Dist: six<=1.16.0,>=1.13.0
Dynamic: author
Dynamic: classifier
Dynamic: description
Dynamic: home-page
Dynamic: license-file
Dynamic: requires-dist
Dynamic: summary

Library to make your services reliable
======================================

Retry on specific exception
---------------------------

.. code-block:: python

  import yretry

  @yretry.decorators.retry(attempts_number=3,
                           retry_on=(MyException,))
  def reliable_function():
      raise MyException()


Specific function to make decision about retries
------------------------------------------------

.. code-block:: python

  import yretry

  def is_my_mistake(error):
      return isinstance(error, MyMistake)

  @yretry.decorators.retry(attempts_number=3,
                           retry_on=is_my_mistake)
  def reliable_function():
      raise MyMistake()


Retry on network errors
-----------------------

You can use following code to add retries for your custom network
function:

.. code-block:: python

  import requests
  import yretry

  @yretry.network.retry()
  def reliable_function():
     response = requests.get('http://localhost:5002')
     response.raise_for_status()
     return response

  print reliable_function()


Logging
=======

Global logger
-------------

You can pass specific logger to decorator:

.. code-block:: python

  import logging
  import logging.config

  LOGGING = {
      'version': 1,
      'formatters': {
          'precise': {
              'datefmt': '%Y-%m-%d,%H:%M:%S',
              'format': '%(levelname)-7s %(asctime)15s '
                        '%(name)s:%(lineno)d %(message)s'
          }
      },
      'handlers': {
          'console': {
              'class': 'logging.StreamHandler',
              'formatter': 'precise',
              'stream': 'ext://sys.stderr'
          },
      },
      'root': {
          'level': 'INFO',
          'handlers': ['console']
      }
  }

  logging.config.dictConfig(LOGGING)

  LOGGER = logging.getLogger(__name__)

  @yretry.network.retry(logger=LOGGER)
  def reliable_function():
     response = requests.get('http://localhost:5002')
     response.raise_for_status()
     return response


Object-specific logger
----------------------

You can use object-specific logger, just add 'get_logger' method:

.. code-block:: python

  class MyClass(object):
     def __init__(self):
         self._logger = logging.getLogger(__name__)

     def get_logger(self):
         return self._logger

     @yretry.network.retry()
     def reliable_method(self):
         pass

  obj = MyClass()
  obj.reliable_method()
  # obj._logger will be used to write warnings about retries

