将自定义信息传递给 mongrel_rails start
我真正不明白的一件事是如何将自定义启动选项传递给混合实例。
我发现一种常见的方法是使用环境变量,但在我的环境中这是行不通的,因为我的 Rails 应用程序为许多不同的客户端提供服务。许多代码在客户端之间共享,但也存在许多差异,我通过子类化控制器和视图来重载或扩展现有功能或引入新功能来实现。为了使这一切正常工作,我只需将客户端特定模块的路径添加到模块加载路径($:)。
为了为特定客户端启动应用程序,我现在可以使用环境变量,例如 TARGET=AMAZONE。不幸的是,在某些系统上我运行多个混合集群,每个集群服务于不同的客户端。其中一些系统在 Windows 下运行,为了启动 mongrel,我安装了 mongrel_services。显然,这使得我的环境变量不合适。
事实证明,将这些额外的数据传递给应用程序是一个真正的挑战。首先, mongrel_rails service_install 将拒绝任何未记录的[自定义]命令行参数。我不太担心,因为使用安装程序安装服务很简单。
但是,即使我设法安装 mongrel_services,以便在运行时将自定义命令行选项 --target 传递给 mongrel_rails start,我也会收到错误,因为 mongrel_rails 无法识别该开关。
以下是我查看的内容:
传递额外参数:
mongrel_rails start --target XYZ ...
使用配置文件并添加 target:XYZ,然后执行:
mongrel_rails start -C x:\myapp\myconfig.yml
修改文件:
Ruby\lib\ruby\gems\1.8\gems\mongrel-1.1.5-x86-mswin32-60\lib\mongrel\command.rb
也许我可以使用 --script 选项,但我在其上找到的所有文档都是针对 Unix 的
1 和 2,根本不起作用。我玩过 4 个,但从来没有让它做任何事情。所以我别无选择,只能选择3。虽然它相对简单,但我讨厌更改ruby库代码。
特别令人失望的是2不起作用。我的意思是在配置文件中添加其他[自定义]选项有什么不合理的?实际上我认为这是 Rails 中缺少的一个基本部分。无论如何,应用程序应该能够注册和访问它期望的命令行参数。
如果有人知道如何使用当前的基础设施更优雅地做到这一点,我有一条巧克力鱼可以赠送!
One thing I really don't understand is how I can pass custom start-up options to a mongrel instance.
I see that a common approach is the use environment variables, but in my environment this is not going to work because my rails application serves many different clients. Much code is shared between clients, but there are also many differences which I implement by subclassing controllers and views to overload or extend existing features or introduce new ones. To make this all work, I simply add the paths to client specific modules the module load path ($:).
In order to start the application for a particular client, I could now use an environment variable like say, TARGET=AMAZONE. Unfortunately, on some systems I'm running multiple mongrel clusters, each cluster serving a different client. Some of these systems run under Windows and to start mongrel I installed mongrel_services. Clearly, this makes my environment variable unsuitable.
Passing this extra bit of data to the application is proving to be a real challenge. For a start, mongrel_rails service_install will reject any [custom] command line parameters that aren't documented. I'm not too concerned as installing the services using the install program is trivial.
However, even if I manage to install mongrel_services such that when run it passes the custom command line option --target to mongrel_rails start, I get an error because mongrel_rails doesn't recognize the switch.
So here were the things I looked at:
Pass an extra parameter:
mongrel_rails start --target XYZ ...
use a config file and add target:XYZ, then do:
mongrel_rails start -C x:\myapp\myconfig.yml
modify the file:
Ruby\lib\ruby\gems\1.8\gems\mongrel-1.1.5-x86-mswin32-60\lib\mongrel\command.rb
Perhaps I can use the --script option, but all docs that I found on it were for Unix
1 and 2 simply don't work. I played with 4 but never managed it to do anything. So I had no choice but to go with 3. While it is relatively simple, I hate changing ruby library code.
Particularly disappointing is that 2 doesn't work. I mean what is so unreasonable about adding other [custom] options in the config file? Actually I think this is a fundamental piece that is missing in rails. Somehow, the application should be able to register and access command line arguments it expects.
If anybody has a good idea how to do this more elegantly using the current infrastructure, I have a chocolate fish to give away!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可能没有必要向 mongrel 传递一些东西。或许可以使用现有的机制来提供您所寻求的灵活性。让我们首先尝试清楚地了解这些限制。
换言之,似乎以下条件有效。
应该验证的一个假设是每个代码库都位于单独的目录中。如果是这样,那么可能有一个非常简单的解决方案。
如果每个客户都在自己的目录中,如下所示:
如果您使用以下内容启动混合进程:
那么,您可以拥有一个在系统启动时读取的
customer_config.yml
文件(在您的环境中) .rb),您可以在其中放置您的客户端自定义值。因此,如果您需要传入“Amazone”作为目标值,那么您的 yaml 文件可能如下所示:然后,每个客户都会获得自己的 customer_config.yml 文件,该文件仅驻留在他们的目录中,并且您只有一个文件可以使用更改添加新客户时切换行为。
修改您的environment.rb以查找专门命名的YAML文件是完全可以接受的。解析 YAML 文件相当容易,并且为管理每个客户的定制提供了很大的灵活性。
It may not be necessary to pass in something to mongrel. It may be possible to use an existing mechanism that will provide the flexibility you seek. Let's start by trying to be very clear about the constraints.
To paraphrase, it seems that the following conditions are in effect.
One assumption that should be validated is that each code base is in a separate directory. If so, then there may be a very straightforward solution.
If each customer is in its own directory, like so:
And if you start your mongrel processes with something like:
Then, you could have a
customer_config.yml
file that is read at system startup (in your environment.rb) in which you can place your client customization value. So, if you need to pass in "Amazone" as the target value, then your yaml file might look like:Then, each customer gets their own customer_config.yml file, which resides only in their directory, and you have only one file to change to switch behaviors when adding a new customer.
Modifying your environment.rb to look for a specifically named YAML file would be perfectly acceptable. It's rather easy to parse a YAML file, and it gives a lot of flexibility for managing customization by each customer.