不同时区的芹菜 [英] celery for different timezones

查看:39
本文介绍了不同时区的芹菜的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我使用django-celery向用户发送预定的电子邮件.如果所有用户都在相同的时区,则可以正常工作.但是,如果用户在不同的时区,则他会在不正确的时间获得该信息.

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.

例如,我安排每天使用CrontabSchedule将电子邮件发送给用户a,用户b,每天早上8点,服务器是GMT时间,用户a是GMT,用户b是GMT + 1,用户a会在上午8点收到该电子邮件,用户b将在上午9点得到它.

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?

推荐答案

当用户B的时区设置为欧洲/维也纳"时,他将在冬季为GMT + 1,在夏季为GMT + 2.每天的交货时间需要与日期结合起来才能知道何时需要在UTC中发送.

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.

解决方案可能是每日脚本,该脚本计算每个用户的交货日期时间,并以正确的日期作为ETA发送芹菜任务.(我希望send_task仍然可以那样工作)

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)

这篇关于不同时区的芹菜的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆