如何运行所有rake任务?

Jon*_*n_W 1 ruby cron rake ruby-on-rails nokogiri

刚刚安装了宝石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
Run Code Online (Sandbox Code Playgroud)

使用RoR 4是Cron的新手.

小智 7

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
Run Code Online (Sandbox Code Playgroud)

在控制台中写$ rake sc:all


def*_*ite 5

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

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
Run Code Online (Sandbox Code Playgroud)

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

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

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

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


kha*_*zan 5

为每个抓取任务编写单独的 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
Run Code Online (Sandbox Code Playgroud)

然后将 rake 任务称为

rake scrape
Run Code Online (Sandbox Code Playgroud)