简体   繁体   English

Rails - 每当 gem - 动态值

[英]Rails - Whenever gem - Dynamic values

Lets say I have a cronjob like this:假设我有一个这样的 cronjob:

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.虽然,我想让“1.day”更具动态性,例如通过管理页面中的表单更改值。

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 %>

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 .value 可以包含例如: 1.day:sunday
.additional_data contains the timestamp, example: 11:00am .additional_data 包含时间戳,例如: 11 :00am

And yes, I'm aware that I need to run --update crontab again :)是的,我知道我需要再次运行--update crontab :)
But I'll let a cronjob update itself, hehe.但我会让 cronjob 自行更新,呵呵。

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.我从未尝试过,但是您应该能够通过在任何时候加载 Rails 环境来执行此操作,以便您可以使用您的模型类。 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.只需在 schedule.rb 中使用require File.dirname(__FILE__) + "./environment" (或 Rails 3 的"./application" ),假设您的 schedule.rb 在配置目录中。

However, since all whenever does is generate lines in the crontab, any changes made to any Constant would require running whenever --update-crontab again.但是,由于所有时间都会在 crontab 中生成行,因此对任何Constant所做的任何更改都需要whenever --update-crontab再次运行whenever --update-crontab运行。

# 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别忘了随时更新

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.schedule.rb文件中,我添加了一个 cron 作业,它每天在 00:00 运行并选择今天可以运行的记录。 And after it added to queue with sidekiq using perform_at .在它使用perform_at添加到 sidekiq 队列之后。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM