应用服务器+邮件服务器上的DJANGO Celery

时间:2014-10-17 19:28:00

标签: python django celery django-celery celery-task

我正在尝试使用单独的邮件服务器在应用程序服务器上配置DJANGO + Celery。

两台服务器都有相同的文件:

APP_NAME / settings.py

BROKER_URL = "sqs://%s:%s@" % (AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY)
CELERY_TIMEZONE = 'America/New_York'
CELERY_ACCEPT_CONTENT = ['pickle','json', 'msgpack', 'yaml']
from celery.schedules import crontab
from datetime import timedelta
CELERY_ROUTES = {
                'mail.tasks.send_reminder_email_end_user': {'queue': 'scheduler'},
                'mail.tasks.send_all_daily_new_user_email': {'queue': 'scheduler'},
                'mail.tasks.send_notification_email': {'queue': 'scheduler'},
                'mail.tasks.send_new_post_daily_email': {'queue': 'scheduler'},
                'insightStat.tasks.save_stats_history_daily': {'queue': 'scheduler'},
                }

CELERY_DISABLE_RATE_LIMITS = True
CELERYBEAT_SCHEDULE = {
    'send_reminder_email_daily': {
        'task': 'mail.tasks.send_reminder_email_end_user',
        'schedule': crontab(minute= 0,hour=7)
    },
    'update_stats_history_daily': {
        'task': 'insightStat.tasks.save_stats_history_daily',
        'schedule': crontab(minute= 0,hour=1)
    },
    'send_notification_email_weekly': {
        'task': 'mail.tasks.send_notification_email',
        'schedule': crontab(minute= 0,hour=8)
    },
    'send_digest_email_daily': {
        'task': 'mail.tasks.send_new_post_daily_email',
        'schedule': crontab(minute= 0,hour=8)
    },
    'send_daily_new_users': {
    'task': 'mail.tasks.send_all_daily_new_user_email',
    'schedule': crontab(minute= 0,hour=7)
    },
    'send_digest_email_weekly': {
        'task': 'mail.tasks.send_weekly_digest',
        'schedule': crontab(minute= 0,hour=8,day_of_week='Mon')
    },


}

mail / tasks.py文件:

@shared_task(queue='scheduler')
def send_all_daily_new_user_email():
    apps = Application.objects.all()
    for app in apps:
        daily_new_user_email.delay(app)


@shared_task(queue='daily_user')
def daily_new_user_email(app):
    print "sending daily_new_user_email"
    if not app.digest_email:
        print "app digest is off, return"
        return
    if not app.owner.all():
        return
    yesterday = date.today() - timedelta(days=1)
    users= app.appuser.filter(created_at__gt=yesterday)
    users_count = users.count()
    owner = app.owner.all()[0]
    if users_count == 0:
        print "no new users for %s " % owner.email
        return
    recent_user = users[0]
    subject  = "%s new subscribers" % str(users_count)
    content_html = render_to_string('daily_new_users.html',locals())
    email = owner.email
    print sendEmail(NOTIFICATION_EMAIL,email,subject,content_html,content_html,'daily_new_users')

app_name / celery_task.py文件包含:

app = Celery('app_name', backend='', broker='"sqs://%s:%s@" % (AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY)',
            include=['mail.tasks'])

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

# 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)

其中:scheduler是Que on application服务器,daily_user是Que on mail服务器。我正在使用主管来启动芹菜工人

这是多次向用户发送邮件。我在这里做错了什么?

此外,有人可以简要介绍一下@shared_task吗?

0 个答案:

没有答案