config file in schedule.rb with Rails Whenever gem?
Asked Answered
C

1

2

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!

Cao answered 28/3, 2010 at 21:24 Comment(0)
N
3

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.

Nonillion answered 29/3, 2010 at 9:28 Comment(2)
aha! so the initializers don't get loaded up when load_config.rb does? thanks! i'll give it a goCao
worked like a charm! don't need the require 'yaml' when I require load_config.rb. thanks, john, you're always so helpful!Cao

© 2022 - 2024 — McMap. All rights reserved.