lib/jets/booter.rb in jets-2.0.6 vs lib/jets/booter.rb in jets-2.1.0

- old
+ new

@@ -21,20 +21,52 @@ Jets.application.configs! app_initializers run_turbines(:after_initializers) Jets.application.finish! - # Eager load project code. Rather have user find out early than later on AWS Lambda. - Jets::Autoloaders.main.eager_load + setup_db # establish db connections in Lambda Execution Context. + # The eager load calls connects_to in models and establish those connections in Lambda Execution Context also. + Jets::Autoloaders.main.eager_load # Eager load project code. Rather have user find out early than later on AWS Lambda. - setup_db # TODO: Figure out how to build middleware during Jets.boot without breaking jets new and webpacker:install # build_middleware_stack @booted = true end + # Using ActiveRecord outside of Rails, so we need to set up the db connection ourself. + # + # Only connects to database for ActiveRecord and when config/database.yml exists. + # Dynomite handles connecting to the clients lazily. + def setup_db + return unless File.exist?("#{Jets.root}/config/database.yml") + + db_configs = Jets.application.config.database + # DatabaseTasks.database_configuration for db:create db:migrate tasks + # Documented in DatabaseTasks that this is the right way to set it when + # using ActiveRecord rake tasks outside of Rails. + ActiveRecord::Tasks::DatabaseTasks.database_configuration = db_configs + + if db_configs[Jets.env].blank? + abort("ERROR: config/database.yml exists but no environment section configured for #{Jets.env}") + end + ActiveRecord::Base.configurations = db_configs + connect_db + end + + # Eager connect to database, so connections are established in the Lambda Execution Context and get reused. + # Interestingly, the connections info is stored in the shared state but the connection doesnt show up on + # `show processlist` until after a query. Have confirmed that the connection is reused and the connection count stays + # the same. + def connect_db + primary_hash_config = ActiveRecord::Base.configurations.configs_for(env_name: Jets.env).find { |hash_config| + hash_config.spec_name == "primary" + } + primary_config = primary_hash_config.config # config is a normal Ruby Hash + ActiveRecord::Base.establish_connection(primary_config) + end + def load_internal_turbines Dir.glob("#{__dir__}/internal/turbines/**/*.rb").each do |path| Jets::Autoloaders.once.preload(path) end end @@ -68,30 +100,9 @@ end # Builds and memoize stack so it only gets built on bootup def build_middleware_stack Jets.application.build_stack - end - - # Only connects connect to database for ActiveRecord and when - # config/database.yml exists. - # Dynomite handles connecting to the clients lazily. - def setup_db - return unless File.exist?("#{Jets.root}/config/database.yml") - - db_configs = Jets.application.config.database - # DatabaseTasks.database_configuration for db:create db:migrate tasks - # Documented in DatabaseTasks that this is the right way to set it when - # using ActiveRecord rake tasks outside of Rails. - ActiveRecord::Tasks::DatabaseTasks.database_configuration = db_configs - - current_config = db_configs[Jets.env] - if current_config.blank? - abort("ERROR: config/database.yml exists but no environment section configured for #{Jets.env}") - end - # Using ActiveRecord rake tasks outside of Rails, so we need to set up the - # db connection ourselves - ActiveRecord::Base.establish_connection(current_config) end # Cannot call this for the jets new def confirm_jets_project! unless File.exist?("#{Jets.root}/config/application.rb")