daily pastebin goal
25%
SHARE
TWEET

Untitled

a guest Feb 22nd, 2019 123 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # 1. First install celery
  2. # pip install celery
  3.  
  4. # --------------------------
  5.  
  6. # Inside main project directory, create celery.py file
  7.  
  8. # celery.py
  9. import os
  10. from celery import Celery
  11. from django.conf import settings
  12.  
  13. # set the default Django settings module for the 'celery' program.
  14. os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'ProjectName.settings')
  15. app = Celery('ProjectName')
  16.  
  17. # Using a string here means the worker will not have to
  18. # pickle the object when using Windows.
  19. app.config_from_object('django.conf:settings')
  20. app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
  21.  
  22. # --------------------------
  23. # in settings.py
  24.  
  25. BROKER_URL = 'redis://127.0.0.1:6379'
  26. CELERY_RESULT_BACKEND = 'redis://127.0.0.1:6379'
  27. CELERY_ACCEPT_CONTENT = ['application/json']
  28. CELERY_TASK_SERIALIZER = 'json'
  29. CELERY_RESULT_SERIALIZER = 'json'
  30. CELERY_ENABLE_UTC = False
  31. CELERY_TIMEZONE = 'MST'
  32.  
  33. # --------------------------
  34. # create python package called tasks and store all your long running tasks as module here
  35.  
  36. # email_sender_tasks.py
  37. from celery import shared_task
  38.  
  39. @shared_task
  40. def send_notification_email(data, email):
  41.     # your logic here
  42.     try:
  43.         msg_html = render_to_string('templates/email_template.html',{})
  44.         send_mail('Welcome!', msg_html, email,
  45.                   [data['email']], html_message=msg_html)
  46.     except Exception as error:
  47.         return error
  48.  
  49. # ------------------
  50. # Now call the task from your view
  51.  
  52. from email_sender_tasks import send_notification_email
  53.  
  54. def register(request):
  55.     # your logic here
  56.     if register_status == True:
  57.         send_notification_email.delay({"user_info": request.user}, email='insight@gmail.com')
  58.  
  59. # -------------------
  60. # Start celery server in your local machine
  61. celery -A ProjectName worker -l info
  62.  
  63. # *******************************************
  64. # in the EC2 instance, you need to configure the EC2 for celery and django
  65. # the supervisor configuration: celery_supervisor.conf
  66. [program:project_celery_conf]
  67. command = /var/www/start_celery.sh           ; Command to start app
  68. user = www-data                                            ; User to run as
  69. stdout_logfile = /var/www/logs/celery.log     ; Where to write log messages
  70. redirect_stderr = true                                     ; Save stderr in the same log
  71. environment=LANG=en_US.UTF-8,LC_ALL=en_US.UTF-8            ; Set UTF-8 as default encoding
  72.  
  73. # start_celery.sh
  74.  
  75. #!/usr/bin/env bash
  76.  
  77. NAME="project_celery_conf"                          # name of the application
  78. PROJECT_DIR=/var/www/proj-name               # project directory
  79. APP_DIR=/var/www/proj-name                           # can be project dir
  80. APP_MODULE=ProjectName                         # application module name
  81.  
  82. echo "Starting $NAME as `whoami`"
  83.  
  84.  
  85. # Activate the virtual environment
  86. source $PROJECT_DIR/venvpp/bin/activate
  87. export PYTHONPATH=$APP_DIR:$PYTHONPATH
  88.  
  89. cd $APP_DIR
  90.  
  91. # Start Celery
  92. exec celery -A ${APP_MODULE} worker -l info
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top