我有一个主要的佣金任务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运行如下: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
然后重新运行它并显示EXISTS
输出,只有在Snippet存在后才会输出。
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
这是第一次在同一批次中运行。
导致这种双重运行的原因是什么?
答案 0 :(得分:10)
尝试删除load
方法
在Rails项目中,*.rake
目录中的所有lib/tasks
文件都会自动加载。因此,所有这些任务都可以在没有额外load
的情况下使用。
在Rakefile
中,您可以看到最初生成的评论,例如
将自己的任务添加到以.rake结尾的lib / tasks中的文件中, 例如lib / tasks / capistrano.rake,它们将自动提供给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
但如果我在load
bar.rake
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