问题描述
我编写了一个非常简单的 rake 任务来尝试定位此问题的根源.
I've written a very simple rake task to try and locate the source of this problem.
namespace :foo do
task bar: :environment do
puts 'RUNNING'
end
end
在控制台执行 rake foo:bar
时,输出为:
When executed in the console rake foo:bar
the output is:
RUNNING
RUNNING
当我执行任何 rake 任务时会发生这种情况.有没有人遇到过这种情况?
This occurs when I execute any rake task. Has anyone encountered anything like this before?
编辑
上面的 rake 任务就是写在那个 .rake 文件中的全部内容.
The above rake task is all that is written in that .rake file.
这是当前使用的 Rakefile.
Here is the Rakefile currently being used.
require File.expand_path('../config/application', __FILE__)
OurApp::Application.load_tasks
这里也是运行 --trace 的输出.
Also here is the output from running a --trace.
** Invoke foo:bar (first_time)
** Invoke environment (first_time)
** Execute environment
Hostname is: ourhost
** Execute foo:bar
RUNNING
RUNNING
推荐答案
这可以在全新的应用程序中重现.如果您不将 :environment
参数传递给 rake 任务,问题就会消失.
This is reproducible in a brand new app. The problem disappears if you do not pass the :environment
parameter to the rake task.
我将问题追溯到 ~/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/rake/task.rb
在那里我们点击了 enhance()
方法两次用于此 rake 任务:
I traced the issue to ~/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/rake/task.rb
where we hit the enhance()
method twice for this rake task:
[99, 108] in /Users/inovakov/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/rake/task.rb
99: # Enhance a task with prerequisites or actions. Returns self.
100: def enhance(deps=nil, &block)
101: byebug if self.to_s.include? 'foo'
102: @prerequisites |= deps if deps
103: @actions << block if block_given?
=> 104: self
105: end
106:
107: # Name of the task, including any namespace qualifiers.
108: def name
(byebug) @actions
[#<Proc:0x007ff492701aa0@/Users/inovakov/source/test_app/lib/tasks/foo.rake:4>, #<Proc:0x007ff4920d3f70@/Users/inovakov/source/test_app/lib/tasks/foo.rake:4>]
在我们第一次和第二次点击此方法之间,我们初始化了环境 - 在本例中为 app/config/environments/development.rb
.
Between the first and the second time we hit this method we initialise the environment - in this case app/config/environments/development.rb
.
如果我们在 rake 任务中有两个输出,我们会看到它们:
If we have two outputs in the rake task we see them both:
bash-3.2$ bundle exec rake foo:bar --trace
** Invoke foo:bar (first_time)
** Invoke environment (first_time)
** Execute environment
Hostname is: localhost
** Execute foo:bar
RUNNING
STILL RUNNING
RUNNING
STILL RUNNING
(我知道这不是答案,但我还没有评论权限,这有望帮助讨论.)
(I know this is not an answer but I don't have comment privileges yet and this could hopefully help the discussion.)
这篇关于只调用一次时,Rake 任务执行两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!