Rails - Whenever gem - Dynamic values
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.
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.
# 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