Skip to content

Add retry logic to your request function

Sometimes you have a deployment where you know in advance that requests might fail due to reasons out of your control. Think about deployments that set up connections to databases for instance. The connection might fail if there are too many entities trying to access that database at the same moment. In a scenario like that, you typically want the request to retry after a small time interval. This type of retry logic can be added to any function in your deployment class and we'll walk you through it in this howto!

Add the ubiops-connector package to your environment

The retry logic can be added with the use of a retry decorator. To use it, you need the ubiops-connector package. Make sure you include ubiops-connector in your requirements.txt of your deployment.

Contents of the requirements.txt:

ubiops-connector
# add any other dependencies you need

Import the retry decorator in deployment.py

In your deployment code, you can import the retry decorator from the ubiops-connector package. Also import the exception that the retry logic will react to.

from ubiops_connector import retry, RecoverableConnectorError

Add the decorator around your function

Now you can add the retry decorator above the function that needs to be retried.

@retry(attempts=3)
def foo():
    # Some function

    return {}

Raise the RecoverableConnectorError

Make sure you raise the RecoverableConnectorError when an error occurs for which you want to retry.

@retry(attempts=3)
def foo():
    # Some function
    raise RecoverableConnectorError
    return {}

This function above will now retry 3 times. It will fail every time because we raise the error every time the function is run.

Resulting deployment code

Below you can find an example deployment.py that retries the request function once.

from ubiops_connector import retry, RecoverableConnectorError

class Deployment():
    def __init__(self, base_directory, context):
        print("Initialising deployment retry-deployment")
        self.has_failed = False

    @retry(attempts=3)
    def request(self, data):
        print("Processing request for deployment retry-deployment")
        if (not self.has_failed):
            print("This attempt failed")
            self.has_failed = True
            raise RecoverableConnectorError
        else:
            print("Now it's succesful!")

        return {}

You can add the retry decorator to any function in your Deployment class, including the request, requests and __init__ functions.