I tried:
after_initialize do
#code
end
But: (documentation)
Some parts of your application, notably observers and routing, are not yet set up at the point where the after_initialize block is called.
I need routing and logger in my code
Any ideas?
See section 3.1 from http://guides.rubyonrails.org/configuring.html
I beleive you would put this code in config/application.rb
Also http://guides.rubyonrails.org/initialization.html
Lines added to
config.ru
will be run by the Rails server, but not by Rails console or Rake tasks that load the environment.@house9's answer is correct, as pointed out by the comments, this will also execute when running rake tasks, console, etc. I used the following to recognize when a server was actually being executed:
Another option is to create a custom initializer. It's just a ruby file that lives under config/initializers/ and is executed exactly "on_server_start" event :)
Since Rails 5 the default server is Puma, so code in
config/puma.rb
will be run just once, and only if the server is started.