Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How often do initializers run in Rails?

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
like image 661
Leahcim Avatar asked Dec 18 '11 19:12

Leahcim


2 Answers

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

like image 159
Mohit Jain Avatar answered Sep 20 '22 00:09

Mohit Jain


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?

like image 33
Scott Avatar answered Sep 20 '22 00:09

Scott