2016-08-04 16 views
0

Django1.8.2、celery3.1.23、RabbitMqをブローカーとして使用しています。私は電子メールにAmazon sesを使用しています。今、私がdjangoシェルから電子メールを送るとき、それはユーザーに受け取られていますが、私がセロリを通してそれをスケジュールするとき、それは受け取られています。ここDjangoでセロリを使用しているときにメールが届かない

は、プロジェクトファイルです:

プロジェクト/ srcに/設定/ base.py(検閲)

BROKER_URL = 'amqp://' 
CELERY_RESULT_BACKEND = 'rpc://' 
BROKER_POOL_LIMIT = 3 

CELERY_TASK_SERIALIZER = 'json' 
CELERY_RESULT_SERIALIZER = 'json' 
CELERY_ACCEPT_CONTENT = ['application/json'] 
CELERY_TIMEZONE = 'Asia/Kolkata' 
CELERY_ENABLE_UTC = True 
CELERY_SEND_TASK_ERROR_EMAILS = True 
SERVER_EMAIL = '[email protected]' 
ADMINS = [ 
     ('abc', '[email protected]') 
] 
EMAIL_BACKEND = 'django_smtp_ssl.SSLEmailBackend' 
DEFAULT_FROM_EMAIL = '[email protected]' 
EMAIL_USE_TLS = True 
EMAIL_PORT = 465 
EMAIL_TIMEOUT = 10 
EMAIL_HOST = 'email-smtp.us-east-1.amazonaws.com' 
EMAIL_HOST_USER = 'abcjdjdlasskjjdklsaj' 
EMAIL_HOST_PASSWORD = 'djkashdklahsjdhasljkdhjksahdjkashdjakhdak' 

プロジェクト/ srcに/設定/ celery_app.py

from __future__ import absolute_import 
import os 
from celery import Celery 
from django.conf import settings 
from kombu import serialization 
serialization.registry._decoders.pop("application/x-python-serialize") 

# set the default Django settings module for the 'celery' program. 
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project.settings.production') 
app = Celery('project') 

# Using a string here means the worker will not have to 
# pickle the object when using Windows. 
app.config_from_object('django.conf:settings') 
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS) 

if __name__ == '__main__': 
    app.start() 

@app.task(bind=True) 
def debug_task(self): 
    print('Request: {0!r}'.format(self.request)) 

project/src/aここで

def schedule_reminder(self): """ Schedule a celery task to send the reminder """ date_time = datetime.combine(self.date,self.time) reminder_time = arrow.get(date_time).replace(tzinfo=self.time_zone.zone) from .tasks import send_sms_reminder, send_mail_reminder # result='' # result = send_sms_reminder.apply_async((self.pk,),eta=reminder_time,serializer = 'json') # else: result = send_mail_reminder.apply_async((self.pk,),eta=reminder_time, serializer = 'json') return result.id def save(self, *args, **kwargs): """ Now we need to do is ensure Django calls our schedule_reminder method every time an Reminder object is created or updated. """ # Check if we have scheduled a celery task for this reminder before if self.task_id: #Revoke that remnder if its time has changed celery_app.control.revoke(self.task_id) # save our reminder, which populates self.pk, # which is used in schedule_reminder # Schedule a reminder task for this reminder self.task_id = self.schedule_reminder() # Save our reminder again with the task_id print "Args:%s,Kwargs:%s"%(args,kwargs) print self.task_id super(Reminder, self).save(*args, **kwargs) 

@task() 
def send_mail_reminder(reminder_id): 
    logger.info("Send Email") 

    try: 
     reminder = Reminder.objects.get(pk=reminder_id) 
    except Reminder.DoesNotExist: 
     return 
    body = "{0}".format(reminder.message) 
    try: 
     send_mail("Reminder App Notification",body,settings.DEFAULT_FROM_EMAIL,[reminder.email]) 
     logger.info("Email Successfully send") 
     return "Email Successfully send" 
    except Exception as e: 
     logger.info("There is some problem while sending email") 
     print e 
     return e 

プロジェクト/ srcの/アプリ/ models.py PP/tasks.pyはセロリログは celery.log

[tasks] 
    . RemindMeLater.settings.celery_app.debug_task 
    . Reminder.tasks.send_email_reminder 
    . Reminder.tasks.send_mail_reminder 
    . Reminder.tasks.send_sms_reminder 

[2016-08-04 23:21:15,507: INFO/MainProcess] Connected to amqp://guest:**@127.0.0.1:5672// 
[2016-08-04 23:21:15,520: INFO/MainProcess] mingle: searching for neighbors 
[2016-08-04 23:21:16,532: INFO/MainProcess] mingle: all alone 
[2016-08-04 23:21:16,546: WARNING/MainProcess] [email protected] ready. 
[2016-08-04 23:22:21,260: INFO/MainProcess] Received task: Reminder.tasks.send_mail_reminder[e45b972a-d937-4ed9-bd75-bab331b6ed47] eta:[2016-08-04 23:22:00+05:30] 
[2016-08-04 23:22:21,582: INFO/Worker-2] Reminder.tasks.send_mail_reminder[e45b972a-d937-4ed9-bd75-bab331b6ed47]: Send Email 
[2016-08-04 23:22:21,608: INFO/MainProcess] Task Reminder.tasks.send_mail_reminder[e45b972a-d937-4ed9-bd75-bab331b6ed47] succeeded in 0.0274002929982s: None 
[2016-08-04 23:22:46,629: INFO/MainProcess] Received task: Reminder.tasks.send_mail_reminder[7b14ef7b-c123-42c8-90d8-2baf4122963a] eta:[2016-08-04 23:24:00+05:30] 
[2016-08-04 23:24:00,895: INFO/Worker-3] Reminder.tasks.send_mail_reminder[7b14ef7b-c123-42c8-90d8-2baf4122963a]: Send Email 
[2016-08-04 23:24:00,920: INFO/MainProcess] Task Reminder.tasks.send_mail_reminder[7b14ef7b-c123-42c8-90d8-2baf4122963a] succeeded in 0.0263162209994s: None 

です私はDjangoシェルからセロリを使用せずに電子メールを送信します。私もそれが

mail_test.py

from datetime import datetime,time 
from Reminder.models import Reminder 
import arrow 

t = time(23,35) 
date = datetime.today().date() 
a = Reminder.objects.create(message="Hello world",date=date,time=t,email="[email protected]",completed=False) 
from Reminder.tasks import send_mail_reminder 

# b = send_mail_reminder(a.id) 
# print "b:",b 

date_time = datetime.combine(a.date,a.time) 
reminder_time = arrow.get(date_time).replace(tzinfo=a.time_zone.zone) 
c = send_mail_reminder.apply_async((a.id,),eta=reminder_time, serializer='json') 
print "c:",c 

と、端末上のコマンドの下で実行するだろう。このように当時を行うときや、セロリでタスクを追加し、正常に実行されます。

./manage.pyシェル< mail_test.py

私は予想通り、その後も、それが働いているpost_save信号を使用しています。

+1

これは間違った '' 'os.environ.setdefault( 'DJANGO_SETTINGS_MODULE'、 'project.settings.production')' ''に見える - それは単に 'settings.production' ではないでしょうか? –

+1

settings.productionはsettings.baseからインポートします - あなたのメール設定がどこにあるのか –

答えて

0

解決済みです。タスクを呼び出してtask_idで保存するためにpost_saveシグナルを使用しました。

@receiver(post_save, sender=Reminder) 
def send_email_signal(sender,instance, created, **kwargs): 
    if not created: 
     return 
    self = instance 
    date_time = datetime.combine(self.date,self.time) 
    reminder_time = arrow.get(date_time).replace(tzinfo=self.time_zone.zone) 

    from .tasks import send_sms_reminder, send_mail_reminder 
    result = send_mail_reminder.apply_async((self.id,),eta=reminder_time, serializer = 'json') 

    if result.id: 
     instance.task_id = result.id 
     instance.save() 
関連する問題