简体   繁体   English

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

[英]How do I run all rake tasks?

Have just installed whenever gem https://github.com/javan/whenever to run my rake tasks, which are nokogiri / feedzilla dependent scraping tasks.刚刚安装了每当 gem https://github.com/javan/whenever来运行我的 rake 任务,这些任务是依赖于 nokogiri / feedzilla 的抓取任务。

eg my tasks are called grab_bbc, grab_guardian etc例如,我的任务称为grab_bbc、grab_guardian 等

My question - as I update my site, I keep add more tasks to scheduler.rake.我的问题 - 当我更新我的网站时,我不断向 scheduler.rake 添加更多任务。

What should I write in my config/schedule.rb to make all rake tasks run, no matter what they are called?我应该在我的 config/schedule.rb 中写什么来让所有 rake 任务运行,不管它们叫什么?

Would something like this work?这样的东西会起作用吗?

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

Am new to Cron, using RoR 4.我是 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

in console write $ rake sc:all在控制台写 $ rake sc:all

write separate rake tasks for each scraping tasks.为每个抓取任务编写单独的 rake 任务。 then write a aggregated task to run all those scraping rake tasks.然后编写一个聚合任务来运行所有这些抓取 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

then call the rake task as然后将 rake 任务称为

rake scrape

Make sure you have a unique namespace with all the tasks in it, like:确保您有一个唯一的命名空间,其中包含所有任务,例如:

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

You could then run all tasks of that namespace with a task outside of that namespace:然后,您可以使用该命名空间之外的任务运行该命名空间的所有任务:

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

That said, I'm pretty sure that this is not how you should run a set of scrapers.也就是说,我很确定这不是您应该如何运行一组刮刀。 If for any reason the if part should return true you might unintenionally run tasks like rake db:drop如果出于任何原因if部分应该返回 true,您可能会无意中运行诸如rake db:drop类的任务

Either "manually" maintaining schedule.rb or a master task seems like a better option to me. “手动”维护schedule.rb或主任务对我来说似乎是更好的选择。

The aggregated task can be concise:聚合任务可以是简洁的:

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']

Namespaces are also a good practice.命名空间也是一种很好的做法。

Use namespace and in_namespace to run all tasks dynamically.使用namespacein_namespace动态运行所有任务。

I prefer this method because it keeps things clean and precludes you from having to remember to update your "parent" task if any of our namespace tasks change.我更喜欢这种方法,因为它使事情保持干净,并且如果我们的任何命名空间任务发生变化,您就不必记住更新您的“父”任务。

Note, the example was borrowed from Dmitry Shvetsov's excellent answer .请注意,这个例子是从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