简体   繁体   English

如何通过rails中的命令行运行rake任务

[英]How to run a rake task by command line in rails

I have defined a rake task as follows in a file called file_locker_task.rake 我在一个名为file_locker_task.rake的文件中定义了一个rake任务,如下所示

namespace :myspace do
   task :process => :environment do
      FileLocker.lock_files   
   end
end

How do I execute this rake task from the command line? 如何从命令行执行此rake任务? I tried: 我试过了:

rake myspace:process and rake process but both are throwing an error like this: rake myspace:processrake process但两者都抛出这样的错误:

rake aborted!
Don't know how to build task 'process'
  1. Run rake -T -A from your Rails home directory to see all the tasks that rake knows about. 从Rails主目录运行rake -T -A以查看rake知道的所有任务。 Yours must be in that list for rake to run it. 你的必须在rake的列表中运行它。
  2. By default, in a Rails app, rake looks in the lib/tasks directory and its subdirectories for your .rake files. 默认情况下,在Rails应用程序中,rake会在lib/tasks目录及其.rake文件的子目录中.rake Check that. 检查一下。 (I suspect this is the problem.) (我怀疑这是问题所在。)

According to docs 根据文件

Any ruby file (including other rakefiles) can be included with a standard Ruby require command. 任何ruby文件(包括其他rakefiles)都可以包含在标准的Ruby require命令中。

- -

Additional rake files (with the file extension “.rake”) may be placed in rakelib directory located at the top level of a project (ie the same directory that contains the main Rakefile). 其他rake文件(文件扩展名为“.rake”)可以放在位于项目顶层的rakelib目录中(即包含主Rakefile的同一目录)。 Also, rails projects may include additional rake files in the lib/tasks directory. 此外,rails项目可能包含lib / tasks目录中的其他rake文件。

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

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