简体   繁体   English

Rails 如何运行 rake 任务

[英]Rails how to run rake task

How do I run this rake file in terminal/console?如何在终端/控制台中运行这个 rake 文件?

my statistik.rake in lib/tasks我在 lib/tasks 中的 statistik.rake

desc "Importer statistikker"
namespace :reklamer do
  task :iqmedier => :environment do
    ...
  end
  task :euroads => :environment do
    ...
  end
  task :mikkelsen => :environment do
    ...
  end
  task :orville => :environment do
    ...
  end
end

You can run Rake tasks from your shell by running:您可以通过运行以下命令从 shell 运行 Rake 任务:

rake task_name

To run from from Ruby (eg, in the Rails console or another Rake task):从 Ruby 运行(例如,在 Rails 控制台或其他 Rake 任务中):

Rake::Task['task_name'].invoke

To run multiple tasks in the same namespace with a single task, create the following new task in your namespace:要使用单个任务在同一命名空间中运行多个任务,请在命名空间中创建以下新任务:

task :runall => [:iqmedier, :euroads, :mikkelsen, :orville] do
  # This will run after all those tasks have run
end
Rake::Task['reklamer:orville'].invoke

或者

Rake::Task['reklamer:orville'].invoke(args)

Sometimes Your rake tasks doesn't get loaded in console, In that case you can try the following commands有时您的 rake 任务不会在控制台中加载,在这种情况下,您可以尝试以下命令

require "rake"
YourApp::Application.load_tasks
Rake::Task["Namespace:task"].invoke

Have you tried rake reklamer:iqmedier ?您是否尝试过rake reklamer:iqmedier

My custom rake tasks are in the lib directory, not in lib/tasks.我的自定义 rake 任务在 lib 目录中,而不是在 lib/tasks 中。 Not sure if that matters.不确定这是否重要。

If you aren't sure how to run a rake task, first find out first what tasks you have and it will also list the commands to run the tasks.如果您不确定如何运行 rake 任务,请首先找出您拥有的任务,它还会列出运行任务的命令。

Run rake --tasks on the terminal.在终端上运行rake --tasks

It will list the tasks like the following:它将列出如下任务:

rake gobble:dev:prime             
rake gobble:dev:reset_number_of_kits                                    
rake gobble:dev:scrub_prod_data

You can then run your task with: rake gobble:dev:prime as listed.然后,您可以使用以下命令运行您的任务: rake gobble:dev:prime如所列。

In rails 4.2 the above methods didn't work.在 rails 4.2 中,上述方法不起作用。

  1. Go to the Terminal.前往航站楼。
  2. Change the directory to the location where your rake file is present.将目录更改为您的 rake 文件所在的位置。
  3. run rake task_name.运行 rake task_name。
  4. In the above case, run rake iqmedier - will run only iqmedir task.在上述情况下,运行 rake iqmedier - 将仅运行 iqmedir 任务。
  5. run rake euroads - will run only the euroads task.运行 rake euroads - 将只运行 euroads 任务。
  6. To Run all the tasks in that file assign the following inside the same file and run rake all要运行该文件中的所有任务,请在同一文件中分配以下内容并运行 rake all

     task :all => [:iqmedier, :euroads, :mikkelsen, :orville ] do #This will print all the tasks o/p on the screen end

As the https://stackoverflow.com/a/5641807/7262646 and https://stackoverflow.com/a/49400110/7262646 described正如https://stackoverflow.com/a/5641807/7262646https://stackoverflow.com/a/49400110/7262646所述

you have to add你必须添加

require 'rake'
Rake::Task.clear
YourAppName::Application.load_tasks

on the top of the file.在文件的顶部。

YourAppName comes from config/applicaion.rb , which is defined as a namespace, such as: YourAppName来自config/applicaion.rb YourAppName ,它被定义为一个命名空间,例如:

module YourAppName
  class Application < Rails::Application
  end
end

and then you can use然后你可以使用

Rake::Task["task_name"].invoke

to invoke your task.调用您的任务。

here is a very good tutorial on running rake tasks that i found helpful.. 这是一个很好的关于运行rake任务的教程,对我有帮助。

http://jasonseifer.com/2010/04/06/rake-tutorial http://jasonseifer.com/2010/04/06/rake-tutorial

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

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