whenever gem schedule.rb file: doesn't recognize RAILS_ROOT variable
Asked Answered
P

3

9

In schedule.rb file, the statement:

require "#{RAILS_ROOT}/config/environment.rb"

 every "10 10 2 * * *" do
      command "mysqldump -u #{@db_username} -p#{@db_password} --single-transaction #{@db_name} > #{@backup_Path}/#{@db_name}.sql 2> log/error_crontab.log"
 end

When i try to execute the whenever cmd from terminal, getting the following error:

 config/schedule.rb:48:in `initialize': uninitialized constant Whenever::JobList::RAILS_ROOT (NameError)
    from /usr/local/lib/ruby/gems/1.9.1/gems/whenever-0.7.0/lib/whenever/job_list.rb:19:in `instance_eval'
    from /usr/local/lib/ruby/gems/1.9.1/gems/whenever-0.7.0/lib/whenever/job_list.rb:19:in `initialize'
    from /usr/local/lib/ruby/gems/1.9.1/gems/whenever-0.7.0/lib/whenever.rb:16:in `new'
    from /usr/local/lib/ruby/gems/1.9.1/gems/whenever-0.7.0/lib/whenever.rb:16:in `cron'
    from /usr/local/lib/ruby/gems/1.9.1/gems/whenever-0.7.0/lib/whenever/command_line.rb:40:in `run'
    from /usr/local/lib/ruby/gems/1.9.1/gems/whenever-0.7.0/lib/whenever/command_line.rb:7:in `execute'
    from /usr/local/lib/ruby/gems/1.9.1/gems/whenever-0.7.0/bin/whenever:38:in `<top (required)>'
    from /usr/local/bin/whenever:19:in `load'
    from /usr/local/bin/whenever:19:in `<main>'

i am using the require statement to get the dynamic values from the form to schedule the job. Please help to solve this issue?

Note: i have seen the following stackoverflow queries: How to detect Rails environment inside whenever

Following this thread to get dynamic values, but facing problem with require statement. Rails - Whenever gem - Dynamic values

Ruby/Rails - Whenever gem - Loop cron tasks

config file in schedule.rb with Rails Whenever gem?

Prehensile answered 2/11, 2011 at 9:35 Comment(2)
Rails 3 deprecates RAILS_ROOT in favor of Rails.root. Have you tried that yet?Muldon
Rails.root also throws the same errorPrehensile
H
30

Whenever doesn't require or depend on Rails at all, so when it runs, RAILS_ROOT is not defined, however because whenever's schedule.rb is generally kept in /config/schedule.rb, we can make an assumption that it is in a rails project, and set our own RAILS_ROOT like this:

# in schedule.rb
RAILS_ROOT = File.dirname(__FILE__) + '/..'

Edit: in the case that you actually need Rails loaded, do this:

# in schedule.rb
# this will require config/environment and load your entire rails environment
require File.expand_path(File.dirname(__FILE__) + "/environment")
Hurwitz answered 2/11, 2011 at 13:43 Comment(3)
A similar approach is described in here: groups.google.com/group/whenever-gem/browse_thread/thread/…Muldon
i am getting database user id and password from form, hence to pass them to the schedule.rb, i need to load the rails environment in schedule.rb.Prehensile
@Prehensile In that case, I have updated my answer to actually require and boot Rails.Hurwitz
Q
18

The whenever developer already answered this question, check this out https://github.com/javan/whenever/issues/81

Javan Whenever no longer attempts to load your Rails environment. However, it does automatically set a path variable to the directory whenever was executed from. This should work just the same:

set :output, "#{path}/log/cron.log"
Quiff answered 24/1, 2013 at 18:12 Comment(1)
You are wellcome, I know sometimes its hard to find a solution to a problem, but when I finally find it, I like to share it to help others save time :D...Quiff
E
3

In Rails 4 try with:

require File.expand_path(File.dirname(__FILE__) + "/../config/environment")

in your schedule.rb file. This way you also have access to all your active-record models and initializers.

Equimolecular answered 14/4, 2016 at 23:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.