gpt4 book ai didi

ruby-on-rails - Rake 任务仅调用一次时执行两次

转载 作者:数据小太阳 更新时间:2023-10-29 07:12:39 24 4
gpt4 key购买 nike

我写了一个非常简单的 rake 任务来尝试找到这个问题的根源。

namespace :foo do
task bar: :environment do
puts 'RUNNING'
end
end

当在控制台中执行 rake foo:bar 时,输出为:

RUNNING
RUNNING

当我执行任何 rake 任务时会发生这种情况。有没有人遇到过这样的事情?

编辑

上面的 rake 任务就是写在那个 .rake 文件中的所有内容。

这是当前正在使用的 Rakefile。

require File.expand_path('../config/application', __FILE__)

OurApp::Application.load_tasks

这里还有运行 --trace 的输出。

** Invoke foo:bar (first_time)
** Invoke environment (first_time)
** Execute environment
Hostname is: ourhost
** Execute foo:bar
RUNNING
RUNNING

最佳答案

这可以在全新的应用程序中重现。如果您不将 :environment 参数传递给 rake 任务,问题就会消失。

我将问题追溯到 ~/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/rake/task.rb 我们点击 enhance( ) 这个 rake 任务的方法两次:

[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

如果我们在 rake 任务中有两个输出,我们会同时看到它们:

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

(我知道这不是答案,但我还没有评论权限,希望这有助于讨论。)

关于ruby-on-rails - Rake 任务仅调用一次时执行两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35875661/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com