Rails 中初始化程序多久运行一次?
每次有人访问站点时,Rails 应用程序中的初始化程序都会运行吗?
例如,如果我的服务器上午 10 点在德克萨斯州启动,有人下午 1 点从纽约访问我的网站,有人晚上 10 点从洛杉矶访问我的网站,则当来自纽约的人访问我的网站时,是否运行 Rails 应用程序中的初始化程序和洛杉矶访问,或者初始化程序仅在我启动德克萨斯州的服务器后运行?
我问的原因是因为我在初始化程序文件中使用 case 表达式来根据一天中访问应用程序的时间更改电子邮件设置。当然,只有当有人访问该站点时初始化程序运行时,这才有意义。如果它们仅在服务器启动时运行,那么这只是一种情况...
如果这不是执行此操作的正确位置,或者如果初始化程序仅在德克萨斯州启动服务器后运行(例如),那么您会在哪里把这个代码?
case
when Time.now.hour == 0
ActionMailer::Base.smtp_settings = {
:user_name => "[email protected]",
:password => "blahblah",
:address => "smtp.examplel.com",
:port => 25,
:tls => true
}
when Time.now.hour == 1
ActionMailer::Base.smtp_settings = {
:user_name => "[email protected]",
:password => "eatshit",
:address => "smtp.example.com",
:port => 25,
:tls => true
}
end
Do the initializers in a Rails app run each time someone visits the site?
For example, if my server is started in Texas at 10 a.m. , and someone visits my site from New York at 1 p.m. and someone visits from Los Angeles at 10 p.m, do the the initializers in a rails application run when the people from New York and Los Angeles visit, or do the initializers only run once I start the server in Texas?
The reason I'm asking is because I was using a case expression in an initializer file to change email settings depending on the time of day that app is visited. This would only make sense of course if the initializers ran when someone visited the site. If they ran only when the server was started then it would only be one case...
If that's not the right place to do it, or if the initializers only run once the server is started in Texas (for example) then where would you put this code?
case
when Time.now.hour == 0
ActionMailer::Base.smtp_settings = {
:user_name => "[email protected]",
:password => "blahblah",
:address => "smtp.examplel.com",
:port => 25,
:tls => true
}
when Time.now.hour == 1
ActionMailer::Base.smtp_settings = {
:user_name => "[email protected]",
:password => "eatshit",
:address => "smtp.example.com",
:port => 25,
:tls => true
}
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一个非常简单直接的答案是:仅一次,当您的服务器启动时。
您可能对这篇文章Rails 初始化过程感兴趣
A very simple and straight answer is: Just once, when your server kicks up.
You may be intrested in this article The Rails Initialization Process
每当您启动乘客/杂种或您正在使用的任何东西时,都会加载初始化程序。
要在运行时设置这些设置,请查看 Rails:ActionMailer 的运行时配置?
Initializers get loaded whenever you start up passenger / mongrel or whatever you are using.
To set these settings at runtime take a look at Rails: Runtime configuration of ActionMailer?