在schedule.rb文件中,语句为:
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
当我尝试从终端执行每个cmd时,得到以下错误:
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>'
我正在使用require语句从窗体获取动态值来计划作业.请帮忙解决这个问题?
注意:我已经看到以下stackoverflow查询:
How to detect Rails environment inside whenever
以下这个线程得到动态值,但面临的问题与require语句.
Rails – Whenever gem – Dynamic values
Ruby/Rails – Whenever gem – Loop cron tasks
config file in schedule.rb with Rails Whenever gem?
谢谢,
Sudhir C.N.
解决方法
每当不需要或依赖于Rails,所以运行时,RAILS_ROOT没有定义,但是由于schedule.rb通常保存在/config/schedule.rb中,所以我们可以假设它是在一个rails项目,并设置我们自己的RAILS_ROOT像这样:
# in schedule.rb RAILS_ROOT = File.dirname(__FILE__) + '/..'
编辑:在您实际需要加载Rails的情况下,请执行以下操作:
# in schedule.rb # this will require config/environment and load your entire rails environment require File.expand_path(File.dirname(__FILE__) + "/environment")