Skip to content

Instantly share code, notes, and snippets.

@alekpopovic
Forked from kripy/unicorn.rb
Created May 18, 2023 11:33
Show Gist options
  • Save alekpopovic/0c7eb2039c3a1049d02a5d632a05c94e to your computer and use it in GitHub Desktop.
Save alekpopovic/0c7eb2039c3a1049d02a5d632a05c94e to your computer and use it in GitHub Desktop.
Heroku Sequel Fork
# Using Sequel with Sinatra (Unicorn) on Heroku and getting this error:
# Sequel::DatabaseDisconnectError - PG::ConnectionBad: PQconsumeInput() SSL error: decryption failed or bad record mac.
# Need to do some forking.
# In unicorn.rb:
worker_processes 4 # amount of unicorn workers to spin up
timeout 30 # restarts workers that hang for 30 seconds
preload_app true # avoid regeneration of jekyll site for each fork
before_fork do |server, worker|
Signal.trap 'TERM' do
puts 'Unicorn master intercepting TERM and sending myself QUIT instead'
Process.kill 'QUIT', Process.pid
end
defined?(Sequel::Model) and
Sequel::Model.db.disconnect
end
after_fork do |server, worker|
Signal.trap 'TERM' do
puts 'Unicorn worker intercepting TERM and doing nothing. Wait for master to send QUIT'
end
defined?(Sequel::Model) and
Sequel::Model.db.connect(ENV['DATABASE_URL'] || 'postgres://user@localhost/database')
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment