Rails 从初始值设定项文件调用依赖于 :environment 任务的 rake 任务
我正在使用 rufus 调度程序来替换部署系统中的 cron 作业,并在部署时加载应用程序时启动这些作业。
现在我已将此 Scheduler.rb 放置在应用程序根目录的 config/initializers 目录中。
Scheduler.rb 文件的内容如下:
require 'rufus/scheduler'
require 'rubygems'
require 'rake'
load File.join(RAILS_ROOT,'lib','tasks','tempfile.rake')
temp_files_cleaning_scheduler = Rufus::Scheduler.start_new
temp_files_cleaning_scheduler.cron '*/1 * * * *' do
Rake::Task["tempfile:delete_all"].reenable
Rake::Task["tempfile:delete_all"].invoke
end
现在,当我启动应用程序服务器时,收到如下错误消息:
scheduler caught exception :
Don't know how to build task 'environment'
/home/karthik/.rvm/gems/jruby-1.5.2/gems/rake-0.8.7/lib/rake.rb:1728:in `[]'
/home/karthik/.rvm/gems/jruby-1.5.2/gems/rake-0.8.7/lib/rake.rb:605:in `invoke_prerequisites'
其中“environment”是我正在调用的任务“tempfile:delete_all”的依赖任务。这个 :environment 任务在railties/lib/tasks/misc.rake 中定义。
我不想通过硬编码该文件的路径来加载该文件。 有没有更干净的方法来解决这个问题?
谢谢。
I'm using a rufus scheduler to replace cron jobs from the deployment system and get those jobs kick-started when an application loads on deployment.
Now I have this scheduler.rb placed in the config/initializers directory from the application root directory.
The content of the scheduler.rb file is as below:
require 'rufus/scheduler'
require 'rubygems'
require 'rake'
load File.join(RAILS_ROOT,'lib','tasks','tempfile.rake')
temp_files_cleaning_scheduler = Rufus::Scheduler.start_new
temp_files_cleaning_scheduler.cron '*/1 * * * *' do
Rake::Task["tempfile:delete_all"].reenable
Rake::Task["tempfile:delete_all"].invoke
end
Now when I start the application server, I get the error message as below:
scheduler caught exception :
Don't know how to build task 'environment'
/home/karthik/.rvm/gems/jruby-1.5.2/gems/rake-0.8.7/lib/rake.rb:1728:in `[]'
/home/karthik/.rvm/gems/jruby-1.5.2/gems/rake-0.8.7/lib/rake.rb:605:in `invoke_prerequisites'
where 'environment' is a dependant task for the task "tempfile:delete_all" that I'm invoking. And this :environment task is defined in railties/lib/tasks/misc.rake.
I don't want to load this misc.file by hard-coding the path to it. Is there a cleaner way to solve this problem?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
听起来您需要 Rakefiles 中未加载的更多定义,可能是因为“lib/tasks/tempfile.rake”中没有任何
require
语句。我假设这可以从命令行运行,如果是这样,您有两个选择:
加载应用程序的主 Rakefile,其中包含所有必需的内容:
load File.join(RAILS_ROOT,'lib','tasks','tempfile.rake')
就像从控制台一样调用它:
system('rake tempfile:delete_all')
希望这有帮助!
It sounds like you need more definitions that are in Rakefiles that aren't getting loaded, probably because "lib/tasks/tempfile.rake" doesn't have any
require
statements in it.I assume this works from the command line, and if so, you have two options:
Load your app's main Rakefile, which has all teh necessary includes:
load File.join(RAILS_ROOT,'lib','tasks','tempfile.rake')
Just call it as as if from the console:
system('rake tempfile:delete_all')
Hope that helps!