GithubHelp home page GithubHelp logo

django-db-retry's Introduction

PyPI version PyPI - Python Version GitHub stars PyPI - Downloads GitHub issues

Description

Django database retries. Main motivation for using this - to deal with network/db issues to avoid data-loss.

Usually when you develop a project locally with a single user - everything works perfectly. Local networks are super-stable, and simultaneous users don't bother you. When it comes to the real world - your application can (and definitely will) face network issues. Second case - deadlocks, which I personally see too often across different projects. And the only possible solution here to avoid the data-loss - to do a query retry.

IMPORTANT: Right now this works ONLY WITH MYSQL. If someone requires postgres/other dbs support - please create an issue

Usage

Install: pip install django-db-retry

Choose your flow (or use both):

  • Monkey-patch django internal methods (IMPORTANT: global patching won't handle retries for atomic transactions. with_query_retry decorator should be used instead)
  • Use explicit decorator

Monkey-patch:

Add next code somewhere on the top level of your project

from django_db_retry import install as install_db_retries
install_db_retries()

That's it =) All the needed underlying django methods will be wrapped with the retry decorator and will do execution retry if your app will face a network issue or deadlock

Decorator

Can be used on top of any function/view and will do a retry if deadlock/network error will happen. Default number of retries is 5. This value can be configured by using the QueryRetry class (see example 2):

from django_db_retry import with_query_retry
from django.db.transaction import atomic

@with_query_retry
def some_view():
    query_0, query_1 = ...
    with atomic():
        query_0()
        query_1()
    return ...

Configuring own value of retries

from django_db_retry import QueryRetry
from django.db.transaction import atomic

my_retry_decorator = QueryRetry(max_tries=100)

@my_retry_decorator
def some_view():
    query_0, query_1 = ...
    with atomic():
        query_0()
        query_1()
    return ...

Todo

  • Deal with atomic transactions during global patching
  • Add possibility to install the package using django INSTALLED_APPS settings

django-db-retry's People

Contributors

parikls avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.