Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Celery compatibility issue #213

Open
christokritz opened this issue Mar 7, 2024 · 1 comment
Open

Celery compatibility issue #213

christokritz opened this issue Mar 7, 2024 · 1 comment

Comments

@christokritz
Copy link

I'm using django-multitenant together with Celery and a Redis broker.

I've added a tenant_id parameter to all my celery functions. When the tasks start it calls 'set_current_tenant' with this tenant ID.

In one case I forgot to call 'set_current_tenant', however, it used the tenant_id of the task that was last ran. It is as if it remembered the tenant_id of another task that another user (of another tenant) ran.

I thought that this shouldn't be possible, so I'm worried about potential unintended consequences this may have, such as race conditions.

Is this project compatible with Celery and if so, is there any particular configuration I should do?

@aprams
Copy link

aprams commented Oct 11, 2024

Hi @christokritz, I'm not a maintainer, but we faced a similar situation. As the tenant is just saved as a thread variable, I suggest wrapping your celery tasks, something like TenantSafeCeleryTask:

from celery import Task
class TenantSafeCeleryTask(Task):
    passs

And then making it the default class:

app = Celery(
    "my_app", task_cls="my_app.tasks.TenantSafeCeleryTask"
)

In the custom task, we check if a tenant id is passed and with before_start and after_return we set / unset the tenant safely. Celery doesn't know about multitenant and multitenant relies on you to set the tenant context, so this seemed like the safest version for us.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants