Rails 从初始值设定项文件调用依赖于 :environment 任务的 rake 任务

发布于 2024-10-16 08:45:40 字数 1001 浏览 7 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

脱离于你 2024-10-23 08:45:40

听起来您需要 Rakefiles 中未加载的更多定义,可能是因为“lib/tasks/tempfile.rake”中没有任何 require 语句。

我假设这可以从命令行运行,如果是这样,您有两个选择:

  1. 加载应用程序的主 Rakefile,其中包含所有必需的内容:

    load File.join(RAILS_ROOT,'lib','tasks','tempfile.rake')

  2. 就像从控制台一样调用它:

    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:

  1. Load your app's main Rakefile, which has all teh necessary includes:

    load File.join(RAILS_ROOT,'lib','tasks','tempfile.rake')

  2. Just call it as as if from the console:

    system('rake tempfile:delete_all')

Hope that helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文