def test_task_get_tenant_for_schema_should_cache_results_global_setting( transactional_db): # Celery is not able to pick up settings without this app = CeleryApp('testapp') app.config_from_object('django.conf:settings', namespace="CELERY") # TASK_TENANT_CACHE_SECONDS global setting set to 10s class DummyTask(TenantTask): def run(self, *args, **kwargs): pass task = DummyTask() fresh_tenant = create_client(name="test_global", schema_name="test_global", domain_url="test_global.test.com") cached_tenant = task.get_tenant_for_schema("test_global") # Check for equality, but the objects should be different. The one from cache was fetched separately. assert cached_tenant == fresh_tenant assert cached_tenant is not fresh_tenant cache_hit_tenant = task.get_tenant_for_schema("test_global") # A cache hit. The same instance should be returned. assert cache_hit_tenant == cached_tenant assert cache_hit_tenant is cached_tenant with freeze_time(datetime.utcnow() + 2 * timedelta( seconds=int(settings.CELERY_TASK_TENANT_CACHE_SECONDS))): cache_miss_tenant = task.get_tenant_for_schema("test_global") # A cache miss. Equality is required, but they are not they same objects. assert cache_miss_tenant == cached_tenant assert cache_miss_tenant is not cached_tenant
import os os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'test_app.settings') from django.conf import settings from tenant_schemas_celery.app import CeleryApp app = CeleryApp() app.config_from_object('django.conf:settings') app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
from __future__ import absolute_import import json import os import requests # set the default Django settings module for the 'celery' program. os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'EquiTrack.settings.production') from django.conf import settings from tenant_schemas_celery.app import CeleryApp app = CeleryApp('EquiTrack') # Using a string here means the worker will not have to # pickle the object when using Windows. app.config_from_object('django.conf:settings') app.autodiscover_tasks(lambda: settings.INSTALLED_APPS) @app.task def send_to_slack(message): if settings.SLACK_URL: requests.post( settings.SLACK_URL, data=json.dumps({'text': message}) )
from __future__ import absolute_import import json import os from django.conf import settings import requests from tenant_schemas_celery.app import CeleryApp # set the default Django settings module for the 'celery' program. os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'EquiTrack.settings.production') app = CeleryApp('EquiTrack') # Using a string here means the worker will not have to # pickle the object when using Windows. app.config_from_object('django.conf:settings', namespace='CELERY') app.autodiscover_tasks(lambda: settings.INSTALLED_APPS) @app.task def send_to_slack(message): if settings.SLACK_URL: requests.post(settings.SLACK_URL, data=json.dumps({'text': message}))
from __future__ import absolute_import, unicode_literals import os from celery import Celery 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", "daio.local_settings") # app = Celery('crypto_daio') app = TenantAwareCeleryApp("crypto_daio") # Using a string here means the worker doesn't have to serialize # the configuration object to child processes. # - 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()
from __future__ import absolute_import import os from tenant_schemas_celery.app import CeleryApp from django.conf import settings # Lets the celery command line program know where project settings are. os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'predictable_success.settings.local') # Creates the instance of the Celery app. app = CeleryApp('predictable_success',include=['org.tasks', 'leadership_styles.tasks'],) app.config_from_object('django.conf:settings') # Set up autodiscovery of tasks in the INSTALLED_APPS. app.autodiscover_tasks(lambda: settings.INSTALLED_APPS) if __name__ == '__main__': app.start()