The Rails Initialization Process
This guide explains the internals of the initialization process in Rails.It is an extremely in-depth guide and recommended for advanced Rails developers.
After reading this guide, you will know:
- How to use
bin/rails server. - The timeline of Rails' initialization sequence.
- Where different files are required by the boot sequence.
- How the Rails::Server interface is defined and used.
This guide goes through every method call that isrequired to boot up the Ruby on Rails stack for a default Railsapplication, explaining each part in detail along the way. For thisguide, we will be focusing on what happens when you executebin/rails serverto boot your app.
Paths in this guide are relative to Rails or a Rails application unless otherwise specified.
If you want to follow along while browsing the Railssourcecode, we recommend that you use thetkey binding to open the file finder inside GitHub and find filesquickly.
1. Launch!
Let's start to boot and initialize the app. A Rails application is usuallystarted by runningbin/rails console orbin/rails server.
1.1.bin/rails
This file is as follows:
#!/usr/bin/env rubyAPP_PATH=File.expand_path("../config/application",__dir__)require_relative"../config/boot"require"rails/commands"TheAPP_PATH constant will be used later inrails/commands. Theconfig/boot file referenced here is theconfig/boot.rb file in our application which is responsible for loading Bundler and setting it up.
1.2.config/boot.rb
config/boot.rb contains:
ENV["BUNDLE_GEMFILE"]||=File.expand_path("../Gemfile",__dir__)require"bundler/setup"# Set up gems listed in the Gemfile.require"bootsnap/setup"# Speed up boot time by caching expensive operations.In a standard Rails application, there's aGemfile which declares alldependencies of the application.config/boot.rb setsENV['BUNDLE_GEMFILE'] to the location of this file. If theGemfileexists, thenbundler/setup is required. The require is used by Bundler toconfigure the load path for your Gemfile's dependencies.
1.3.rails/commands.rb
Onceconfig/boot.rb has finished, the next file that is required israils/commands, which helps in expanding aliases. In the current case, theARGV array simply containsserver which will be passed over:
require"rails/command"aliases={"g"=>"generate","d"=>"destroy","c"=>"console","s"=>"server","db"=>"dbconsole","r"=>"runner","t"=>"test"}command=ARGV.shiftcommand=aliases[command]||commandRails::Command.invokecommand,ARGVIf we had useds rather thanserver, Rails would have used thealiasesdefined here to find the matching command.
1.4.rails/command.rb
When one types a Rails command,invoke tries to lookup a command for the givennamespace and executes the command if found.
If Rails doesn't recognize the command, it hands the reins over to Raketo run a task of the same name.
As shown,Rails::Command displays the help output automatically if thenamespaceis empty.
moduleRailsmoduleCommandclass<<selfdefinvoke(full_namespace,args=[],**config)args=["--help"]ifrails_new_with_no_path?(args)full_namespace=full_namespace.to_snamespace,command_name=split_namespace(full_namespace)command=find_by_namespace(namespace,command_name)with_argv(args)doifcommand&&command.all_commands[command_name]command.perform(command_name,args,config)elseinvoke_rake(full_namespace,args,config)endendrescueUnrecognizedCommandError=>erroriferror.name==full_namespace&&command&&command_name==full_namespacecommand.perform("help",[],config)elseputserror.detailed_messageendexit(1)endendendendWith theserver command, Rails will further run the following code:
moduleRailsmoduleCommandclassServerCommand<Base# :nodoc:defperformset_application_directory!prepare_restartRails::Server.new(server_options).tapdo|server|# Require application after server sets environment to propagate# the --environment option.requireAPP_PATHDir.chdir(Rails.application.root)ifserver.serveable?print_boot_information(server.server,server.served_url)after_stop_callback=->{say"Exiting"unlessoptions[:daemon]}server.start(after_stop_callback)elsesayrack_server_suggestion(options[:using])endendendendendendThis file will change into the Rails root directory (a path two directories upfromAPP_PATH which points atconfig/application.rb), but only if theconfig.ru file isn't found. This then starts up theRails::Server class.
1.5.actionpack/lib/action_dispatch.rb
Action Dispatch is the routing component of the Rails framework.It adds functionality like routing, session, and common middlewares.
1.6.rails/commands/server/server_command.rb
TheRails::Server class is defined in this file by inheriting fromRackup::Server. WhenRails::Server.new is called, this calls theinitializemethod inrails/commands/server/server_command.rb:
moduleRailsclassServer<Rackup::Serverdefinitialize(options=nil)@default_options=options||{}super(@default_options)set_environmentendendendFirstly,super is called which calls theinitialize method onRackup::Server.
1.7. Rackup:lib/rackup/server.rb
Rackup::Server is responsible for providing a common server interface for all Rack-based applications, which Rails is now a part of.
Theinitialize method inRackup::Server simply sets several variables:
moduleRackupclassServerdefinitialize(options=nil)@ignore_options=[]ifoptions@use_default_options=false@options=options@app=options[:app]ifoptions[:app]else@use_default_options=true@options=parse_options(ARGV)endendendendIn this case, return value ofRails::Command::ServerCommand#server_options will be assigned tooptions.When lines inside if statement is evaluated, a couple of instance variables will be set.
server_options method inRails::Command::ServerCommand is defined as follows:
moduleRailsmoduleCommandclassServerCommand<Base# :nodoc:no_commandsdodefserver_options{user_supplied_options:user_supplied_options,server:options[:using],log_stdout:log_to_stdout?,Port:port,Host:host,DoNotReverseLookup:true,config:options[:config],environment:environment,daemonize:options[:daemon],pid:pid,caching:options[:dev_caching],restart_cmd:restart_command,early_hints:early_hints}endendendendendThe value will be assigned to instance variable@options.
Aftersuper has finished inRackup::Server, we jump back torails/commands/server/server_command.rb. At this point,set_environmentis called within the context of theRails::Server object.
moduleRailsmoduleServerdefset_environmentENV["RAILS_ENV"]||=options[:environment]endendendAfterinitialize has finished, we jump back into the server commandwhereAPP_PATH (which was set earlier) is required.
1.8.config/application
Whenrequire APP_PATH is executed,config/application.rb is loaded (recallthatAPP_PATH is defined inbin/rails). This file exists in your applicationand it's free for you to change based on your needs.
1.9.Rails::Server#start
Afterconfig/application is loaded,server.start is called. This method isdefined like this:
moduleRailsclassServer<::Rackup::Serverdefstart(after_stop_callback=nil)trap(:INT){exit}create_tmp_directoriessetup_dev_cachinglog_to_stdoutifoptions[:log_stdout]super()# ...endprivatedefsetup_dev_cachingifoptions[:environment]=="development"Rails::DevCaching.enable_by_argument(options[:caching])endenddefcreate_tmp_directories%w(cache pids sockets).eachdo|dir_to_make|FileUtils.mkdir_p(File.join(Rails.root,"tmp",dir_to_make))endenddeflog_to_stdoutwrapped_app# touch the app so the logger is set upconsole=ActiveSupport::Logger.new(STDOUT)console.formatter=Rails.logger.formatterconsole.level=Rails.logger.levelunlessActiveSupport::Logger.logger_outputs_to?(Rails.logger,STDERR,STDOUT)Rails.logger.broadcast_to(console)endendendendThis method creates a trap forINT signals, so if youCTRL-C the server, it will exit the process.As we can see from the code here, it will create thetmp/cache,tmp/pids, andtmp/sockets directories. It then enables caching in developmentifbin/rails server is called with--dev-caching. Finally, it callswrapped_app which isresponsible for creating the Rack app, before creating and assigning an instanceofActiveSupport::Logger.
Thesuper method will callRackup::Server.start which begins its definition as follows:
moduleRackupclassServerdefstart(&block)ifoptions[:warn]$-w=trueendifincludes=options[:include]$LOAD_PATH.unshift(*includes)endArray(options[:require]).eachdo|library|requirelibraryendifoptions[:debug]$DEBUG=truerequire"pp"poptions[:server]ppwrapped_appppappendcheck_pid!ifoptions[:pid]# Touch the wrapped app, so that the config.ru is loaded before# daemonization (i.e. before chdir, etc).handle_profiling(options[:heapfile],options[:profile_mode],options[:profile_file])dowrapped_appenddaemonize_appifoptions[:daemonize]write_pidifoptions[:pid]trap(:INT)doifserver.respond_to?(:shutdown)server.shutdownelseexitendendserver.run(wrapped_app,**options,&block)endendendThe interesting part for a Rails app is the last line,server.run. Here we encounter thewrapped_app method again, which this timewe're going to explore more (even though it was executed before, andthus memoized by now).
moduleRackupclassServerdefwrapped_app@wrapped_app||=build_appappendendendTheapp method here is defined like so:
moduleRackupclassServerdefapp@app||=options[:builder]?build_app_from_string:build_app_and_options_from_configend# ...privatedefbuild_app_and_options_from_configif!::File.exist?options[:config]abort"configuration#{options[:config]} not found"endRack::Builder.parse_file(self.options[:config])enddefbuild_app_from_stringRack::Builder.new_from_string(self.options[:builder])endendendTheoptions[:config] value defaults toconfig.ru which contains this:
# This file is used by Rack-based servers to start the application.require_relative"config/environment"runRails.applicationRails.application.load_serverTheRack::Builder.parse_file method here takes the content from thisconfig.ru file and parses it using this code:
moduleRackclassBuilderdefself.load_file(path,**options)# ...new_from_string(config,path,**options)end# ...defself.new_from_string(builder_script,path="(rackup)",**options)builder=self.new(**options)# We want to build a variant of TOPLEVEL_BINDING with self as a Rack::Builder instance.# We cannot use instance_eval(String) as that would resolve constants differently.binding=BUILDER_TOPLEVEL_BINDING.call(builder)eval(builder_script,binding,path)builder.to_appendendendTheinitialize method ofRack::Builder will take the block here and execute it within an instance ofRack::Builder.This is where the majority of the initialization process of Rails happens.Therequire line forconfig/environment.rb inconfig.ru is the first to run:
require_relative"config/environment"1.10.config/environment.rb
This file is the common file required byconfig.ru (bin/rails server) and Passenger. This is where these two ways to run the server meet; everything before this point has been Rack and Rails setup.
This file begins with requiringconfig/application.rb:
require_relative"application"1.11.config/application.rb
This file requiresconfig/boot.rb:
require_relative"boot"But only if it hasn't been required before, which would be the case inbin/rails serverbutwouldn't be the case with Passenger.
Then the fun begins!
2. Loading Rails
The next line inconfig/application.rb is:
require"rails/all"2.1.railties/lib/rails/all.rb
This file is responsible for requiring all the individual frameworks of Rails:
require"rails"%w( active_record/railtie active_storage/engine action_controller/railtie action_view/railtie action_mailer/railtie active_job/railtie action_cable/engine action_mailbox/engine action_text/engine rails/test_unit/railtie).eachdo|railtie|beginrequirerailtierescueLoadErrorendendThis is where all the Rails frameworks are loaded and thus madeavailable to the application. We won't go into detail of what happensinside each of those frameworks, but you're encouraged to try andexplore them on your own.
For now, just keep in mind that common functionality like Rails engines,I18n and Rails configuration are all being defined here.
2.2. Back toconfig/environment.rb
The rest ofconfig/application.rb defines the configuration for theRails::Application which will be used once the application is fullyinitialized. Whenconfig/application.rb has finished loading Rails and definedthe application namespace, we go back toconfig/environment.rb. Here, theapplication is initialized withRails.application.initialize!, which isdefined inrails/application.rb.
2.3.railties/lib/rails/application.rb
Theinitialize! method looks like this:
definitialize!(group=:default)# :nodoc:raise"Application has been already initialized."if@initializedrun_initializers(group,self)@initialized=trueselfendYou can only initialize an app once. The Railtieinitializersare run through therun_initializers method which is defined inrailties/lib/rails/initializable.rb:
defrun_initializers(group=:default,*args)returnifinstance_variable_defined?(:@ran)initializers.tsort_eachdo|initializer|initializer.run(*args)ifinitializer.belongs_to?(group)end@ran=trueendTherun_initializers code itself is tricky. What Rails is doing here istraversing all the class ancestors looking for those that respond to aninitializers method. It then sorts the ancestors by name, and runs them.For example, theEngine class will make all the engines available byproviding aninitializers method on them.
TheRails::Application class, as defined inrailties/lib/rails/application.rbdefinesbootstrap,railtie, andfinisher initializers. Thebootstrap initializersprepare the application (like initializing the logger) while thefinisherinitializers (like building the middleware stack) are run last. Therailtieinitializers are the initializers which have been defined on theRails::Applicationitself and are run between thebootstrap andfinisher.
Do not confuse Railtie initializers overall with theload_config_initializersinitializer instance or its associated config initializers inconfig/initializers.
After this is done we go back toRackup::Server.
2.4. Rack: lib/rack/server.rb
Last time we left when theapp method was being defined:
moduleRackupclassServerdefapp@app||=options[:builder]?build_app_from_string:build_app_and_options_from_configend# ...privatedefbuild_app_and_options_from_configif!::File.exist?options[:config]abort"configuration#{options[:config]} not found"endRack::Builder.parse_file(self.options[:config])enddefbuild_app_from_stringRack::Builder.new_from_string(self.options[:builder])endendendAt this pointapp is the Rails app itself (a middleware), and whathappens next is Rack will call all the provided middlewares:
moduleRackupclassServerprivatedefbuild_app(app)middleware[options[:environment]].reverse_eachdo|middleware|middleware=middleware.call(self)ifmiddleware.respond_to?(:call)nextunlessmiddlewareklass,*args=middlewareapp=klass.new(app,*args)endappendendendRemember,build_app was called (bywrapped_app) in the last line ofRackup::Server#start.Here's how it looked like when we left:
server.run(wrapped_app,**options,&block)At this point, the implementation ofserver.run will depend on theserver you're using. For example, if you were using Puma, here's whattherun method would look like:
moduleRackmoduleHandlermodulePuma# ...defself.run(app,options={})conf=self.config(app,options)log_writer=options.delete(:Silent)?::Puma::LogWriter.strings:::Puma::LogWriter.stdiolauncher=::Puma::Launcher.new(conf,log_writer:log_writer,events:@events)yieldlauncherifblock_given?beginlauncher.runrescueInterruptputs"* Gracefully stopping, waiting for requests to finish"launcher.stopputs"* Goodbye!"endend# ...endendendWe won't dig into the server configuration itself, but this isthe last piece of our journey in the Rails initialization process.
This high level overview will help you understand when your code isexecuted and how, and overall become a better Rails developer. If youstill want to know more, the Rails source code itself is probably thebest place to go next.