Skip to content

Instantly share code, notes, and snippets.

@shmatov
Created November 15, 2012 13:17

Revisions

  1. shmatov revised this gist Nov 20, 2012. 1 changed file with 3 additions and 6 deletions.
    9 changes: 3 additions & 6 deletions mina.rb
    Original file line number Diff line number Diff line change
    @@ -88,10 +88,7 @@
    # ------------------------------------------------------------------------------
    desc "Restart unicorn using 'upgrade'"
    task :restart => :environment do
    queue 'echo "-----> Restart Unicorn"'
    queue! %{
    test -s "#{unicorn_pid}" && kill -HUP `cat "#{unicorn_pid}"` && echo "Reload Ok" && exit 0
    #{start_unicorn}
    }
    invoke 'unicorn:stop'
    invoke 'unicorn:start'
    end
    end
    end
  2. shmatov revised this gist Nov 15, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion mina.rb
    Original file line number Diff line number Diff line change
    @@ -50,7 +50,7 @@
    invoke 'deploy:link_shared_paths'

    to :launch do
    invoke :'unicorn:start'
    invoke :'unicorn:restart'
    end
    end
    end
  3. shmatov created this gist Nov 15, 2012.
    7 changes: 7 additions & 0 deletions deploy.rake
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    # lib/tasks/deploy.rake
    namespace :deploy do
    desc 'Deploy to staging environment'
    task :staging do
    exec 'mina deploy -f config/deploy/staging.rb'
    end
    end
    97 changes: 97 additions & 0 deletions mina.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,97 @@
    # config/deploy/staging.rb
    require 'mina/bundler'
    require 'mina/rails'
    require 'mina/git'
    require 'mina/rvm'

    # Config
    # ==============================================================================
    set :term_mode, :system
    set :rails_env, 'staging'

    set :domain, '50.56.204.208'
    set :port, 37894

    set :deploy_to, "/home/apps/mfp/#{rails_env}"
    set :app_path, "#{deploy_to}/#{current_path}"

    set :repository, 'git@goup.unfuddle.com:goup/mfp.git'
    set :brach, 'master'

    set :user, 'apps'
    set :shared_paths, ['public/static', 'tmp']
    set :keep_releases, 5

    # RVM
    # ==============================================================================
    set :rvm_path, '/usr/local/rvm/scripts/rvm'

    task :environment do
    invoke 'rvm:use[1.9.3]'
    end

    # Setup task
    # ==============================================================================
    task :setup do
    queue! %{
    mkdir -p "#{deploy_to}/shared/tmp/pids"
    }
    end

    # Deploy task
    # ==============================================================================
    desc "deploys the current version to the server."
    task :deploy => :environment do
    deploy do
    invoke 'git:clone'
    invoke 'bundle:install'
    invoke 'rails:db_migrate'
    invoke 'rails:assets_precompile'
    invoke 'deploy:link_shared_paths'

    to :launch do
    invoke :'unicorn:start'
    end
    end
    end


    # Unicorn
    # ==============================================================================
    namespace :unicorn do
    set :unicorn_pid, "#{app_path}/tmp/pids/unicorn.pid"
    set :start_unicorn, %{
    cd #{app_path}
    bundle exec unicorn -c #{app_path}/config/unicorn/#{rails_env}.rb -E #{rails_env} -D
    }

    # Start task
    # ------------------------------------------------------------------------------
    desc "Start unicorn"
    task :start => :environment do
    queue 'echo "-----> Start Unicorn"'
    queue! start_unicorn
    end

    # Stop task
    # ------------------------------------------------------------------------------
    desc "Stop unicorn"
    task :stop do
    queue 'echo "-----> Stop Unicorn"'
    queue! %{
    test -s "#{unicorn_pid}" && kill -QUIT `cat "#{unicorn_pid}"` && echo "Stop Ok" && exit 0
    echo >&2 "Not running"
    }
    end

    # Restart task
    # ------------------------------------------------------------------------------
    desc "Restart unicorn using 'upgrade'"
    task :restart => :environment do
    queue 'echo "-----> Restart Unicorn"'
    queue! %{
    test -s "#{unicorn_pid}" && kill -HUP `cat "#{unicorn_pid}"` && echo "Reload Ok" && exit 0
    #{start_unicorn}
    }
    end
    end
    29 changes: 29 additions & 0 deletions unicorn.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    # config/unicorn/staging.rb
    app_path = "/home/apps/mfp/staging/current"

    worker_processes 1
    preload_app true
    timeout 180
    listen '127.0.0.1:9021'
    user 'apps', 'apps'
    working_directory app_path
    pid "#{app_path}/tmp/pids/unicorn.pid"
    stderr_path "log/unicorn.log"
    stdout_path "log/unicorn.log"

    before_fork do |server, worker|
    ActiveRecord::Base.connection.disconnect!

    old_pid = "#{server.config[: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

    after_fork do |server, worker|
    ActiveRecord::Base.establish_connection
    end