Last active
February 3, 2016 12:27
-
-
Save Djarnis/c0d0df15656810123f8a to your computer and use it in GitHub Desktop.
Celery
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import absolute_import | |
from celery import Celery | |
import os | |
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'MY_PROJECT.settings') | |
from django.conf import settings | |
app = Celery('MY_PROJECT', include=['MY_MODULE.tasks'], broker=settings.BROKER_URL) | |
app.config_from_object('django.conf:settings') | |
# Optional configuration, see the application user guide. | |
app.conf.update( | |
CELERY_ACCEPT_CONTENT=['json', 'pickle'], | |
CELERY_TASK_RESULT_EXPIRES=3600, | |
) | |
if __name__ == '__main__': | |
app.start() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# - - - - - - - - - - | |
# BROKER SETTINGS | |
# - - - - - - - - - - | |
BROKER_URL = os.environ['APP_BROKER_URL'] | |
BROKER_HEARTBEAT = 10 | |
BROKER_HEARTBEAT_CHECKRATE = 2.0 | |
# Setting BROKER_POOL_LIMIT to None disables pooling | |
# Disabling pooling causes open/close connections for every task. | |
# However, the rabbitMQ cluster being behind an Elastic Load Balancer, | |
# the pooling is not working correctly, | |
# and the connection is lost at some point. | |
# There seems no other way around it for the time being. | |
BROKER_POOL_LIMIT = None | |
BROKER_TRANSPORT_OPTIONS = {'confirm_publish': True} | |
BROKER_CONNECTION_TIMEOUT = 20 | |
BROKER_CONNECTION_RETRY = True | |
BROKER_CONNECTION_MAX_RETRIES = 100 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import absolute_import | |
from __future__ import print_function | |
import datetime | |
import os | |
from celery import Celery | |
from celery import task | |
# set the default Django settings module for the 'celery' program. | |
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'MY_PROJECT.settings') | |
from django.conf import settings | |
app = Celery( | |
'MY_PROJECT', | |
broker=settings.BROKER_URL | |
) | |
app.config_from_object('django.conf:settings') | |
@app.task | |
def rabbit_tester(str_arg): | |
"""Test rabbit""" | |
print('Running rabbit task %s ...' % str_arg) | |
return 'Rabbit all done' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment