Ruby-on-rails 我有一个 Rails 任务:我应该使用 script/runner 还是 rake?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/591503/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:00:01  来源:igfitidea点击:

I have a Rails task: should I use script/runner or rake?

ruby-on-railsrubyrake

提问by Mike Woodhouse

For ad hocRails tasks we have a few implementation alternatives, chief among which would seem to be:

对于临时Rails 任务,我们有一些实现替代方案,其中主要的似乎是:

script/runner some_useful_thing

and:

和:

rake some:other_useful_thing

Which option should I prefer? If there's a clear favourite then when, if ever, should I consider using the other? If never, then why would you suppose it's still present in the framework without deprecation warnings?

我应该更喜欢哪个选项?如果有一个明显的最爱,那么我什么时候应该考虑使用另一个?如果从来没有,那么您为什么会认为它仍然存在于框架中而没有弃用警告?

采纳答案by Luke Francl

The difference between them is that script/runnerboots Rails whereas a Rake task doesn't unless you tell it to by making the task depend on :environment, like this:

它们之间的区别在于script/runner引导 Rails 而 Rake 任务不会,除非您通过使任务依赖于来告诉它:environment,就像这样:

task :some_useful_task => :environment do
  # do some useful task
end

Since booting Rails is expensive, it might be worth skipping if you can avoid it.

由于引导 Rails 的成本很高,如果可以避免,可能值得跳过。

Other than that, they are roughly equivalent. I use both, but lately I've used script/runnerexecuting a script separately more.

除此之外,它们大致相同。我两者都使用,但最近我script/runner更多地使用单独执行脚本。

回答by esilver

FWIW there seems to be some movement away from using script runnerin favor of rake:

FWIW 似乎有一些远离使用脚本运行器而支持 rake 的动作:

Update (4/25/2009): I recommend using rake tasks as opposed to script/runner for recurring tasks.

更新 (4/25/2009):我建议使用 rake 任务而不是脚本/运行器来处理重复性任务。

Also, as per this postyou can use rake for recurring tasks just fine:

此外,根据这篇文章,您可以使用 rake 来执行重复性任务就好了:

If I then wanted this to run nightly on my production database at midnight, I might write a cronjob that looks something like this:

0 0 * * * cd /var/www/apps/rails_app/ && /usr/local/bin/rake RAILS_ENV=production utils:send_expire_soon_emails

如果我希望它每晚在我的生产数据库午夜运行,我可能会编写一个看起来像这样的 cronjob:

0 0 * * * cd /var/www/apps/rails_app/ && /usr/local/bin/rake RAILS_ENV=production utils:send_expire_soon_emails

回答by Alfred Fazio

Passing parameters to a rake task is a pain in the butt, to say the least. You either need to resort to environment variables or a very hackish parameter system that is not intuitive and has lots of caveats.

至少可以说,将参数传递给 rake 任务是一件麻烦事。您要么需要求助于环境变量,要么需要使用不直观且有很多警告的非常hackish 的参数系统。

If your task needs to handle command line arguments gracefully then writing a script is the way to go.

如果您的任务需要优雅地处理命令行参数,那么编写脚本是最佳选择。

Luke Francl mentions script/runner booting up Rails. That's true. But if you don't want to boot up rails then just run the script as is without script/runner. So the only real difference between scripts and rake tasks are their aesthetics. Choose whatever feels right to you.

Luke Francl 提到了启动 Rails 的脚本/运行程序。确实如此。但是,如果您不想启动 rails,那么只需按原样运行脚本,无需脚本/运行程序。所以脚本和 rake 任务之间唯一真正的区别是它们的美学。选择你觉得合适的任何东西。

I use rake tasks for little tasks (one or two lines). Anything more complicated goes into the script/ directory. I'll break this rule if I think other developers will expect the code to live in one place over another.

我将 rake 任务用于小任务(一两行)。任何更复杂的东西都会进入 script/ 目录。如果我认为其他开发人员希望代码在一个地方而不是另一个地方,我会打破这个规则。

回答by Ben Walding

Corrected based on comment 2 down. Give them the karma!

根据评论 2 进行更正。给他们业力!

FWIW - Rails 3.0+ changes how you initialize the Rails system in a standalone script.

FWIW - Rails 3.0+ 更改了在独立脚本中初始化 Rails 系统的方式。

require File.dirname(__FILE__) + '/config/environment'

As mentioned above you can also do:

如上所述,您还可以执行以下操作:

rails runner script/<script name>

Or put all the code in a Rake task, but I have a lot of legacy code from Rails 2; so I didn't want to go down that path immediately.

或者将所有代码放在一个 Rake 任务中,但我有很多来自 Rails 2 的遗留代码;所以我不想立即走这条路。

Each has its advantages and disadvantages.

每个都有其优点和缺点。

回答by Orion Edwards

One thing I've done is just write normal ruby scripts and put them in the script/maintenancedirectory.

我所做的一件事就是编写普通的 ruby​​ 脚本并将它们放在script/maintenance目录中。

All you need to do to load rails and get access to all your models, etc, is put require '../../config/environment.rb'at the top of your file, then you're away.

加载 rails 和访问所有模型等所需要做的一切都放在require '../../config/environment.rb'文件的顶部,然后你就离开了。

回答by Daniel Morris

For one off commands script/runner can be fine. For anything repeated, a rake task is easier in the long-run, and has a summary if you forget what it does.

对于一次性命令脚本/运行程序可以很好。对于重复的任何事情,从长远来看,rake 任务更容易,并且如果您忘记了它的作用,则有一个摘要。

回答by ppires

In Rails 3.0+, the config/environment.rbrequires the config/application.rb, that requires the config/boot.rb.

在 Rails 3.0+ 中,config/environment.rb需要config/application.rb,需要config/boot.rb.

So, to load an app in Rails 3, you still only have to require the environment.rb

因此,要在 Rails 3 中加载应用程序,您仍然只需要 environment.rb

回答by jlc

I got the impression script/runner was primarily for periodic tasks. E.g., a cron job that runs:

我的印象脚本/运行程序主要用于定期任务。例如,一个运行的 cron 作业:

SomeClass.update_from_web('http://www.sourcefordata.gov/')