QuickRelease / django-tenants-celery-beat

MIT License
12 stars 9 forks source link

Equivalency of `all_tenants` in `tenancy_options` in the Django Admin? #19

Open ZipBrandon opened 2 years ago

ZipBrandon commented 2 years ago

I'm unsure from the documentation how to achieve the following config in Period Tasks for Django admin. Previous to django-tenants-celery-beat I'd just create a task and iterate over all tenants using their tenant_context but I was wondering if I could circumvent this with django-tenants-celery-beat with database-created tasks.

"tenancy_options": {
    "public": False,
    "all_tenants": True,
    "use_tenant_timezone": True,
}
dave-v commented 2 years ago

Are you asking what options you need to use, or where to put this configuration?

A way to register periodic tasks is to set the beat_schedule dict (https://docs.celeryproject.org/en/stable/userguide/periodic-tasks.html#entries and scroll down a little).

This library just provides a function that duplicates the tasks you would normally put in that dict according to the schemas/tenants you want it to run on, as described here https://github.com/QuickRelease/django-tenants-celery-beat#setting-up-a-beat_schedule.

ZipBrandon commented 2 years ago

Specifically I'm asking for the PeriodTask that can be created in the database. I see that we can isolate to a single tenant with the PeriodTaskTenantLink. Would the ------- option run in all tenants or just public schema? If just the public schema, how do you create a single task that executes in all tenants? image

dave-v commented 2 years ago

Would the ------- option run in all tenants or just public schema?

You have to select a tenant for the form to be valid, i.e. -------- is not a valid option.

If just the public schema, how do you create a single task that executes in all tenants?

You have to create one task per tenant. If you use the admin interface, you have to create them all by hand, but if you use the beat_schedule dict to define them, one task per tenant will be created automatically.

If you provide these options:

"tenancy_options": {
    "public": False,
    "all_tenants": True,
    "use_tenant_timezone": True,
}

You will get multiple PeriodicTasks created for you, one per tenant. Each PeriodicTask will have a PeriodicTaskTenantLink linking to the respective tenant.

You can alternatively create a single task and run it on all tenants, but you need to handle that logic within the task itself.