Skip to content

Instantly share code, notes, and snippets.

@lorne-luo
Created November 17, 2017 03:49
Show Gist options
  • Save lorne-luo/3737939bcc2ea55f8799b6516eb2b02f to your computer and use it in GitHub Desktop.
Save lorne-luo/3737939bcc2ea55f8799b6516eb2b02f to your computer and use it in GitHub Desktop.
Celery 4.1 Intergration with Django 1.11

Celery

Monitor celery by using celery -A freightquotes events

Start celery with beats celery -A freightquotes worker -B -E -l INFO --autoscale=2,1

Note:- 
    -B  beats - to execute schedule tasks
    -l  log-level (INFO/DEBUG)
    -E  --task-events

All crontabs should be added in settings/base.py and all celery tasks will contain @task in tasks.py file

INSTALLED_APPS = [
'django_celery_results',
]
# ----------------------------------------- CELERY -----------------------------------------------
BROKER_URL = 'redis://127.0.0.1:6379'
BROKER_TRANSPORT = 'redis'
BROKER_TRANSPORT_OPTIONS = {'visibility_timeout': 604800}
CELERY_BROKER_URL = 'redis://127.0.0.1:6379'
CELERY_BROKER_TRANSPORT = 'redis'
CELERY_BROKER_TRANSPORT_OPTIONS = {'visibility_timeout': 604800}
CELERY_RESULT_BACKEND = BROKER_URL
CELERY_TASK_RESULT_EXPIRES = datetime.timedelta(days=1) # Take note of the CleanUp task in middleware/tasks.py
CELERY_MAX_CACHED_RESULTS = 1000
CELERYBEAT_SCHEDULER = "djcelery.schedulers.DatabaseScheduler"
CELERY_TRACK_STARTED = True
CELERY_SEND_EVENTS = True
CELERY_ACCEPT_CONTENT = ['pickle', 'json', 'msgpack', 'yaml']
REDIS_CONNECT_RETRY = True
REDIS_DB = 0
BROKER_POOL_LIMIT = 2
CELERYD_CONCURRENCY = 1
CELERYD_TASK_TIME_LIMIT = 600
CELERY_BEAT_SCHEDULE = {
'task-number-one': {
'task': 'messaging.tasks.task_number_one',
'schedule': crontab(minute='*/1')
},
'task-number-two': {
'task': 'messaging.tasks.task_number_two',
'schedule': crontab(minute='*/1')
}
}
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'freightquotes.settings.base')
app = Celery('freightquotes')
# 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()
@app.task(bind=True)
def debug_task(self):
print('Request: {0!r}'.format(self.request))
Django==1.11.7
celery==4.1.0
django-celery-results==1.0.1
kombu==4.1.0
amqp==2.2.2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment