puma / puma

A Ruby/Rack web server built for parallelism

Home Page:https://puma.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

before_worker_boot failed with exception (NoMethodError) undefined method `[]='

jramiresbrito opened this issue · comments

Describe the bug
Using Puma with workers and preload_app! raises the following warnings:

[47470] WARNING hook before_worker_boot failed with exception (NoMethodError) undefined method `[]=' for #<ActiveRecord::DatabaseConfigurations::HashConfig:0x000055f5bde953c8 @env_name="development", @name="primary", @configuration_hash={:adapter=>"postgresql", :encoding=>"utf8", :username=>"****", :password=>"****", :timeout=>5000, :pool=>5, :prepared_statements=>false, :host=>"127.0.0.1", :port=>5432, :sslmode=>"disable", :database=>"****"}>
[47435] - Worker 1 (PID: 47470) booted in 0.07s, phase: 0
[47465] WARNING hook before_worker_boot failed with exception (NoMethodError) undefined method `[]=' for #<ActiveRecord::DatabaseConfigurations::HashConfig:0x000055f5bde953c8 @env_name="development", @name="primary", @configuration_hash={:adapter=>"postgresql", :encoding=>"utf8", :username=>"****", :password=>"****", :timeout=>5000, :pool=>5, :prepared_statements=>false, :host=>"127.0.0.1", :port=>5432, :sslmode=>"disable", :database=>"****"}>
[47435] - Worker 0 (PID: 47465) booted in 0.08s, phase: 0

Puma config:

# frozen_string_literal: true

max_threads_count = ENV.fetch('RAILS_MAX_THREADS') { 5 }
min_threads_count = ENV.fetch('RAILS_MIN_THREADS') { max_threads_count }
threads min_threads_count, max_threads_count

port ENV.fetch('PORT') { 3000 }

environment ENV.fetch('RAILS_ENV') { 'development' }

pidfile ENV.fetch('PIDFILE') { 'tmp/pids/server.pid' }

if ENV.fetch('RAILS_ENV') == 'development' or ENV.fetch('CI', false)
  worker_timeout 3600
end

workers ENV.fetch('WEB_CONCURRENCY', 2).to_i

preload_app!

pg_statement_timeout = ENV.fetch('WEB_POSTGRES_STATEMENT_TIMEOUT', 29).to_i
prepared_statements_limit = ENV.fetch('PREPARED_STATEMENTS_LIMIT', 1000).to_i
sidekiq_concurrency = ENV.fetch('SIDEKIQ_CONCURRENCY', 5).to_i

on_worker_boot do
  ActiveSupport.on_load(:active_record) do
    config = 
      ActiveRecord::Base.configurations.configs_for(env_name: Rails.env).first ||
      Rails.application.config.database_configuration.configs_for(env_name: Rails.env).first
    config['pool'] = max_threads_count
    config['statement_limit'] = prepared_statements_limit
    config['prepared_statements'] = false
    config['variables'] ||= {}
    config['variables']['statement_timeout'] = pg_statement_timeout.seconds.in_milliseconds.to_s
    ActiveRecord::Base.establish_connection(config)
  end

  Sidekiq.configure_client do |config|
    config.redis = {
      size: sidekiq_concurrency,
      url: RedisConfig.sidekiq_url,
      ssl_params: { verify_mode: OpenSSL::SSL::VERIFY_NONE }
    }
  end
end

plugin :tmp_restart

To Reproduce
Just start the server with rails s or pumactl start

Expected behavior
Server start without warnings

Desktop (please complete the following information):

  • OS: Linux ( Ubuntu 22.04.3 LTS )
  • Puma Version: 6.3.0
  • Rails Version: 6.1.7.4
  • Ruby Version: ruby 3.0.6-p216