Skip to content
GroundhogDay lets you repeat an operation until you get it right.
Python
Find file
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
groundhogday
test
.gitignore
LICENSE.txt
README.md
blog_post.md
setup.py

README.md

GroundhogDay

GroundhogDay helps you build resilient code in front of less than resilient services. It does so by exposing convenient retry decorators with callbacks for handling error conditions.

Examples

Here's some examples of how you might use GroundhogDay:

from groundhogday import GroundhogDay

class MyClass(object):
    @GroundhogDay
    def my_function(self):
        print 'my message'
        raise Exception('my exception')

MyClass().my_function()

If an exception is raised, the default decorator will retry the method my_function four times, with an initial backoff of 1.7 seconds that grows exponentially.

import groundhogday
from groundhogday import GroundhogDay

class MyClass(object):
    @GroundhogDay(backoff=groundhogday.LINEAR, maximum_retry_callback='maximum_retries_reached')
    def my_function(self):
        print 'my message'
        raise Exception('my exception')

    def maximum_retries_reached(self, last_error):
        print str(last_error)

MyClass().my_function()

This example uses a linear backoff rather than an exponential backoff. It will execute the maximum_retries_reached method after the fourth exception.

Usage

The Groundhog Day decorator can be configured with the following parameters.

  • maximum_retries: how many retries before raising an exception.
  • sleep_time: how long should Groundhog Day sleep before invoking the method again.
  • backoff: the backoff type, either LINEAR or EXPONENTIAL (defaults to EXPONENTIAL).
  • notification_threshold: an optional threshold for configuring a one time notification, prior to retries actually terminating.
  • notification_callback: the name of a callback to execute when the notification threshold is reached.
  • exception_callback: the name of a callback to execute after each exception occurs.
  • maximum_retry_callback: the name of a method to execute after a maximum number of retries is hit.
  • environment: set environment to test to disable sleeping and airbrake notifications.

Airbrake Decorator

Groundhog Day provides a decorator that integrates with Airbrake

from groundhogday import RetryWithAirbrake

class MyClass(object):
    @RetryWithAirbrake(
        name='Python-Crawling-Stack',
        version='1.0.0',
        url='http://attachments.me',
        environment_name='production',
        api_key='[AIRBRAKE-API-KEY]'
    )
    def method_that_sends_error_to_airbrake(self):
        raise Exception('This exception will be sent to Airbrake')
  • You can also provide any of the configuration options available in the GroundhogDay decorator.
  • Rather than providing all the API information in the decorator, you can export the PYTOAD_CONFIG_DIRECTORY environment variable.

Sentry Decorator

Groundhog Day also provides a decorator that integrates with Sentry

from groundhogday import RetryWithSentry

class MyClass(object):
    @RetryWithSentry(
        dsn='[SENTRY-PROJECT-URL]'
    )
    def method_that_sends_error_to_sentry(self):
        raise Exception('This exception will be sent to Sentry')
  • You can provide any keyword arguments that raven supports.
  • You can also provide any of the configuration options available in the GroundhogDay decorator.

Copyright

Copyright (c) 2011 Attachments.me. See LICENSE.txt for further details.

Something went wrong with that request. Please try again.