Skip to content

Instantly share code, notes, and snippets.

@Chitrank-Dixit
Last active September 13, 2016 13:15
Show Gist options
  • Save Chitrank-Dixit/83f46f221eb719cc13eb4c40cc59e648 to your computer and use it in GitHub Desktop.
Save Chitrank-Dixit/83f46f221eb719cc13eb4c40cc59e648 to your computer and use it in GitHub Desktop.
celery configuration for my django project
# This is the settings file
# Rabbitmq configuration
BROKER_URL = "amqp://abcd:abcd@localhost:5672/abcd"
# Celery configuration
CELERY_ACCEPT_CONTENT = ['json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TIMEZONE = 'Asia/Kolkata'
CELERY_RESULT_BACKEND = 'djcelery.backends.database:DatabaseBackend'
CELERYBEAT_SCHEDULER = 'djcelery.schedulers.DatabaseScheduler'
CELERYBEAT_SCHEDULE = {
# Executes every Monday morning at 7:30 A.M
'update-app-data': {
'task': 'myapp.tasks.fetch_data_task',
'schedule': timedelta(days=1),
},
'update-app-categories': {
'task': 'myapp.tasks.fetch_categories_task',
'schedule': timedelta(days=7),
},
'update-app-permissions': {
'task': 'myapp.tasks.fetch_permissions_task',
'schedule': timedelta(days=7),
},
'update-app-reviews': {
'task': 'myapp.tasks.fetch_reviews_task',
'schedule': timedelta(days=1),
},
}
from __future__ import absolute_import
import os
from celery import Celery
from django.conf import settings
# Indicate Celery to use the default Django settings module
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')
app = Celery('myapp')
app.config_from_object('django.conf:settings')
# This line will tell Celery to autodiscover all your tasks.py that are in
# playstore folders
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
app_keywords = Celery('keywords')
app_keywords.config_from_object('django.conf:settings')
# This line will tell Celery to autodiscover all your tasks.py that are in
# keywords folders
app_keywords.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
app1 = Celery('myapp1')
app1.config_from_object('django.conf:settings')
# This line will tell Celery to autodiscover all your tasks.py that are in
# your app folders
app1.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
@task(bind=True)
def fetch_data_task(self, data):
logger.info("Start task")
import pdb;pdb.set_trace()
# post the data to view
headers, cookies = utils.get_csrf_token()
requests.post(settings.SITE_VARIABLES['site_url'] + "/site/general_data/",
data=json.dumps(data), headers=headers, cookies=cookies
)
if data['reviews']:
reviews_data = {'app_id': data['app_data'][
'app_id'], 'reviews': data['reviews'][0]}
requests.post(settings.SITE_VARIABLES['site_url'] + "/site/blog/reviews/",
data=json.dumps(reviews_data), headers=headers, cookies=cookies
)
logger.info("Task fetch data finished")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment