为什么这个 rake 任务 运行 两次?

Why does this rake task run twice?

我调用了一个主 rake 任务 setup_a_new_set_of_snippets.rake,然后调用其他任务。所以主要的看起来像这样:

load './lib/tasks/fetch_and_create_snippets.rake'
load './lib/tasks/generate_diffs_for_snippets.rake'
load './lib/tasks/cleanup_snippets_with_empty_diffs.rake'

desc "Setup a new set of Snippets"
task :setup_a_new_set_of_snippets, [:repo, :path, :entry_id, :framework_id, :method_name] => :environment do |task, args|
  repo = args[:repo]
  path = args[:path]
  entry_id = args[:entry_id]
  framework_id = args[:framework_id]
  method_name = args[:method_name]
  Rake::Task["fetch_and_create_snippets"].invoke(repo,path,entry_id,framework_id,method_name)
  Rake::Task["generate_diffs_for_snippets"].invoke
  Rake::Task["cleanup_snippets_with_empty_diffs"].invoke(framework_id)
end

出于某种原因,fetch_and_create_snippets 抽成 运行 是这样的:Rake::Task["fetch_and_create_snippets"].invoke(repo,path,entry_id,framework_id,method_name) 似乎是 运行 两次。

这是任务 - fetch_and_create_snippets.rake:

desc 'Fetch and Create Snippets from Github'

task :fetch_and_create_snippets, [:repo, :path, :entry_id, :framework_id, :method_name] => :environment do |task, args|
  repo = args[:repo]
  path = args[:path]
  entry_id = args[:entry_id]
  framework_id = args[:framework_id]
  method_name = args[:method_name]
  client = Octokit.client
  commits = GetCommits.new(repo, path).all
  commits_count = commits.count
  commits.each_with_index do |commit, index|
    content = client.contents(repo, path: path, ref: commit.sha)
    if Snippet.exists?(gh_sha: content.sha)
      puts "#{index}/#{commits_count} - EXISTS - #{commit.commit.author.email} - #{content.sha}".blue
    elsif commit.author.nil?
      puts "#{index}/#{commits_count} - NO AUTHOR - #{commit.commit.author.email} - #{content.sha}".magenta
    else
      create_snippet = CreateSnippet.new(repo, path, entry_id, framework_id, commit, method_name)
      if create_snippet
        puts "#{index}/#{commits_count} - #{commit.author.login} - #{commit.sha} SUCCESSFULLY CREATED".green
      else
        puts "#{index}/#{commits_count} - #{commit.author.login} - #{commit.sha} - NOT SUCCESSFULLY CREATED - #{create_snippet.errors}".red
      end
    end
  end
end

控制台的输出如下所示:

来自 fetch_and_create_snippets 的第一个 运行 的第一批成功 运行 事件:

783/786 - dhh - ac3c8a54f83418b5cac4b093fc1bae29f59f3a1f SUCCESSFULLY CREATED
Snippet ID: 2974 SUCCESSFULLY SANITIZED
784/786 - dhh - 7022dd9c05064ef914f8836fb5e8b2d2ee3c02ed SUCCESSFULLY CREATED
Snippet ID: 2975 SUCCESSFULLY SANITIZED
785/786 - dhh - db045dbbf60b53dbe013ef25554fd013baf88134 SUCCESSFULLY CREATED

然后它重新运行s 并显示 EXISTS 输出,只有在代码段存在时才会输出。

0/786 - EXISTS - acme@test.com - c91b5d3fe3d263963eb3f9ef2ec0dc0074951064
1/786 - EXISTS - acme@test.com - 3729e22e6423348aa9282cd17b49c09793ca3a6f
2/786 - EXISTS - acme@test.com - ec9b073c185dcef26bd3293ac124fee8285de103
3/786 - EXISTS - acme@test.com - 5a973fa8019962133449c073c4e8b5cf2ce8fb36
4/786 - EXISTS - acme@test.com - fe440fccc7398d2cd0b4f04f700c62cc76ba2b0a
5/786 - EXISTS - acme@test.com - 2fbecb7d0447b70b30b71addfbc97608690b4106
6/786 - EXISTS - acme@test.com - 77d17fc9757bcbbc63267c2e408b4dbe8ab5d930
7/786 - EXISTS - acme@test.com - ef35d80523fd066ae1d836658ef840dd0da93849
8/786 - EXISTS - acme@test.com - e13fe33b857c7f608590bf80030f8c837a15faea
9/786 - EXISTS - acme@test.com - 03c8b4c97be4ffaf1f84daf657915d8953219476

而且 运行 是第一次完成的同一批次。

是什么导致了这个双重 运行?

尝试删除 load 方法。
在 Rails 项目中,lib/tasks 目录中的所有 *.rake 文件都会自动加载。因此,所有这些任务都可以在没有额外 load.

的情况下使用

在您的 Rakefile 中,您可以看到最初生成的评论,例如

Add your own tasks in files placed in lib/tasks ending in .rake, for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.

我创建了简单的 .rake 个文件。

# lib/tasks/foo.rake

desc 'foo task'
task :foo do
  puts 'call foo task'
end

# lib/tasks/bar.rake

desc 'bar task'
task :bar do
  puts 'call bar task'
  Rake::Task['foo'].invoke
end

像这样效果很好。输出是

# => call bar task
# => call foo task

但是如果我在 bar.rake

中添加 load
load './lib/tasks/foo.rake'
desc 'bar task'
task :bar do
  puts 'call bar task'
  Rake::Task['foo'].invoke
end

foo 任务将被调用两次。

# => call bar task
# => call foo task
# => call foo task