Rails - 每当 gem - 动态值

发布于 2024-10-10 13:38:19 字数 598 浏览 5 评论 0原文

假设我有一个像这样的 cronjob:

every 1.day, :at => '4:30 am' do  
  runner "MyModel.task_to_run_at_four_thirty_in_the_morning"  
end  

尽管如此,我想让“1.day”更加动态,例如通过管理页面中的表单更改值。

然后它可能看起来像这样:

every Constant.find(2).value, :at => '4:30 am' do 

或者

@const = Constant.find(2)
every @const.span, :at => @const.time do 

任何人都可以想出如何使这项工作有效的想法吗?

显然,这样做的原因是我可以使用我网站上数据库中存储的值,就像一条消息说

<%= "The next update is in less than #{@const.time}" #or something similar %>

Lets say I have a cronjob like this:

every 1.day, :at => '4:30 am' do  
  runner "MyModel.task_to_run_at_four_thirty_in_the_morning"  
end  

Although, I would like to make '1.day' more dynamic, such as changing the value via a form in an administration page.

Then it could look like this:

every Constant.find(2).value, :at => '4:30 am' do 

or

@const = Constant.find(2)
every @const.span, :at => @const.time do 

Can anyone come up with an idea on how to make this work?

Obviously, the reason for this would be that I could use the values stored in the database on my site, like an message saying

<%= "The next update is in less than #{@const.time}" #or something similar %>

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

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

发布评论

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

评论(4

可可 2024-10-17 13:38:19

谢谢各位闲人!完全有效。这是我的解决方案:

require "#{RAILS_ROOT}/config/environment.rb"

@notification_daily = Constant.find_by_key("notification_daily_time_span")
every eval(@notification_daily.value), :at => @notification_daily.additional_data do
  runner "Notification.daily"
end

@notification_weekly = Constant.find_by_key("notification_weekly_time_span")
every eval(@notification_weekly.value), :at => @notification_weekly.additional_data do
  runner "Notification.weekly"
end

.value 可以包含例如: 1.day:sunday
.additional_data 包含时间戳,例如:11:00am

是的,我知道我需要再次运行 --update crontab :)
但我会让 cronjob 自行更新,呵呵。

Thank you idlefingers! It totally worked. Here's my solution:

require "#{RAILS_ROOT}/config/environment.rb"

@notification_daily = Constant.find_by_key("notification_daily_time_span")
every eval(@notification_daily.value), :at => @notification_daily.additional_data do
  runner "Notification.daily"
end

@notification_weekly = Constant.find_by_key("notification_weekly_time_span")
every eval(@notification_weekly.value), :at => @notification_weekly.additional_data do
  runner "Notification.weekly"
end

.value can contain for example: 1.day or :sunday
.additional_data contains the timestamp, example: 11:00am

And yes, I'm aware that I need to run --update crontab again :)
But I'll let a cronjob update itself, hehe.

ヅ她的身影、若隐若现 2024-10-17 13:38:19

我从未尝试过,但您应该能够通过随时加载 Rails 环境来做到这一点,以便您可以使用模型类。只需在您的 Schedule.rb 中使用 require File.dirname(__FILE__) + "./environment" (或 "./application" for Rails 3),假设您的日程安排。 rb 位于配置目录中。

但是,由于所有每当执行的操作都会在 crontab 中生成行,因此对任何Constant 进行的任何更改都需要再次运行whenever --update-crontab

I've never tried, but you should be able to do this by loading up the Rails environment in whenever so that you can use your model classes. Just use require File.dirname(__FILE__) + "./environment" (or "./application" for Rails 3) in your schedule.rb, assuming your schedule.rb is in the config dir.

However, since all whenever does is generate lines in the crontab, any changes made to any Constant would require running whenever --update-crontab again.

上课铃就是安魂曲 2024-10-17 13:38:19
# In rails 4
require File.expand_path('../..//config/environment.rb', __FILE__)

# This is your table by which you will get your new value 
bid_update = DynamicOfferTime.first

# Now Task As
every (bid_update.hour_value).hours do
  puts "This will repeat in every #{bid_update.hour_value} hour"
end

随时别忘记更新

# In rails 4
require File.expand_path('../..//config/environment.rb', __FILE__)

# This is your table by which you will get your new value 
bid_update = DynamicOfferTime.first

# Now Task As
every (bid_update.hour_value).hours do
  puts "This will repeat in every #{bid_update.hour_value} hour"
end

Do not forget to update whenever

小糖芽 2024-10-17 13:38:19

我有同样的任务,并决定用另一种方法来解决它。
在我的表中有很多记录,其中存储时间表的设置
(每日、每月、每周、时间)

schedule.rb 文件中,我添加了一个 cron 作业,该作业每天 00:00 运行并选择今天可以运行的记录。然后使用 perform_at 将其添加到 sidekiq 队列中。

I had the same task and decided to solve it a bit another approach.
In my table have a lot of records where storing settings for schedule
(daily, monthly, weekly, time)

In schedule.rb file I added a cron job, which was running every day at 00:00 and select the records, which can be run today. And after it added to queue with sidekiq using perform_at.

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