rom-sql: undefined method `indexes' for nil:NilClass (clustered-mode Puma)
Hi @solnic . I have an almost empty Rails application with the next gems stack:
gem 'rails', '5.1.2'
gem 'pg', '~> 0.20'
gem 'puma', '~> 3.7' # (3.9.1)
gem 'rom', '4.0.0.beta2'
gem 'rom-rails', '1.0.0.beta1'
gem 'rom-sql', '2.0.0.beta2'
Also I have Users relation like this:
class Users < ROM::Relation[:sql]
schema infer: true
end
Initializer looks like:
ROM::Rails::Railtie.configure do |config|
config.gateways[:default] = [:sql, ENV.fetch('DATABASE_URL')]
end
Nothing special as you can see. When I start rails server and trying to access an app I get the folowwing error:
undefined method `indexes’ for nil:NilClass
methods = schema.indexes.each_with_object([]) do |index, generated| rom-sql (2.0.0.beta2) lib/rom/plugins/relation/sql/auto_restrictions.rb:42:in `restriction_methods’
But after some time (~5 seconds) I refresh the page and everything works. I think that problem correlates with the Puma server, because when I run application in a single-mode - everything is OK, but when I use clustered-mode with some number of workers - the problem occurs.
As I can understand it is a problem with ROM-rb database connections, that is related to preload_app! and on_worker_boot functionality. There must be something like it is done for ActiveRecord:
on_worker_boot do
ActiveSupport.on_load(:active_record) do
ActiveRecord::Base.establish_connection
end
end
A little bit similar question was asked on StackOverflow.
What do you think about this? Maybe there are some documentation about using ROM-rb with Puma server?
Some additional info:
Puma config:
threads_count = ENV.fetch('MAX_THREADS') { 5 }.to_i
threads threads_count, threads_count
port ENV.fetch('PORT') { 3000 }
environment ENV.fetch('RAILS_ENV') { 'development' }
preload_app!
workers ENV.fetch('WEB_CONCURRENCY') { 4 }
worker_timeout ENV.fetch('WEB_TIMEOUT') { 60 }.to_i
# Allow puma to be restarted by `rails restart` command.
plugin :tmp_restart
Stack trace
rom-sql (2.0.0.beta2) lib/rom/plugins/relation/sql/auto_restrictions.rb:42:in `restriction_methods'
rom-sql (2.0.0.beta2) lib/rom/plugins/relation/sql/auto_restrictions.rb:34:in `block in <module:AutoRestrictions>'
rom-core (4.0.0.beta2) lib/rom/support/notifications.rb:49:in `trigger'
rom-core (4.0.0.beta2) lib/rom/support/notifications.rb:21:in `block in trigger'
rom-core (4.0.0.beta2) lib/rom/support/notifications.rb:20:in `each'
rom-core (4.0.0.beta2) lib/rom/support/notifications.rb:20:in `trigger'
rom-core (4.0.0.beta2) lib/rom/setup/finalize/finalize_relations.rb:93:in `build_relation'
rom-core (4.0.0.beta2) lib/rom/setup/finalize/finalize_relations.rb:48:in `block (2 levels) in run!'
rom-core (4.0.0.beta2) lib/rom/setup/finalize/finalize_relations.rb:31:in `each'
rom-core (4.0.0.beta2) lib/rom/setup/finalize/finalize_relations.rb:31:in `block in run!'
rom-core (4.0.0.beta2) lib/rom/relation_registry.rb:7:in `initialize'
rom-core (4.0.0.beta2) lib/rom/registry.rb:22:in `new'
rom-core (4.0.0.beta2) lib/rom/registry.rb:22:in `new'
rom-core (4.0.0.beta2) lib/rom/setup/finalize/finalize_relations.rb:30:in `run!'
rom-core (4.0.0.beta2) lib/rom/setup/finalize.rb:86:in `load_relations'
rom-core (4.0.0.beta2) lib/rom/setup/finalize.rb:62:in `run!'
rom-core (4.0.0.beta2) lib/rom/create_container.rb:34:in `finalize'
rom-core (4.0.0.beta2) lib/rom/create_container.rb:11:in `initialize'
rom-core (4.0.0.beta2) lib/rom/create_container.rb:53:in `initialize'
rom-core (4.0.0.beta2) lib/rom/create_container.rb:58:in `new'
rom-core (4.0.0.beta2) lib/rom/create_container.rb:58:in `container'
rom-rails (1.0.0.beta1) lib/rom/rails/railtie.rb:79:in `create_container'
railties (5.1.2) lib/rails/railtie.rb:185:in `public_send'
railties (5.1.2) lib/rails/railtie.rb:185:in `method_missing'
rom-rails (1.0.0.beta1) lib/rom/rails/railtie.rb:44:in `block in <class:Railtie>'
activesupport (5.1.2) lib/active_support/callbacks.rb:413:in `instance_exec'
activesupport (5.1.2) lib/active_support/callbacks.rb:413:in `block in make_lambda'
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Comments: 16 (9 by maintainers)
OK I’m gonna close this but I will keep in mind thread safety during setup/finalize. We should address it at some point.