Skip to main content

Nameko extension allowing AMQP entrypoints to retry later

Project description

nameko-amqp-retry
=================

Extension for `nameko <http://nameko.readthedocs.org>`_ that allows the built-in AMQP entrypoints to schedule a later retry via redelivery of a message.

RabbitMQ 3.5.4 or later is required.


Installation
------------

Install the library from PyPI::

pip install nameko-amqp-retry


Usage
-----

This library subclasses nameko's built-in entrypoints. Use these subclasses in your service definition, and then raise :class:`nameko_amqp_retry.Backoff` inside an entrypoint you wish to retry later:

.. code-block:: python

from nameko_amqp_retry import Backoff
from nameko_amqp_retry.rpc import rpc

class Service:
name = "service"

@rpc
def calculate(self):
""" Calculate something, or schedule a retry if not ready yet.
"""
if not_ready_yet:
raise Backoff()

return 42

The caller will see the final result, or a :class:`Backoff.Expired` exception if more than the allowed number of retries were made:

.. code-block:: python

>>> n.rpc.service.calculate()
... # blocks for some time
>>> 42

.. code-block:: python

>>> n.rpc.service.calculate()
Traceback (most recent call last):
...
nameko.exceptions.RemoteError: Expired Backoff aborted after ...

The retry schedule is controlled by attributes on the `Backoff` class. You should override them in a subclass to control the behaviour. For example:

Fixed schedule:

.. code-block:: python

class RegularBackoff(Backoff):
""" Retries every 1000ms until limit
"""
schedule = (1000,) # ms

No limit:

.. code-block:: python

class InfiniteBackoff(Backoff):
""" Retries forever
"""
limit = 0


Custom schedule:

.. code-block:: python

class ImpatientBackoff(Backoff):
""" Retries after 100, then 200, then 500 milliseconds
"""
schedule = (100, 200, 500) # ms


Dynamic schedule:

.. code-block:: python

class DynamicBackoff(Backoff):
""" Calculates schedule dynamically
"""
@classmethod
def get_next_schedule_item(cls, index):
...

Alternatively, an entrypoint can be decorated with the `entrypoint_retry` decorator, to automatically retry the method if it raises certain exceptions:

.. code-block:: python

from nameko_amqp_retry import entrypoint_retry
from nameko_amqp_retry.rpc import rpc

class Service:
name = "service"

@rpc
@entrypoint_retry(retry_for=ValueError)
def calculate(self):
""" Calculate something, or schedule a retry if not ready yet.
"""
if not_ready_yet:
raise ValueError()

return 42

@rpc
@entrypoint_retry(
retry_for=(TypeError, ValueError),
limit=5,
schedule=(500, 600, 700, 800, 900),
)
def do_something(self):
""" Calculate something else, or schedule a retry if not ready yet.
"""
if type_not_ready_yet:
raise TypeError()

if value_not_ready_yet:
raise ValueError()

return 24


See docs/examples for more.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nameko-amqp-retry-0.3.1.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

nameko_amqp_retry-0.3.1-py2-none-any.whl (7.5 kB view details)

Uploaded Python 2

File details

Details for the file nameko-amqp-retry-0.3.1.tar.gz.

File metadata

File hashes

Hashes for nameko-amqp-retry-0.3.1.tar.gz
Algorithm Hash digest
SHA256 484e18e7150c59ea61af4e4704ce1f3937ab5ab0887edf4151fcafc620ef36e1
MD5 7c4279701c69d894c6e318b9010d9fee
BLAKE2b-256 cb8b5b37c58de6c4375c01928b488ed804a47d87b31e349700761883bc173a19

See more details on using hashes here.

File details

Details for the file nameko_amqp_retry-0.3.1-py2-none-any.whl.

File metadata

File hashes

Hashes for nameko_amqp_retry-0.3.1-py2-none-any.whl
Algorithm Hash digest
SHA256 5ac424c3a21e25bd996bed307ba61abc8ff80e981fad4fcd8d7991dd7316fc8f
MD5 4c6093604a1e4183671c0f7d60b109f5
BLAKE2b-256 dd4de4d09c21ce7f8e6fb8d05bb928589f0672a3af97fbe97940cb5cbcd4ea70

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page