celery for different timezones

雨燕双飞 提交于 2019-12-08 13:34:36

问题


Now I using django-celery to send scheduled emails to user. it works fine if all users in same timezone. But if a user in different timezone, he will get the not in right time.

For example, I scheduled an email send to user a, user b at 8am every day with CrontabSchedule, server is GMT time, user a is GMT, user b is GMT+1, user a will get that email at 8am but user b will get it at 9am.

How can I schedule tasks for different timezones with celery?


回答1:


When user B has his timezone set to "Europe/Vienna" he will be GMT+1 in winter and GMT+2 in summer. A daily delivery time needs to be combined with a date to know when in UTC it needs to be sent.

A solution might be a daily script that calculates the delivery datetime for each user and sends celery tasks with the correct dateime as ETA. (i hope the send_task still works that way)

from pytz import timezone, utc
from datetime import date, datetime
from celery.execute import send_task

def daily_delivery(delivery_time, delivery_timezone, task_name, task_args, task_kwargs):

    tz = timezone(delivery_timezone)
    today = date.today()
    local_delivery = datetime.combine(today, delivery_time)
    utc_delivery = utc.normalize(tz.localize(local_delivery).astimezone(utc))
    return send_task(task_name, task, args=task_args, kwargs=task_kwargs, eta=utc_delivery)


来源:https://stackoverflow.com/questions/20511552/celery-for-different-timezones

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!