Correct place to connect Sequel DB when working with Phusion Passenger with nginx

I have a test application written in ruby ​​using Sinatra + Sequel.

config.ru:

require './main' run Sinatra::Application 

Code example:

 require 'sinatra' require 'haml' require 'sequel' DB=Sequel.connect('oracle://test: test@test ') class Tarification < Sequel::Model(DB[:test_table]) end get '/' do haml :index end 

Everything was fine until I started using Phusion Passenger in my test environment. Now I have an exception in nginx error.log:

Sequel :: DatabaseError - RuntimeError: The connection cannot be reused in a forked process.

Is it correct to place the procedure for connecting to DB in the config.ru configuration file or is it better to do it differently? If the first option is different than how to make a call to connect to the application code correctly?

PS: I know that I can make the passenger_spawn_method conservative and continue to open the connection in the application code, but this is not the way I'm looking for because of its speed and resource usage problems.

+4
source share
2 answers

This issue is documented in Appendix C.3 of the Phusion Passenger Guide. The usual method is to connect to the post-fork callback and restore the connection there.

+2
source

Sorry, I don’t have the opportunity to check it on the Passenger, and I don’t know if this will work, but it’s better to connect in the configure block:

 configure do Sequel.connect(ENV['DATABASE_URL'] || 'sqlite://blog.db') end 

Example: Scanty is another great Sinatra + Sequel application.

+1
source

Source: https://habr.com/ru/post/1391096/


All Articles