Rufus-时区调度程序变量 - ruby​​ on Rails

发布于 2024-12-02 11:51:21 字数 395 浏览 1 评论 0原文

我正在运行一个 Rails 应用程序,它使用 rufus-scheduler 在中午向所有用户发送每日电子邮件,但是,它目前在中午向应用程序时区“台北”发送电子邮件。

我在注册时捕获用户时区,但我无法将变量放入 rufus-scheduler 任务中,

scheduler.cron('0 15 * * * @time_zone') do
  Account.all.each do |account|
    CODE
    account.users.each do |user|
      DELIVER EMAIL CODE
    end
  end
end

我想我只是不确定在哪里定义 @time_zone 变量,以便 rufus 能够正确读取它。

提前致谢!

I am running a rails app that uses rufus-scheduler to send out a daily e-mail to all users at noon, however, it currently sends it out at noon for the apps timezone "Taipei".

I am capturing user timezones on signup, but I am unable to put a variable into a rufus-scheduler task

scheduler.cron('0 15 * * * @time_zone') do
  Account.all.each do |account|
    CODE
    account.users.each do |user|
      DELIVER EMAIL CODE
    end
  end
end

I guess I'm just not sure where to define the @time_zone variable so that rufus will read it properly.

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

童话 2024-12-09 11:51:21

调度程序不需要关心时区,最好以 UTC 来考虑。您想要做的是每小时运行一次任务,并仅查找当前午夜的帐户。所以你的代码看起来更像是这样的:

scheduler.cron('0 * * * *') do
  tz = THE TIMEZONE WHERE IT IS NOW MIDNIGHT
  Account.where(:timezone => tz).each do |account|
    CODE
    account.users.each do |user|
      DELIVER EMAIL CODE
    end
  end
end

The scheduler doesn't need to care about timezones, it is better off thinking in UTC. What you want to do is run your task every hour and look for only those accounts where it is currently midnight. So your code would look more like this:

scheduler.cron('0 * * * *') do
  tz = THE TIMEZONE WHERE IT IS NOW MIDNIGHT
  Account.where(:timezone => tz).each do |account|
    CODE
    account.users.each do |user|
      DELIVER EMAIL CODE
    end
  end
end
離殇 2024-12-09 11:51:21
scheduler.cron("0 15 * * * #{@time_zone}") do
  # ...
end

rufus-scheduler README 说

时区是“tzinfo”rubygem 支持的时区 (https://github.com/tzinfo/tzinfo )。

scheduler.cron("0 15 * * * #{@time_zone}") do
  # ...
end

and the rufus-scheduler README says

The timezones are the ones supported by the ‘tzinfo’ rubygem (https://github.com/tzinfo/tzinfo).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文