config file in schedule.rb with Rails Whenever gem?

允我心安 提交于 2020-02-02 11:17:27

问题


I have a file called config.yml in my /config folder of my rails application. I also have an initializer: config/initializers/load_config.rb with the following code:

APP_CONFIG = YAML.load_file("#{RAILS_ROOT}/config/config.yml")

I am using the Whenever gem to set up a cron job, and would like to use my APP_CONFIG to call a function like so:

#inside schedule.rb
every 2.hours do
  runner "MyModel.someMethod('#{APP_CONFIG['some_value']}')"
end

but the Whenever gem doesn't seem to recognize the config file when I call

 whenever --update-crontab mysite

How can I incorporate values from my configuration in my schedule.rb file (instead of hard-coding the value)?

Thanks!


回答1:


Edit your schedule.rb file to add a require 'yaml' statement to the top. Then add the line from your initializer:

APP_CONFIG = YAML.load_file("#{RAILS_ROOT}/config/config.yml")

Altenatively, you can probably just require the load_config.rb file directly. You should be good to go then.



来源:https://stackoverflow.com/questions/2534622/config-file-in-schedule-rb-with-rails-whenever-gem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!