From db0d28107b80adc505ca9dd19df20c8aa888ab81 Mon Sep 17 00:00:00 2001 From: julian Date: Fri, 24 Feb 2017 00:18:22 +0630 Subject: [PATCH] update environments/production.rb add unicorn.rb, nemo.sh --- Gemfile | 1 + config/deploy.rb | 2 +- config/deploy/production.rb.save | 61 +++++++++++++++++++++++++++ config/environments/production.rb | 4 +- config/nemo.sh | 69 +++++++++++++++++++++++++++++++ config/unicorn.rb | 39 +++++++++++++++++ 6 files changed, 173 insertions(+), 3 deletions(-) create mode 100644 config/deploy/production.rb.save create mode 100644 config/nemo.sh create mode 100644 config/unicorn.rb diff --git a/Gemfile b/Gemfile index abbc6c5..4fb97ed 100644 --- a/Gemfile +++ b/Gemfile @@ -18,6 +18,7 @@ gem 'coffee-rails', '~> 4.2' # gem 'therubyracer', platforms: :ruby gem 'unicorn' gem 'execjs' +gem 'rack', '~> 2.0.0' gem 'therubyracer' platform :ruby do gem 'pg' diff --git a/config/deploy.rb b/config/deploy.rb index 26dc14f..5ec9067 100644 --- a/config/deploy.rb +++ b/config/deploy.rb @@ -21,7 +21,7 @@ set :keep_releases, 5 set :linked_files, %w{config/database.yml config/secrets.yml} # dirs we want symlinking to shared -set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system} +set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system pids sockets} # what specs should be run before deployment is allowed to # continue, see lib/capistrano/tasks/run_tests.cap diff --git a/config/deploy/production.rb.save b/config/deploy/production.rb.save new file mode 100644 index 0000000..140fc76 --- /dev/null +++ b/config/deploy/production.rb.save @@ -0,0 +1,61 @@ +# server-based syntax +# ====================== +# Defines a single server with a list of roles and multiple properties. +# You can define all roles on a single server, or split them: + +# server "example.com", user: "deploy", roles: %w{app db web}, my_property: :my_value +# server "example.com", user: "deploy", roles: %w{app web}, other_property: :other_value +# server "db.example.com", user: "deploy", roles: %w{db} +/deploy/production.rb: + + +# role-based syntax +# ================== + +# Defines a role with one or multiple servers. The primary server in each +# group is considered to be the first unless any hosts have the primary +# property set. Specify the username and a domain or IP for the server. +# Don't use `:all`, it's a meta role. + +# role :app, %w{deploy@example.com}, my_property: :my_value +# role :web, %w{user1@primary.com user2@additional.com}, other_property: :other_value +# role :db, %w{deploy@example.com} + + + +# Configuration +# ============= +# You can set any configuration variable like in config/deploy.rb +# These variables are then only loaded and set in this stage. +# For available Capistrano configuration variables see the documentation page. +# http://capistranorb.com/documentation/getting-started/configuration/ +# Feel free to add new variables to customise your setup. + + + +# Custom SSH Options +# ================== +# You may pass any option but keep in mind that net/ssh understands a +# limited set of options, consult the Net::SSH documentation. +# http://net-ssh.github.io/net-ssh/classes/Net/SSH.html#method-c-start +# +# Global options +# -------------- +# set :ssh_options, { +# keys: %w(/home/rlisowski/.ssh/id_rsa), +# forward_agent: false, +# auth_methods: %w(password) +# } +# +# The server-based syntax can be used to override options: +# ------------------------------------ +# server "example.com", +# user: "user_name", +# roles: %w{web app}, +# ssh_options: { +# user: "user_name", # overrides user setting above +# keys: %w(/home/user_name/.ssh/id_rsa), +# forward_agent: false, +# auth_methods: %w(publickey password) +# # password: "please use keys" +# } diff --git a/config/environments/production.rb b/config/environments/production.rb index 151e852..7817a04 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -23,7 +23,7 @@ Rails.application.configure do # config.assets.css_compressor = :sass # Do not fallback to assets pipeline if a precompiled asset is missed. - config.assets.compile = false + config.assets.compile = true # `config.assets.precompile` and `config.assets.version` have moved to config/initializers/assets.rb @@ -55,7 +55,7 @@ Rails.application.configure do # Use a real queuing backend for Active Job (and separate queues per environment) # config.active_job.queue_adapter = :resque # config.active_job.queue_name_prefix = "nemo_encoder_#{Rails.env}" - config.action_mailer.perform_caching = false + config.action_mailer.perform_caching = true # Ignore bad email addresses and do not raise email delivery errors. # Set this to true and configure the email server for immediate delivery to raise delivery errors. diff --git a/config/nemo.sh b/config/nemo.sh new file mode 100644 index 0000000..3a84751 --- /dev/null +++ b/config/nemo.sh @@ -0,0 +1,69 @@ +#!/bin/sh +### BEGIN INIT INFO +# Provides: unicorn +# Required-Start: $all +# Required-Stop: $all +# Default-Start: 2 3 4 5 +# Default-Stop: 0 1 6 +# Short-Description: starts the unicorn app server +# Description: starts unicorn using start-stop-daemon +### END INIT INFO + +set -e +USAGE="Usage: $0 " + +# app settings +USER="ubuntu" +APP_NAME="nemo_encoder_production" +APP_ROOT="/home/$USER/apps/$APP_NAME/current" +ENV="production" + +# environment settings +PATH="/home/$USER/.rbenv/shims:/home/$USER/.rbenv/bin:$PATH" +CMD="/home/$USER/.rbenv/shims/unicorn_rails -c $APP_ROOT/config/unicorn.rb -E $ENV -D" +PID="$APP_ROOT/pids/unicorn.nemo.pid" +OLD_PID="$PID.oldbin" + + +cd $APP_ROOT || exit 1 + +sig () { + test -s "$PID" && kill -$1 `cat $PID` +} + +oldsig () { + test -s $OLD_PID && kill -$1 `cat $OLD_PID` +} + + +case $1 in + start) + sig 0 && echo >&2 "Already running" && exit 0 + echo "Starting $APP_NAME" + su - $USER -c "$CMD" + ;; + stop) + echo "Stopping $APP_NAME" + sig QUIT && exit 0 + echo >&2 "Not running" + ;; + force-stop) + echo "Force stopping $APP_NAME" + sig TERM && exit 0 + echo >&2 "Not running" + ;; + restart|reload|upgrade) + sig USR2 && echo "reloaded $APP_NAME" && exit 0 + echo >&2 "Couldn't reload, starting '$CMD' instead" + $CMD + ;; + rotate) + sig USR1 && echo rotated logs OK && exit 0 + echo >&2 "Couldn't rotate logs" && exit 1 + ;; + *) + echo >&2 $USAGE + exit 1 + ;; +esac + diff --git a/config/unicorn.rb b/config/unicorn.rb new file mode 100644 index 0000000..ba6070d --- /dev/null +++ b/config/unicorn.rb @@ -0,0 +1,39 @@ +root=File.expand_path(File.dirname(__FILE__) + '/..') +#shared_dir="#{root}/shared" +working_directory root +pid "#{root}/pids/unicorn.nemo.pid" +stderr_path "#{root}/log/unicorn.stderr.log" +stdout_path "#{root}/log/unicorn.stdout.log" +listen "#{root}/sockets/unicorn.nemo.sock", :backlog => 64 +listen(8081, backlog: 64) if ENV['RAILS_ENV'] == 'production' +worker_processes 2 +preload_app true +timeout 30 + + +before_fork do |server, worker| + # the following is highly recomended for Rails + "preload_app true" + # as there's no need for the master process to hold a connection + defined?(ActiveRecord::Base) and ActiveRecord::Base.connection.disconnect! + + ## + # When sent a USR2, Unicorn will suffix its pidfile with .oldbin and + # immediately start loading up a new version of itself (loaded with a new + # version of our app). When this new Unicorn is completely loaded + # it will begin spawning workers. The first worker spawned will check to + # see if an .oldbin pidfile exists. If so, this means we've just booted up + # a new Unicorn and need to tell the old one that it can now die. To do so + # we send it a QUIT. + # + # Using this method we get 0 downtime deploys. + + old_pid = "#{root}/pids/unicorn.nemo.pid.oldbin" + if File.exists?(old_pid) && server.pid != old_pid + begin + Process.kill("QUIT", File.read(old_pid).to_i) + rescue Errno::ENOENT, Errno::ESRCH + # someone else did our job for us + end + end +end +