一个 Plack 应用程序如何影响另一个 Plack 应用程序?
我有这个:
use Plack::Builder;
my $config_app = sub {...};
my $app = sub {...}
builder {
mount "/admin" => $config_app;
mount "/" => $app;
};
$config_app
将配置值保存到文件 app.cfg
中,$app
将其加载为配置文件。不需要在每个请求中读取配置文件。需要在申请开始时阅读并在更改时重新阅读。
实现这一目标的最佳方法是什么?
我唯一的想法是:应用程序会记住最后一次config_read_time,并且在每个请求中都会检查app.cfg
的修改时间。如果文件被修改,将重新读取它。
这里有更好的解决方案吗? (意味着 $config_app 和 $app 之间的一些消息传递,例如,当 $config_app 保存新配置时将向 $app 发送一些消息:重新读取配置
。
I have this:
use Plack::Builder;
my $config_app = sub {...};
my $app = sub {...}
builder {
mount "/admin" => $config_app;
mount "/" => $app;
};
the $config_app
saving configuration values into file app.cfg
and the $app
loading it as config-file. Reading config file in every request is not needed. Need read it at the start of application and re-read it when it is changed.
What is the best way achieve this?
My only idea is: The app will remember the last config_read_time, and in every request will check the modification time of the app.cfg
. If the file was modified, will re-read it.
Is here any better solution? (mean some messaging between $config_app and $app, e.g. when $config_app saved the new config will send some message to $app: re-read the config
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
虽然在
$config_app
中调用$app
(有点像内部重定向)并非不可能,但我个人建议不要这样做。如果您创建一个单独的普通 Perl 类(MyApp::ConfigFile 或其他)并从
$app
和$config_app
针对单例对象。请注意,无论如何,该技术仅适用于单进程 Web 服务器环境。如果您检查修改时间并重新读取,则可以在 Starman Web 服务器等分叉环境中工作。Although it's not impossible to call
$app
(kind of like internal redirects) inside$config_app
, i would personally recommend against it.It should be much easier if you create a separate plain-old Perl class (MyApp::ConfigFile or whatever) and calls the method both from
$app
and$config_app
against a singleton object. Note that the technique only works for a single-process web server environment anyway. If you check the modification time and re-read, that can work in a forked environment such as Starman web server.有很多方法可以监控配置文件。
编写如下的配置检查例程:
您需要注意的主要事情是,不要在文件更改时一遍又一遍地重新加载。
现在,在您可能需要加载新一轮配置的任何地方调用“load_config()”。因为它总是成功的,所以你不需要测试或做任何比把它撒在方便的地方更聪明的事情。就像在应用程序处理程序的顶部一样。
这对于执行诸如打开正在运行的进程的日志记录或强制重新加载某些模块之类的操作非常有用。没有太多的用途是缩放不会让你烦恼的。
你知道,如果你有一堆机器来提供服务,它就无法扩展。您必须重新同步已更改的文件,或者更糟糕的是,将它们放在 NFS 挂载上。
这是一个激进的想法:为什么不使用数据库?
我听说现在很酷的孩子们正在尝试基于数据库的网络应用程序,而且它们实际上运行得很好。
严肃地说,编程和构建系统的有趣之处在于在设计权衡之间进行选择。可能存在一些边缘情况,传递更改的文件是一次辉煌的成功,而我尖刻的数据库评论则显示为完全愚蠢。不确定该用例是什么,但它可能存在。 您知道自己的用例。尝试一下。不要害怕变得有点愚蠢。有时,看似非常愚蠢的解决方案结果却出人意料地优雅。然而,如果这个想法最终被证明是愚蠢的,那就从经验中学习并继续前进。
There are a bunch of ways to monitor a config file.
Write a config check routine like this:
The main thing you need to be careful of is that you don't wind up reloading over and over the second that the file changes.
Now call `load_config() anywhere where you might want a load new round of configuration. Since it always succeeds you don't have to test or do anything smarter than sprinkle it in handy places. Like at the top of your app handler.
This will work great for doing things like turning up logging on a running process, or forcing a reload of some module. There aren't a lot of uses where scaling won't bite you.
You know, if you have a bunch of machines serving this, it won't scale. You'd have to rsync the files changed files around, or worse put them on an NFS mount.
Here's a radical notion: why not use a database?
I hear the cool kids are experimenting with database based web apps these days, and that they actually work pretty well.
In all seriousness, the fun thing about programming and building systems is choosing between design trade-offs. There may be some edge case where passing around a changed file is a brilliant success, and my snarky database comment is shown for utter foolishness. Not sure what that use-case is, but it may exist. You know your use case. Try stuff out. Don't be afraid to be a bit stupid. Sometimes, what seems like a really stupid solution turns out to be surprisingly elegant. However, if the idea turns out to be just dumb, learn from the experience and move on.