繁体   English   中英

我如何运行所有的 rake 任务?

[英]How do I run all rake tasks?

刚刚安装了每当 gem https://github.com/javan/whenever来运行我的 rake 任务,这些任务是依赖于 nokogiri / feedzilla 的抓取任务。

例如,我的任务称为grab_bbc、grab_guardian 等

我的问题 - 当我更新我的网站时,我不断向 scheduler.rake 添加更多任务。

我应该在我的 config/schedule.rb 中写什么来让所有 rake 任务运行,不管它们叫什么?

这样的东西会起作用吗?

    every 12.hours do
        rake:task.each do |task|
            runner task
        end 
    end

我是 Cron 的新手,使用 RoR 4。

namespace :sc do
  desc 'All'
  task all: [:create_categories, :create_subcategories]

  desc 'Create categories'
  task create_categories: :environment do
    # your code
  end

  desc 'Create subcategories'
  task create_subcategories: :environment do
    # your code
  end
end

在控制台写 $ rake sc:all

为每个抓取任务编写单独的 rake 任务。 然后编写一个聚合任务来运行所有这些抓取 rake 任务。

desc "scrape nytimes"
task :scrape_nytimes do
  # scraping method
end

desc "scrape guardian"
task :scrape_guardian do
  # scraping method
end

desc "perform all scraping"
task :scrape do
  Rake::Task[:scrape_nytimes].execute 
  Rake::Task[:scrape_guardian].execute 
end

然后将 rake 任务称为

rake scrape

确保您有一个唯一的命名空间,其中包含所有任务,例如:

namespace :scrapers do

  desc "Scraper Number 1" 
  task :scrape_me do
    # Your code here
  end

  desc "Scraper Number 2"
  task :scrape_it do
    # Your code here
  end

end

然后,您可以使用该命名空间之外的任务运行该命名空间的所有任务:

task :run_all_scrapers do
  Rake.application.tasks.each do |task|
    task.invoke if task.name.starts_with?("scrapers:")
  end
end

也就是说,我很确定这不是您应该如何运行一组刮刀。 如果出于任何原因if部分应该返回 true,您可能会无意中运行诸如rake db:drop类的任务

“手动”维护schedule.rb或主任务对我来说似乎是更好的选择。

聚合任务可以是简洁的:

namespace :scrape do
  desc "scrape nytimes"
  task :nytimes do
    # scraping method
  end

  desc "scrape guardian"
  task :guardian do
    # scraping method
  end
end

desc "perform all scraping"
task scrape: ['scrape:nytimes', 'scrape:guardian']

命名空间也是一种很好的做法。

使用namespacein_namespace动态运行所有任务。

我更喜欢这种方法,因为它使事情保持干净,并且如果我们的任何命名空间任务发生变化,您就不必记住更新您的“父”任务。

请注意,这个例子是从Dmitry Shvetsov 的优秀回答中借来

namespace :scrape do
  desc "scrape nytimes"
  task :nytimes do
    # scraping method
  end

  desc "scrape guardian"
  task :guardian do
    # scraping method
  end
end

desc "perform all scraping"
task :scrape do
  Rake.application.in_namespace( :scrape ){ |namespace| namespace.tasks.each( &:invoke ) }
end

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM