问题
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