Sunday, 26 February 2023

Running different celery tasks (@shared_tasks) on different databases in django multi tenant application

The application I am working on is a multitenant application. I am using celery to run background tasks.

The background tasks that get pushed to Queue (rabbitmq) are getting executed properly when run on the default db setting configured in the settings. But when I submit the background jobs from other tenants, i.e. settings other than default they fail. This is because, in normal sync flow, I am using a custom router that sets the DB to be used based on the request URL( which contains the tenant details). but the same context is lost when the job is submitted as a background task.

Any suggestions ?

I tried using transaction block and passing the db_name as below, but still, it is using the default database only

@shared_task
def run_background_task(task_id, db_name):
    with transaction.atomic(using=tenant_db):
        task = DPTask.objects.get(pk=task_id)

Ideally, the above query should get executed on db_name database settings, but it is happening on default only



from Running different celery tasks (@shared_tasks) on different databases in django multi tenant application

No comments:

Post a Comment