在控制台外运行Rails命令

时间:2022-07-04 20:47:49

With my large application, the Rails console takes a while to load up. Is there a way to single commands more easily?

使用我的大型应用程序,Rails控制台需要一段时间才能加载。有没有办法更轻松地单个命令?

I'd also like to be able to automate stuff, and echo "query" | rails console isn't a great way to do things.

我也希望能够自动化东西,并回显“查询”| rails console不是一个很好的做事方式。

Thoughts?

思考?

EDIT: What about a long-running process that I can ping queries to whenever I have need?

编辑:一个长时间运行的进程,我可以在需要时ping查询?

2 个解决方案

#1


42  

There are two main ways to run commands outside console:

在控制台外运行命令有两种主要方法:

  1. Rake task which depends on :environment
  2. 耙子任务取决于:环境
  3. rails runner (previously script/runner), eg:

    rails runner(以前的脚本/跑步者),例如:

    $ rails runner "query"

    $ rails runner“query”

Both are pretty well documented on the rails guide: http://guides.rubyonrails.org/command_line.html

两者都在rails指南中有很好的文档记录:http://guides.rubyonrails.org/command_line.html

btw: both of these methods will still take the same time as a console to fire up, but they are useful for non-interative tasks.

顺便说一下:这两种方法仍然需要与控制台同时启动,但它们对于非交互式任务非常有用。

#2


23  

Just pipe it in:

只需将其输入:

echo 'puts Article.count' | bundle exec rails c

It should now be a lot faster than when then the question was originally asked, because of Spring. It's not immediate, but still a lot faster than spinning up the whole app. Use this for the fast lane, it should run in under a second (assuming your required command is fast):

现在它应该比最初提出的问题要快得多,因为Spring。这不是直接的,但仍然比旋转整个应用程序快得多。使用它作为快速通道,它应该在一秒钟内运行(假设您的所需命令很快):

echo 'puts Article.count' | spring rails c

If you really want a single long-running process, you could easily do it by creating a controller action that simply runs whatever you POST to it, then send commands to it using curl behind an alias. The action would of course be completely insecure and should be triple-guarded against running anywhere near production, but it would be easy to setup.

如果你真的想要一个长时间运行的进程,你可以通过创建一个控制器动作来轻松地执行它,该动作只是运行你向它发送的任何内容,然后使用别名后面的curl向它发送命令。这个动作当然是完全不安全的,并且应该在生产附近的任何地方进行三重防护,但它很容易设置。

#1


42  

There are two main ways to run commands outside console:

在控制台外运行命令有两种主要方法:

  1. Rake task which depends on :environment
  2. 耙子任务取决于:环境
  3. rails runner (previously script/runner), eg:

    rails runner(以前的脚本/跑步者),例如:

    $ rails runner "query"

    $ rails runner“query”

Both are pretty well documented on the rails guide: http://guides.rubyonrails.org/command_line.html

两者都在rails指南中有很好的文档记录:http://guides.rubyonrails.org/command_line.html

btw: both of these methods will still take the same time as a console to fire up, but they are useful for non-interative tasks.

顺便说一下:这两种方法仍然需要与控制台同时启动,但它们对于非交互式任务非常有用。

#2


23  

Just pipe it in:

只需将其输入:

echo 'puts Article.count' | bundle exec rails c

It should now be a lot faster than when then the question was originally asked, because of Spring. It's not immediate, but still a lot faster than spinning up the whole app. Use this for the fast lane, it should run in under a second (assuming your required command is fast):

现在它应该比最初提出的问题要快得多,因为Spring。这不是直接的,但仍然比旋转整个应用程序快得多。使用它作为快速通道,它应该在一秒钟内运行(假设您的所需命令很快):

echo 'puts Article.count' | spring rails c

If you really want a single long-running process, you could easily do it by creating a controller action that simply runs whatever you POST to it, then send commands to it using curl behind an alias. The action would of course be completely insecure and should be triple-guarded against running anywhere near production, but it would be easy to setup.

如果你真的想要一个长时间运行的进程,你可以通过创建一个控制器动作来轻松地执行它,该动作只是运行你向它发送的任何内容,然后使用别名后面的curl向它发送命令。这个动作当然是完全不安全的,并且应该在生产附近的任何地方进行三重防护,但它很容易设置。