Frank Rennekamp <[email protected]> wrote:
> Hi all,
>
> I'm fighting with the after_fork hook and my sinatra application. The Sinatra
> app is using active_record,
> In my unciron.rb file I'm using
> preload_app true
<snip>
> I get the same error, when using different database adapter. On the
> other hand everything is working fine, when repeating the database
> configuration in after_fork hook, or completely remove database
> configuration from Sinatra app into after_fork hook. But this is not a
> solution for me. First I don't like to double code, second I need the
> database configuration in the Sinatra app for running tests .
I agree, none of those solutions seem good.
> My question is what am I doing wrong? All documentation says that
> defined?(ActiveRecord::Base) and
> ActiveRecord::Base.establish_connection
> end
> should work.
I'm not familiar with Rails nowadays, but from reading the Rails 3.2.6
source/docs, I believe calling ActiveRecord::Base.establish_connection
(without arguments) only works with a Rails environment.
Perhaps a Rails DB config file (config/database.yml) is all that's
needed, you could try that.
For the ultimate in DRY-ness, perhaps putting the logic in a before
block for your Sinatra app:
--------------------------------- 8< --------------------------------
# use a Mutex in case this gets used on a multi-threaded server
# (something other than unicorn)
db_lock = Mutex.new
db_connected = false
before do
db_lock.synchronize do
unless db_connected
ActiveRecord::Base.establish_connection(
:adapter => 'sqlite3',
:database => '/tmp/dbfile'
)
db_connected = true
end
end
end
--------------------------------- 8< --------------------------------
The above example should work regardless of which Rack server you
choose, and I think the per-request overhead will be negligible.
I'd also be interested to hear anybody elses' experiences and
recommendations for combining Sinatra + ActiveRecord with unicorn.
_______________________________________________
Unicorn mailing list - [email protected]
http://rubyforge.org/mailman/listinfo/mongrel-unicorn
Do not quote signatures (like this one) or top post when replying