I would like my development environment for Rails to automatically start redis and resque (and potentially in other projects, mongod, mysql-server etc.) for me, in the following cases:
我希望我的Rails开发环境能够自动启动redis和resque(并可能在其他项目中,mongod, mysql-server等),在以下情况下:
- When starting up the development server
rails server
. - 当启动开发服务器rails服务器时。
Additionally, it would be nice if the following cases detect already running services, and, if not running start them up too:
另外,如果下面的案例检测到已经运行的服务,并且,如果不运行,也会很好:
- Rake rspec, rspec /spec, when running tests.
- 当运行测试时,耙子rspec, rspec /spec。
- When starting up a
rails console
. - 当启动一个rails控制台时。
When shutting down the rails server
, the started child-services should be shut down too.
当关闭rails服务器时,启动的子服务也应该关闭。
What is the correct place for such additional startup scripts?
这些额外的启动脚本的正确位置是什么?
And how to avoid them being started in production too (where I run everything trough /etc/init.d services)?
以及如何避免它们在生产中启动(我在/etc/ initit中运行所有内容)。d服务)?
2 个解决方案
#1
2
A lot of these built-in tasks are available as rake tasks already.
许多内置的任务已经可以作为rake任务可用了。
You can create a master rake task that does it all.
您可以创建一个主rake任务来完成这一切。
For example, with resque, you get "rake resque:start" "rake resque:scheduler:start", etc.
例如,使用resque,您将获得“rake resque:start”、“rake resque:scheduler:start”等。
You can create a generic "start" task that depends on the rest. Similarly, a "stop" task would shut everything down.
您可以创建一个通用的“开始”任务,该任务依赖于其他任务。类似地,“停止”任务会关闭一切。
So you would do:
所以你会做的事:
rake start # starts all associated processes
rake stop # stops them all
This is also very use to use from Capistrano, when you end up deploying your code somewhere else. Rake tasks are very easy to call from Capistrano.
当您最终将代码部署到其他地方时,Capistrano也非常使用这种方法。Rake任务很容易从Capistrano调用。
#2
1
I think it's really better to do that in some external script. Do it in your rails server command can be really annoying to anyone to try your code.
我认为在一些外部脚本中这样做更好。在rails服务器命令中执行它可能会让任何人都讨厌尝试您的代码。
By example, in one year, a nez developper come to your project. He can be desoriented if your rails server commande launch a such of other application in background.
举例来说,一年之后,一个nez开发人员来到您的项目。如果您的rails服务器命令在后台启动这样的其他应用程序,那么它可以被反定向。
In same idea, if you do that you need maintain your code in your rails env. Can be a little tricky. Maintain an independant script can be more usefull.
同样,如果您这样做,您需要在rails env中维护代码。可能有点棘手。维护独立脚本可能更有用。
You can add your script in script directory. That be a good pratice. But not when you launch a command with a manual who do not that.
您可以在脚本目录中添加脚本。这是一个很好的实践。但当你用手册发出命令时,你就不会这么做了。
#1
2
A lot of these built-in tasks are available as rake tasks already.
许多内置的任务已经可以作为rake任务可用了。
You can create a master rake task that does it all.
您可以创建一个主rake任务来完成这一切。
For example, with resque, you get "rake resque:start" "rake resque:scheduler:start", etc.
例如,使用resque,您将获得“rake resque:start”、“rake resque:scheduler:start”等。
You can create a generic "start" task that depends on the rest. Similarly, a "stop" task would shut everything down.
您可以创建一个通用的“开始”任务,该任务依赖于其他任务。类似地,“停止”任务会关闭一切。
So you would do:
所以你会做的事:
rake start # starts all associated processes
rake stop # stops them all
This is also very use to use from Capistrano, when you end up deploying your code somewhere else. Rake tasks are very easy to call from Capistrano.
当您最终将代码部署到其他地方时,Capistrano也非常使用这种方法。Rake任务很容易从Capistrano调用。
#2
1
I think it's really better to do that in some external script. Do it in your rails server command can be really annoying to anyone to try your code.
我认为在一些外部脚本中这样做更好。在rails服务器命令中执行它可能会让任何人都讨厌尝试您的代码。
By example, in one year, a nez developper come to your project. He can be desoriented if your rails server commande launch a such of other application in background.
举例来说,一年之后,一个nez开发人员来到您的项目。如果您的rails服务器命令在后台启动这样的其他应用程序,那么它可以被反定向。
In same idea, if you do that you need maintain your code in your rails env. Can be a little tricky. Maintain an independant script can be more usefull.
同样,如果您这样做,您需要在rails env中维护代码。可能有点棘手。维护独立脚本可能更有用。
You can add your script in script directory. That be a good pratice. But not when you launch a command with a manual who do not that.
您可以在脚本目录中添加脚本。这是一个很好的实践。但当你用手册发出命令时,你就不会这么做了。