celery / django-celery-results

Celery result back end with django
Other
696 stars 206 forks source link

Fix atomic transaction not routing to the the correct DB in DatabaseBackend.on_chord_part_return transaction.atomic #427

Closed gianbot closed 6 months ago

gianbot commented 7 months ago

Hi, when using chord with Database result backend and multiple databases with a Database Router we encounter this error: select_for_update cannot be used outside of a transaction. This depends on the fact that in DatabaseBackend.on_chord_part_return a transaction.atomic is created on the default database and not on the one associated with ChordCounter.

We write this simple fix for handle also this case.

Setting we configured:

DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.postgresql",
        "NAME": "data",
    },
    "celery_result": {
        "ENGINE": "django.db.backends.postgresql",
        "NAME": "results",
    },
}
DATABASE_ROUTERS = [
    "app.db_routers.DjangoCeleryResultRouter",
]

Router class:

class DjangoCeleryResultRouter:
    route_app_labels = {"django_celery_results"}

    def db_for_read(self, model, **hints):
        if model._meta.app_label in self.route_app_labels:
            return "celery_result"
        return None

    def db_for_write(self, model, **hints):
        if model._meta.app_label in self.route_app_labels:
            return "celery_result"
        return None

    def allow_relation(self, obj1, obj2, **hints):
        if obj1._meta.app_label in self.route_app_labels or obj2._meta.app_label in self.route_app_labels:
            return True
        return None

    def allow_migrate(self, db, app_label, model_name=None, **hints):
        if app_label in self.route_app_labels:
            return db == "celery_result"
        return None
AllexVeldman commented 7 months ago

related: #422

gianbot commented 7 months ago

Hi @AllexVeldman , we are working on the tests and will update you as soon as possible.

gianbot commented 6 months ago

Hi @AllexVeldman , thanks for the feedback. We have updated the tests following the suggestions. Can you check if that's ok?

auvipy commented 6 months ago

thanks both of you

gianbot commented 5 months ago

Hi @auvipy, when do you plan to release the next release on PyPI that also contains this change?

auvipy commented 5 months ago

Yes