tra/spawnling

Need to preserve all connection pools

Closed this issue · 2 comments

Rails supports multiple databases among models via Model.establish_connection('other_database'), and spawn resets these connection pools:

Ex, we put delayed jobs in a different database:

ActiveRecord::Base.connection_handler.connection_pools.keys # => [ "Delayed::Job", "ActiveRecord::Base"]

spawn { ActiveRecord::Base.connection_handler.connection_pools.keys } # => ["ActiveRecord::Base"]

I'm currently hacking around this, but thought I'd make a ticket.

I've done more hacking, and thought I'd share. Still don't feel like patching spawn's code, it does a lot of connection management already that I don't want to mess with, but I'm using DelayedJobSpawner and put this in its prefork:

$AR_connection_pools = ActiveRecord::Base.connection_handler.connection_pools.map do |name, pool|
  OpenStruct.new(:model => name, :config => pool.spec.config)
end

and this in the each_spawn:

$AR_connection_pools.each {|c| c.model.constantize.establish_connection(c.config)}

maybe that will help push the ticket along.

BTW, rails 2.3.5

Closing to clean up as this in an old issue, if this is still valid on the latest version of spawnling then please re-open the issue and I will address it.