Skip to main content

An integration package for Django-Q with Django Tenants

Project description

Django Tenants Q

Django Tenants Q is a custom implementation for Django-Q cluster for projects that make use Django Tenants package to achieve multi-tenancy in their Django projects with Postgres as database service. Using this package, developer can setup a Django project to be multi-tenant and setup Django-Q cluster to work per tenant/schema basis. This package works with any other broker apart from Django ORM.

Note: This Library is the replica of the library django-tenant-schemas-q

Full Credit goes to the author Rajesh Yogeshwar

I have just modified it to work with django_tenants.

Installation

pip install django-tenants-q

This should install django-tenants and django-q.

How to set this up?

As a developer I often find that setting up projects, packages and getting configurations right is sometimes a relatively tough job. In order to make this work nicely, just follow the steps below.

  • Refer to settings required by django_tenants mentioned here

  • Setup django_tenants as per the instructions in the above mentioned link. This will take care of setting up of django_tenants.

  • Add django_q to TENANT_APPS setting. This will setup models like Task, Schedule on every tenant post migrations.

  • Follow it up with setting up the Django-Q cluster that needs to be run in order to process tasks and run our scheduler.

  • While setting up configuration for cluster, make use of any other broker supported by Django-Q apart from Django ORM.

  • Finally add a setting SCHEMAS_TO_BE_EXCLUDED_BY_SCHEDULER in your settings file. The value for this setting is a list of schema that you wish to exclude from scheduler, If not specified, scheduler will exclude a schema by name public by default.

How will this work

Assuming you have followed the instructions perfectly, you should now have settings looking like

SHARED_APPS = ['django_tenants', 'app_containing_tenant_model ', 'any_other_app_needed']

TENANT_APPS = ['django_q', 'standard_django_apps', 'any_other_app_needed']

INSTALLED_APPS = ['django_tenants', 'app_containing_tenant_model', 'django_q', 'standard_django_apps', 'any_other_app_needed']

TENANT_MODEL = 'app_name.ModelName'

SCHEMAS_TO_BE_EXCLUDED_BY_SCHEDULER = ['public']

Q_CLUSTER = {} # Configuration for Q Cluster

What these settings will do is simple. It will simply create Task & Schedule models per schema. Django-Q uses these modules to store the tasks and schedules. With these models now setup on per schema/tenant basis the things become a bit cleaner.

To run the cluster, use the command python manage.py mscluster

Once the command is fired, the cluster will start and accept the tasks and schedules.

Now, given that are schedules are tasks that are executed as per the frequency having them per schema/tenant basis is something that I wanted to achieve. This would give me ability to configure same tasks with different times and the system will work as expected.

How to use it

To allow adding a task or schedule with database schema awareness, package contains utilities to make it is seamless.

Add the line from django_tenants_q.utils import QUtilities to the line from where you wish to add a task or a schedule

To add an async task you can use

QUtilities.add_async_task(func_name_as_string, *args, **kwargs)

You can add sync=True in kwargs to run the task synchronously.

To create a new schedule you can use

QUtilities.create_schedule(func_name_as_string, *args, **kwargs)

To get a result of a single task

QUtilities.get_result(task_id, wait=0, cached=Conf.CACHED)

To get a result of a group

QUtilities.get_result_group(group_id, failures=False, wait=0, count=None, cached=Conf.CACHED)

To fetch a single task

QUtilities.fetch_task(task_id, wait=0, cached=Conf.CACHED)

To fetch a group of tasks

QUtilities.fetch_task_group(group_id, failures=True, wait=0, count=None, cached=Conf.CACHED)

To get count of groups

QUtilities.get_group_count(group_id, failures=False, cached=Conf.CACHED)

To delete a group of tasks

QUtilities.delete_task_group(group_id, tasks=False, cached=Conf.CACHED)

To delete a task from cache

QUtilities.delete_task_from_cache(task_id, broker=None)

To get the size of the queue

 QUtilities.get_queue_size(broker=None)

To add multiple async tasks using Iter

QUtilities.add_async_tasks_from_iter(func, args_iter, **kwargs)

For this use Iter from django_tenants_q.custom module.

To create a chain of tasks using Chain

QUtilities.create_async_tasks_chain(chain, group=None, cached=Conf.CACHED, sync=Conf.SYNC, broker=None)

For this use Chain from django_tenants_q.custom module.

Test the project

There is a test django project in the repository.

  • Clone the repository
  • Run docker-compose -f test-compose.yml build
  • Run docker-compose -f test-compose.yml up -d
  • Run docker-compose -f test-compose.yml run backend python manage.py setupdata
  • Run docker-compose -f test-compose.yml run backend python manage.py test --keepdb

Full credit to authors https://github.com/Koed00 of Django-Q and https://github.com/django-tenants of Django-Tenants for two wonderful packages.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_tenants_q-1.0.0.tar.gz (15.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_tenants_q-1.0.0-py3-none-any.whl (15.2 kB view details)

Uploaded Python 3

File details

Details for the file django_tenants_q-1.0.0.tar.gz.

File metadata

  • Download URL: django_tenants_q-1.0.0.tar.gz
  • Upload date:
  • Size: 15.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10

File hashes

Hashes for django_tenants_q-1.0.0.tar.gz
Algorithm Hash digest
SHA256 17e1d59b931d83f3e7ade80ed356284599286d430d64b188b00f0213b1f1651a
MD5 86c7352c9865e3855b231680baa29500
BLAKE2b-256 a69c45baef5d58d40f41af5f94381b18b76624e12d5f8d8a23bb0bd41aacd686

See more details on using hashes here.

File details

Details for the file django_tenants_q-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: django_tenants_q-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 15.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10

File hashes

Hashes for django_tenants_q-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6a31c040a1a9b030cc1ce060cb410df55ca41d88cbef62f953b8231e5cdd144b
MD5 78851525cfc444d4ad0db8eaca23b9a6
BLAKE2b-256 93f7cbcb5e5bc33b694f72f89f14935d79b3270c1db3ccc89faae6d20e1ac657

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page