配置文件在schedule.rb与Rails每当gem? [英] config file in schedule.rb with Rails Whenever gem?
问题描述
我在我的rails应用程序的/ config文件夹中有一个名为config.yml的文件。
我还有一个初始化程序:config / initializers / load_config.rb与以下代码:
APP_CONFIG = YAML。 load_file(#{RAILS_ROOT} /config/config.yml)
设置cron作业,并想要使用我的APP_CONFIG来调用如下所示的函数:
#inside schedule.rb
pre>
every 2.hours do
runnerMyModel.someMethod('#{APP_CONFIG ['some_value']}')
end
但是Whenever gem似乎不能识别配置文件当我调用
每当--update-crontab mysite
在我的schedule.rb文件(而不是硬编码的值)?
谢谢!
解决方案编辑您的
schedule.rb
文件,将require'yaml'
最佳。然后添加您的初始化器中的行:APP_CONFIG = YAML.load_file(#{RAILS_ROOT} /config/config.yml )
您可能只需要
load_config.rb
文件。你应该很好去。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!
解决方案Edit your
schedule.rb
file to add arequire '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.这篇关于配置文件在schedule.rb与Rails每当gem?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!