thiagopradi / octopus

Database Sharding for ActiveRecord
2.53k stars 504 forks source link

Rails 5.2 + Puma doesn't work when Puma workers are enabled #511

Open zainzafar opened 5 years ago

zainzafar commented 5 years ago

My environment: Rails 5.2.0 Octopus: 0.10.1 Puma 3.12.0

undefined method `any?' for nil:NilClass

activerecord (5.2.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:421:in `block in connected?'
/Users/zain/.rbenv/versions/2.6.0/lib/ruby/2.6.0/monitor.rb:230:in `mon_synchronize'
activerecord (5.2.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:421:in `connected?'
ar-octopus (0.10.1) lib/octopus/proxy.rb:73:in `safe_connection'
ar-octopus (0.10.1) lib/octopus/proxy.rb:80:in `select_connection'
ar-octopus (0.10.1) lib/octopus/proxy.rb:225:in `legacy_method_missing_logic'
ar-octopus (0.10.1) lib/octopus/proxy.rb:131:in `method_missing'
activerecord (5.2.0) lib/active_record/migration.rb:554:in `call'
newrelic_rpm (5.7.0.350) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
actionpack (5.2.0) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
activesupport (5.2.0) lib/active_support/callbacks.rb:98:in `run_callbacks'
actionpack (5.2.0) lib/action_dispatch/middleware/callbacks.rb:26:in `call'
newrelic_rpm (5.7.0.350) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
actionpack (5.2.0) lib/action_dispatch/middleware/executor.rb:14:in `call'
newrelic_rpm (5.7.0.350) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
actionpack (5.2.0) lib/action_dispatch/middleware/debug_exceptions.rb:61:in `call'
newrelic_rpm (5.7.0.350) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
web-console (3.7.0) lib/web_console/middleware.rb:135:in `call_app'
web-console (3.7.0) lib/web_console/middleware.rb:30:in `block in call'
web-console (3.7.0) lib/web_console/middleware.rb:20:in `catch'
web-console (3.7.0) lib/web_console/middleware.rb:20:in `call'
newrelic_rpm (5.7.0.350) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
actionpack (5.2.0) lib/action_dispatch/middleware/show_exceptions.rb:33:in `call'
newrelic_rpm (5.7.0.350) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
railties (5.2.0) lib/rails/rack/logger.rb:38:in `call_app'
railties (5.2.0) lib/rails/rack/logger.rb:26:in `block in call'
activesupport (5.2.0) lib/active_support/tagged_logging.rb:71:in `block in tagged'
activesupport (5.2.0) lib/active_support/tagged_logging.rb:28:in `tagged'
activesupport (5.2.0) lib/active_support/tagged_logging.rb:71:in `tagged'
railties (5.2.0) lib/rails/rack/logger.rb:26:in `call'
newrelic_rpm (5.7.0.350) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
sprockets-rails (3.2.1) lib/sprockets/rails/quiet_assets.rb:13:in `call'
newrelic_rpm (5.7.0.350) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
actionpack (5.2.0) lib/action_dispatch/middleware/remote_ip.rb:81:in `call'
newrelic_rpm (5.7.0.350) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
request_store (1.4.1) lib/request_store/middleware.rb:19:in `call'
newrelic_rpm (5.7.0.350) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
actionpack (5.2.0) lib/action_dispatch/middleware/request_id.rb:27:in `call'
newrelic_rpm (5.7.0.350) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
rack (2.0.6) lib/rack/method_override.rb:22:in `call'
newrelic_rpm (5.7.0.350) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
rack (2.0.6) lib/rack/runtime.rb:22:in `call'
newrelic_rpm (5.7.0.350) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
activesupport (5.2.0) lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'
newrelic_rpm (5.7.0.350) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
actionpack (5.2.0) lib/action_dispatch/middleware/executor.rb:14:in `call'
newrelic_rpm (5.7.0.350) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
actionpack (5.2.0) lib/action_dispatch/middleware/static.rb:127:in `call'
newrelic_rpm (5.7.0.350) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
rack (2.0.6) lib/rack/sendfile.rb:111:in `call'
newrelic_rpm (5.7.0.350) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
rack-cors (1.0.2) lib/rack/cors.rb:97:in `call'
newrelic_rpm (5.7.0.350) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
railties (5.2.0) lib/rails/engine.rb:524:in `call'
newrelic_rpm (5.7.0.350) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
puma (3.12.0) lib/puma/configuration.rb:225:in `call'
puma (3.12.0) lib/puma/server.rb:658:in `handle_request'
puma (3.12.0) lib/puma/server.rb:472:in `process_client'
puma (3.12.0) lib/puma/server.rb:332:in `block in run'
puma (3.12.0) lib/puma/thread_pool.rb:133:in `block in spawn_thread'
error
kjvarga commented 5 years ago

Seems to be a duplicate of https://github.com/thiagopradi/octopus/issues/489

knagode commented 5 years ago

@zainzafar did you figure this out? I have the same issue ...

zainzafar commented 5 years ago

@knagode nop.. I stuck to Rails 5.1.x for now. Rails 6 has native support for multiple databases.

knagode commented 5 years ago

@zainzafar After I added (config/puma.rb) ...

on_worker_boot do
  ActiveRecord::Base.establish_connection
  ActiveRecord::Base.connection.initialize_shards(Octopus.config
end

Octopus started working but I only can use it on dynos - I still have issues on worker dynos or rails console.

knagode commented 5 years ago

@zainzafar how do you use octopus in gemfile? gem 'ar-octopus'? Or do you load specific version? Which?

zainzafar commented 5 years ago

I did gem ar-octopus and made sure that I had the latest version in Gemfile.lock

thiagopradi commented 5 years ago

HI @zainzafar,

I haven't used Octopus + Puma yet. I'll try to reproduce your error locally to see if I can fix it.

Thiago

zainzafar commented 5 years ago

Thanks @thiagopradi. This is the only gem that's preventing us to upgrade to Rails 5.2. Would love a fix to this.

redox commented 4 years ago

@thiagopradi looks like there is a number of issues opened related to this; any chance you could help?

zainzafar commented 4 years ago

Any update on this @thiagopradi?

stuartchaney commented 4 years ago

@zainzafar - did you have any luck with this?

jnarowski commented 1 year ago

Still running into issues with this. Tried using the branch from master as well as kevinjcoleman's branch, but having no luck. Anyone else figure this out?

gem 'ar-octopus', git: 'https://github.com/kevinjcoleman/octopus.git', branch: 'feature/updating-octopus-versions'

We're using Puma and Rails 5.2.8.1

jnarowski commented 1 year ago

Don't want to speak too soon, but I might have fixed it with this:

on_worker_boot do
  if defined?(ActiveRecord::Base)
    ActiveRecord::Base.establish_connection
    ActiveRecord::Base.connection.initialize_shards(Octopus.config) if Octopus.enabled?
  end
end
Adesoji1 commented 10 months ago

Can you upgrade your rails to version 7?