Skip to content

Instantly share code, notes, and snippets.

@warmwaffles
Last active August 29, 2015 14:05
Show Gist options
  • Save warmwaffles/1528f71988d4193d9bd0 to your computer and use it in GitHub Desktop.
Save warmwaffles/1528f71988d4193d9bd0 to your computer and use it in GitHub Desktop.
Do you need a way to run unicorn like you would with webrick?
require 'fileutils'
APP_PATH = FileUtils.pwd()
APP_PID_PATH = 'tmp/pids/unicorn.pid'
APP_PORT = ENV['PORT']
APP_SOCKET = "/tmp/unicorn_#{APP_PORT}.sock"
working_directory(APP_PATH)
listen(APP_SOCKET, :backlog => 64)
listen(APP_PORT, :tcp_nopush => true)
pid(File.join(APP_PATH ,APP_PID_PATH))
worker_processes(3)
timeout(30)
preload_app(true)
if GC.respond_to?(:copy_on_write_friendly=)
GC.copy_on_write_friendly = true
end
check_client_connection(false)
before_exec do |server|
if defined?(Dotenv)
env = Dotenv::Environment.new('.env')
env.each { |k, v| ENV[k] = v }
end
end
before_fork do |server, worker|
if defined?(ActiveRecord::Base)
ActiveRecord::Base.connection.disconnect!
end
old_pid = "#{server.config[:pid]}.oldbin"
if old_pid != server.pid
begin
sig = (worker.nr + 1) >= server.worker_processes ? :QUIT : :TTOU
Process.kill(sig, File.read(old_pid).to_i)
rescue Errno::ENOENT, Errno::ESRCH
end
end
end
after_fork do |server, worker|
if defined?(ActiveRecord::Base)
ActiveRecord::Base.establish_connection
end
end
#!/bin/bash
set -e
usage() {
echo "Usage: unicron [options]"
echo "runs unicorn in a webrick like way"
echo
echo "He is known as the Dark God, the Destroyer,"
echo "the Chaos Bringer, the Planet Eater, and he"
echo "is dedicated to consuming the universe"
echo
echo "-p,--port : specify the port to use"
echo "-h,--help : display this message"
}
PORT="80$(($RANDOM % 89 + 10))"
APP_NAME="unicorn_$PORT"
while test -n "$1"; do
case "$1" in
-h|--help)
usage
exit 0
;;
-p|--port)
PORT=$2
shift 2;
;;
*)
esac
done
onexit() {
echo "ᕕ(ಠ_ಠ)ᕗ ᕕ(ಠ_ಠ)ᕗ ᕕ(ಠ_ಠ)ᕗ ᕕ(ಠ_ಠ)ᕗ ᕕ(ಠ_ಠ)ᕗ ᕕ(ಠ_ಠ)ᕗ"
echo " Stopping Unicorn"
echo "ᕕ(ಠ_ಠ)ᕗ ᕕ(ಠ_ಠ)ᕗ ᕕ(ಠ_ಠ)ᕗ ᕕ(ಠ_ಠ)ᕗ ᕕ(ಠ_ಠ)ᕗ ᕕ(ಠ_ಠ)ᕗ"
sleep 1
}
trap onexit SIGINT SIGTERM EXIT
export PORT=$PORT
echo "ᕕ(ᐛ)ᕗ ᕕ(ᐛ)ᕗ ᕕ(ᐛ)ᕗ ᕕ(ᐛ)ᕗ ᕕ(ᐛ)ᕗ ᕕ(ᐛ)ᕗ ᕕ(ᐛ)ᕗ ᕕ(ᐛ)ᕗ"
echo " Launching Unicorn"
echo " on port $PORT"
echo "ᕕ(ᐛ)ᕗ ᕕ(ᐛ)ᕗ ᕕ(ᐛ)ᕗ ᕕ(ᐛ)ᕗ ᕕ(ᐛ)ᕗ ᕕ(ᐛ)ᕗ ᕕ(ᐛ)ᕗ ᕕ(ᐛ)ᕗ"
bundle exec unicorn -c ~/bin/assets/unicorn.rb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment