I'm trying to make Django not to wait until an email is sent. I've decided to use Celery to do that. Unfortunately I can't figure out how to make it work asynchronously.
I've created a file tasks.py
:
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from .notifications import CustomerNotifications
from celery import shared_task
@shared_task
def prereservation_has_been_confirmed(reservation):
CustomerNotifications.prereservation_has_been_confirmed(reservation)
The CustomerNotifications.prereservation_has_been_confirmed(reservation)
method sends an email to customer.
Email sending works but it still waits until the email is sent. (the view is called by AJAX
).
Do you know what to do?
EDIT
This is how the function is being called:
@csrf_exempt
def reservation_confirm(request):
if request.method == 'POST':
reservation_id = request.POST.get('reservation_id', False)
reservation = get_object_or_404(dolava_models.Reservation, id=reservation_id)
reservation.confirmed = True
reservation.save()
tasks.prereservation_has_been_confirmed(reservation)
return JsonResponse({})
raise Http404(u'...')
I tried tasks.prereservation_has_been_confirmed.delay(reservation)
but it returns that connection has been refused.
SETTINGS.PY - added BROKER_URL = 'django://'
, 'kombu.transport.django'
and migrated.
# -*- coding: utf-8 -*-
"""
Django settings for dolava project.
Generated by 'django-admin startproject' using Django 1.9.7.
For more information on this file, see
https://docs.djangoproject.com/en/1.9/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.9/ref/settings/
"""
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.9/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'secret_key'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'admin_interface',
'flat',
'colorfield',
'django.contrib.admin',
'django.contrib.auth',
# 'djcelery_email',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'dolava_app',
# 'constance',
# 'constance.backends.database',
'solo',
'django_extensions',
'django_tables2',
'django_countries',
'kombu.transport.django'
]
MIDDLEWARE_CLASSES = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'dolava.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'django.core.context_processors.media',
],
},
},
]
WSGI_APPLICATION = 'dolava.wsgi.application'
BROKER_URL = 'django://'
# Database
# https://docs.djangoproject.com/en/1.9/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Password validation
# https://docs.djangoproject.com/en/1.9/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/1.9/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.9/howto/static-files/
STATIC_URL = '/static/'
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static')
# CELERY_ALWAYS_EAGER = True
# SMTP SETTINGS
# EMAIL_BACKEND = 'djcelery_email.backends.CeleryEmailBackend'
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
# EMAIL_BACKEND = "mailer.backend.DbBackend"
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = '[email protected]'
EMAIL_HOST_PASSWORD = 'pass'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
[Following the comments] your task was correctly sent to the broker (to the database in your case) you can probably check that your pending task is there.
you don't see anything in the console because you must launch a celery worker that will read all the pending tasks on the broker and will execute them. have you started the worker process? there you'll see the tasks that are being called and the results or traceback if something fails