quickrelease / django-tenants-celery-beat Goto Github PK
View Code? Open in Web Editor NEWLicense: MIT License
License: MIT License
Hi,
Great add-on, thanks. I have "django_celery_results" on "shared_apps" and "tenant_apps" but for some reason all tenants results are being saved on django_celery_results_taskresult
table at the public_schema
and not at tenant level.
Does anyone know how to fix this? I would like to log the results by tenant.
Looks like the migration 0001_initial.py
was mistakenly left out. This means that it will not work unless the user runs makemigrations
. This isn't typically run during CI or docker build or anything like that leaving the app broken for production. In an environment like heroku where the file system is reset whenever a dyno restart occurs, the migration will go missing.
Nice package. This issue adds some more information to your celery.backend_cleanup example.
There were some additional steps to get the example to work. Consider these as possible documentation additions.
First - where does the example code go: polls/polls/celery.py
Second - you have to import crontab from somewhere. Eventually you will find you should import from celery.schedules
from celery.schedules import crontab
Third - to use generate_beat_schedule you will have to import it from django_tenants_celery_beat.utils , however this import calls the project's settings environment, so you need to populate from settings before you import generate_beat_schedule. Something like this:
# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'polls.settings')
# must follow settings configuration as gen_beat_sch references settings
from django_tenants_celery_beat.utils import generate_beat_schedule
Finally, perhaps obvious to others (not me), but you need to have the beat_schedule config info after autodiscover_tasks()
app.autodiscover_tasks()
app.conf.beat_schedule = generate_beat_schedule(
Full example:
import os
from celery.schedules import crontab
from tenant_schemas_celery.app import CeleryApp as TenantAwareCeleryApp
# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'polls.settings')
# must follow settings configuration as gen_beat_sch references settings
from django_tenants_celery_beat.utils import generate_beat_schedule
app = TenantAwareCeleryApp('polls')
# - namespace='CELERY' means all celery-related configuration keys
# should have a `CELERY_` prefix.
app.config_from_object('django.conf:settings', namespace='CELERY')
# Load task modules from all registered Django app configs.
app.autodiscover_tasks()
app.conf.beat_schedule = generate_beat_schedule(
{
"celery.backend_cleanup": {
"task": "celery.backend_cleanup",
"schedule": crontab("0", "4", "*"),
"options": {"expire_seconds": 12 * 3600},
"tenancy_options": {
"public": True,
"all_tenants": True,
"use_tenant_timezone": True,
}
}
}
)
@app.task(bind=True)
def debug_task(self):
print('Request: {0!r}'.format(self.request.tenant.schema_name))
Test that the admin views for public and tenant domains are behaving as expected, to avoid name leakage or ways to set up tasks to run on tenant schemas other than your own.
Public domain can:
Tenant domains can:
(In initial attempts, I had trouble getting the test database schemas to be created properly.)
Version 0.2.0 of django-tenants-celery-beat will generate the unexpected keyword argument 'display_GMT_offset' error when used with Django 4.1.9, and probably all versions of Django 4.
There is a solution to this error in this repo, but it was difficult to find. The purpose of this issue is to raise the visibility of the solution that can be found at the following link:
https://github.com/QuickRelease/django-tenants-celery-beat/pull/27/files
When setting tenant aware periodic tasks via the admin menu, leaving use_tenant_timezone
unchecked results in the task being made public despite it being on the tenant schema.
There is the problem with migrating this package ' django_tenants_celery_beat' causes that dependcies are different than after reupdating/reinstalling pip packages from Docker.
django.db.migrations.exceptions.InconsistentMigrationHistory: Migration django_tenants_celery_beat.0001_initial is applied before its dependency tenants.0003_auto_20210421_0627 on database 'default'.
A temporary solution is to copy the entire package so that the migrations remain unchanged.
@dave-v Can you provide an example how to connect this with admin panel? Also I am trying to inherit the model and add more fields to run that on user define schedule, any help would be appreciated.
Thanks
Hello,
This might be a very stupid question, but what does the all_tenants
attribute do when creating a beat schedule?
For example, I want to create a task that runs at 5am everyday for each tenant, but I would like the tenant to be able to change the time as they please. Would I set all_tenants
to true or false?
app.conf.beat_schedule = generate_beat_schedule(
{
"celery.backend_cleanup": {
"task": "celery.backend_cleanup",
"schedule": crontab("0", "4", "*"),
"options": {"expire_seconds": 12 * 3600},
"tenancy_options": {
"public": False,
"all_tenants": True,
"use_tenant_timezone": True,
}
},
}
)
My tenant is named Company
. When I try to add a periodic_task from the admin I get the error:
DoesNotExist at /admin/django_celery_beat/periodictask/add/
Company matching query does not exist.
Thoughts on how to fix this?
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,
}
I want to run individual, independent schedules from tenants with PeriodicTasks from django-celery-beat.
Does this result in the periodic tasks being located in a public schema?
Haya,
I'm quite new with django but I'm getting:
django.core.exceptions.ImproperlyConfigured: Requested setting LOGGING_CONFIG, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
I've managed to clear it with adding
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'app.settings')
in utils.py before
django.setup() # noqa
However, once I deploy my project to heroku it will cleare. Is there a work around to solve the error without amending package code?
Let me know if you want to see full logs
Denis
Hi!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.