J
J
Jeket2013-11-21 17:02:22
Ruby on Rails
Jeket, 2013-11-21 17:02:22

How to deploy unicorn via capistrano 3?

Good evening.
Trying to deploy a site, my script is capistrano.
gems

gem "capistrano", "~> 3.0.1"
gem "capistrano-rvm", "~> 0.0.3"
gem "capistrano-bundler", "~> 1.0.0"
gem "capistrano-rails", "~> 1.1.0"

gem "unicorn", "~> 4.7.0"

config/unicorn.rb
project_name = 'my_project'
root = "/var/www/#{ project_name }/current"
working_directory root
pid "#{root}/tmp/pids/unicorn.pid"
stderr_path "#{root}/log/unicorn.stderr.log"
stdout_path "#{root}/log/unicorn.stdout.log"

listen "#{root}/tmp/sockets/unicorn.sock"
worker_processes 2
timeout 30

config/deploy.rb
set :application, 'my_project'
set :repo_url, '[email protected]*****************.git'

set :deploy_to, '/var/www/my_project'
set :scm, :git
set :branch, "trunk"


set :linked_files, %w{config/database.yml}
set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system}

set :rvm_type, :system
set :rvm_ruby_version, '[email protected]_gemset'

set :keep_releases, 10

set :rails_env, "production"

namespace :deploy do
  desc 'Restart application'
  task :restart do
    invoke 'deploy:unicorn:restart'
  end

  after :finishing, 'deploy:cleanup'




  namespace :unicorn do
    pid_path = "#{release_path}/tmp/pids"
    unicorn_pid = "#{pid_path}/unicorn.pid"

    def run_unicorn
      execute "cd #{current_path} ; bundle exec unicorn_rails -E #{fetch(:rails_env)}"
    end

    desc 'Start unicorn'
    task :start do
      on roles(:app) do
        run_unicorn
      end
    end

    desc 'Stop unicorn'
    task :stop do
      on roles(:app) do
        if test "[ -f #{unicorn_pid} ]"
          execute :kill, "-QUIT `cat #{unicorn_pid}`"
        end
      end
    end

    desc 'Force stop unicorn (kill -9)'
    task :force_stop do
      on roles(:app) do
        if test "[ -f #{unicorn_pid} ]"
          execute :kill, "-9 `cat #{unicorn_pid}`"
          execute :rm, unicorn_pid
        end
      end
    end

    desc 'Restart unicorn'
    task :restart do
      on roles(:app) do
        if test "[ -f #{unicorn_pid} ]"
          execute :kill, "-USR2 `cat #{unicorn_pid}`"
        else
          run_unicorn
        end
      end
    end
  end

end

I execute cap staging deploy and
get
Running cd /var/www/my_project/current ; bundle exec unicorn_rails -E production on 192.168.**.**
DEBUG [829b451b] Command: cd /var/www/my_project/current ; bundle exec unicorn_rails -E production
cap aborted!
cd /var/www/my_project/current ; bundle exec unicorn_rails -E production stdout: Nothing written
cd /var/www/my_project/current ; bundle exec unicorn_rails -E production stderr: Nothing written

In unicorn log file:
I, [2013-11-21T16:06:09.816444 #25171] INFO -- : Refreshing Gem list
I, [2013-11-21T16:06:11.380857 #25171] INFO -- : listening on addr= /var/www/my_project/shared/tmp/sockets/unicorn.socket fd=10
I, [2013-11-21T16:06:11.381253 #25171] INFO -- : worker=0 spawning...
I, [2013- 11-21T16:06:11.382852 #25171] INFO -- : worker=1 spawning...
I, [2013-11-21T16:06:11.383987 #25174] INFO -- : worker=0 spawned pid=25174
I, [ 2013-11-21T16:06:11.385567 #25171] INFO -- : master process ready
I, [2013-11-21T16:06:11.388761 #25174] INFO -- : worker=0 ready
I, [2013-11-21T16 :06:11.389872 #25177] INFO -- : worker=1 spawned pid=25177
I, [2013-11-21T16:06:11.392553 #25177] INFO -- : worker=1 ready

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nitrino, 2013-12-03
@Nitrino

I also suffered with this
1. Instead of unicorn_rails, you just need to use unicorn
2. Specifying the path to rvm also helped me

RAILS_ENV=#{fetch(:stage)} /usr/local/rvm/bin/rvm default do
before bundle
def run_unicorn
      execute cd #{release_path}  && ("RAILS_ENV=#{fetch(:stage)} /usr/local/rvm/bin/rvm default do bundle exec unicorn -E #{fetch(:rails_env)})"
end

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question