セロリと、それを django と統合する方法を確認できます。完了したら、タスクのスケジューリングは簡単です。まず、次のように settings.py に gmail の構成を追加します。
EMAIL_BACKEND = 'django_smtp_ssl.SSLEmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'your_email'
EMAIL_HOST_PASSWORD = 'your password'
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
EMAIL_PORT = 465
次に、tasks.py で、次のようにメールをスケジュールする関数を追加できます。
from django.template.loader import render_to_string
from django.core.mail import EmailMessage
@periodic_task(
run_every=(crontab(hour=3, minute=34)), #runs exactly at 3:34am every day
name="Dispatch_scheduled_mail",
reject_on_worker_lost=True,
ignore_result=True)
def schedule_mail():
message = render_to_string('app/schedule_mail.html')
mail_subject = 'Scheduled Email'
to_email = getmail
email = EmailMessage(mail_subject, message, to=[to_email])
email.send()
最後に、メールテンプレート「schedule_mail.html」
{% autoescape off %}
Hello ,
This is a test email
if you are seeing this, your email got delivered!
Regards,
Coding Team.
{% endautoescape %}
そして、ビートとしてセロリ サービスを実行するコマンド:
celery -A yourapp beat --loglevel=info
「yourapp」をアプリの名前に置き換えます。乾杯!!