Delayed job wont start using Capistrano - ruby-on-rails

I cannot start delayed job process using a capistrano recipe. Here's the error I am getting.
/usr/local/lib/ruby/gems/1.9.1/gems/delayed_job-2.1.1/lib/delayed/command.rb:62:in `mkdir': File exists - /my_app/server/releases/20101120001612/tmp/pids (Errno::EEXIST)
Here's the capistrano code (NOTE-: I have tried both start/restart commands)
after "deploy:restart", "delayed_job:start"
task :start, :roles => :app do
run "cd #{current_path}; RAILS_ENV=#{rails_env} script/delayed_job -n 2 start"
end
More detail errors from deployment logs -
executing command
[err :: my_server] /usr/local/lib/ruby/gems/1.9.1/gems/delayed_job-2.1.1/lib/delayed/command.rb:62:in `mkdir': File exists - /my_app/server/releases/20101120001612/tmp/pids (Errno::EEXIST)
[err :: my_server] from /usr/local/lib/ruby/gems/1.9.1/gems/delayed_job-2.1.1/lib/delayed/command.rb:62:in `daemonize'
[err :: my_server] from script/delayed_job:5:in `<main>'
command finished
failed: "sh -c 'cd /my_app/server/current; RAILS_ENV=production script/delayed_job -n 3 restart'" on myserevr
This is a Rails 3 app (v3.0.3)

Seeing the same problem.
It turns out I was missing the ~/apps/application_name/shared/pids directory.
Finally creating it made this problem go away.
No need to set up custom dj_pids directory.

I also got this error and found a couple of issues:
Ensure you have a shared/pids folder.
Ensure you have the correct hooks setup
Your deploy.rb script should contain:
require "delayed/recipes"
after "deploy:stop", "delayed_job:stop"
after "deploy:start", "delayed_job:start"
after "deploy:restart", "delayed_job:restart"
I'd copied the hooks from an old post and they appear to be incorrect now. These are from the actual delayed_job recipe file comments.
I believe cap deploy:setup should create the pids folder but I set things up a different way and it was not created. app/current/tmp/pids links to app/shared/pids and this was causing the false directory exists error.

This is how I fixed the issue, I passed an explicit pids dir parameter using "--pid-dir". Not sure if this is perfect, but it worked.
task :restart, :roles => :app do
run "cd #{current_path}; RAILS_ENV=#{rails_env} script/delayed_job -n #{dj_proc_count} --pid-dir=#{app_root}/shared/dj_pids restart"
end

Add the creation of this directory before
after "deploy:restart", "delayed_job:start"
task :start, :roles => :app do
run "mkdir #{current_path}/tmp/pids"
run "cd #{current_path}; RAILS_ENV=#{rails_env} script/delayed_job -n 2 start"
end

I had the same issue. Turned out that there was an existing
application_name/shared/pids/delayed_job.main.pid
file, which had incorrect owner permissions, which was causing the deployment to fail. Fixing this file's permissions solved the issue for me.

Since the creation of the directories is cheap and fast, use the following callback:
before 'deploy', 'deploy:setup'
This will ensure that structure is always there before each deploy.

Related

"missing argument -c" when restarting Unicorn via Mina

I am trying to deploy my app to my server using Mina and I need the server to be restarted automatically. But unfortunately this doesn't work and I don't know why. Here is what I am trying:
require 'mina/bundler'
require 'mina/rvm'
require 'mina/rails'
require 'mina/git'
...
set :unicorn_conf, "#{shared_path}/config/unicorn.rb"
set :unicorn_pid, "#{deploy_to}/current/tmp/pids/unicorn.pid"
...
task :environment do
invoke :'rvm:use[ruby-2.2.3]'
end
task deploy: :environment do
deploy do
# Put things that prepare the empty release folder here.
# Commands queued here will be run on a new release directory.
invoke :'git:clone'
invoke :'deploy:link_shared_paths'
invoke :'bundle:install'
invoke :'rails:db_migrate'
invoke :'rails:assets_precompile'
invoke :restart_server
end
end
task :restart_server do
if File.exists? unicorn_pid
queue 'kill `cat #{unicorn_pid}`'
end
queue 'bundle exec unicorn -c #{deploy_to}/#{unicorn_conf} -E production -D'
puts "bundle exec unicorn -c #{deploy_to}/#{:unicorn_conf} -E production -D"
end
This last puts statement, I put it just to debug and it prints the string I want. But I still got this error:
/home/webuser/tmpcms/tmp/build-145333668721611/vendor/bundle/ruby/2.2.0/gems/unicorn-5.0.1/bin/unicorn:110:in `block in <top (required)>': missing argument: -c (OptionParser::MissingArgument)
from /home/webuser/tmpcms/tmp/build-145333668721611/vendor/bundle/ruby/2.2.0/gems/unicorn-5.0.1/bin/unicorn:10:in `new'
from /home/webuser/tmpcms/tmp/build-145333668721611/vendor/bundle/ruby/2.2.0/gems/unicorn-5.0.1/bin/unicorn:10:in `<top (required)>'
from /home/webuser/tmpcms/tmp/build-145333668721611/vendor/bundle/ruby/2.2.0/bin/unicorn:23:in `load'
from /home/webuser/tmpcms/tmp/build-145333668721611/vendor/bundle/ruby/2.2.0/bin/unicorn:23:in `<main>'
! bash: line 209: log: command not found
! ERROR: Deploy failed.
I don't know what causes it, can you help me with it?
UPD: It seems to be something with the variable substitution and fetch function, but I still can't understand what is wrong. Here is what I've tested:
task :restart_server => :environment do
if File.exists? unicorn_pid
queue 'kill `cat #{unicorn_pid}`'
end
queue 'cd /home/webuser/tmpcms/current; pwd; bundle exec unicorn -c #{deploy_to}/#{unicorn_conf} -E production -D'
end
bundle exec part doesn't work, it doesn't execute it and prints Connection closed instead. The same thing if I will replace the path in the cd command with cd #{deploy_to} or to cd #{fetch(:deploy_to)}
Oh my, the answer was so easy.
I forgot that I was using single quotes instead of double quotes, and the variables weren't substituted there needed.
Just replacing single quotes with double quotes solved it.

bundler: not executable: script/delayed_job

I'm trying to run the following command on my remote server (either via capistrano or ssh):
bundle exec RAILS_ENV=production script/delayed_job start
But I'm getting this error message: bundler: not executable: script/delayed_job
Never saw this before, and google had nothing for me. Any idea what might be the problem?
Maybe it does not have permissions to run? Try running this command
chmod +x script/delayed_job
and then executing the file again.
I am not sure if if it is a fundamental misunderstanding of the capistrano rbenv gem or some issue with the gem itself, but I had similar issue with delayed_job, where the bin/delayed_job file just would not get the executable permission when copied to the server by capistrano. So I wrote a task which I had run before invoking the delayed_job:restart task.
Note - Adding this answer because earlier one is from 2014, and also I wanted to show how to add the task, so the permission change can happen during deployment itself.
Created a task in lib/capistrano/tasks folder (in namespace delayed_job):
namespace :delayed_job do
desc 'Ensure that bin/delayed_job has the permission to be executable. Ideally, this should not have been needed.'
task :ensure_delayed_job_executable do
on roles(delayed_job_roles) do
within release_path do
execute :chmod, :'u+x', :'bin/delayed_job'
end
end
end
end
after 'deploy:publishing', 'deploy:restart'
namespace :deploy do
task :restart do
invoke 'delayed_job:ensure_delayed_job_executable'
invoke 'delayed_job:restart'
end
end

Capistrano 3 recipe for dynamic_sitemaps

I'm trying to "translate" the Capistrano recipe to deploy dynamic_sitemaps to work with Capistrano 3.
The snippet suggested in the readme looks like this:
after "deploy:update_code", "sitemaps:create_symlink"
namespace :sitemaps do
task :create_symlink, roles: :app do
run "mkdir -p #{shared_path}/sitemaps"
run "rm -rf #{release_path}/public/sitemaps"
run "ln -s #{shared_path}/sitemaps #{release_path}/public/sitemaps"
end
end
But this doesn't work with Capistrano 3. I pasted this code into config/deploy.rb and the first error I got was: Don't know how to build task 'sitemaps:create_symlink'`.
I read somewhere that in Capistrano 3 the namespaces have to be defined before the calls so I reversed the order of the blocks, defining the namespace first and having the after call last. I got NoMethodError: undefined method `map' for :roles:Symbol`.
So I rewrote the namespace block to:
namespace :sitemaps do
task :create_symlink do
on roles(:web) do
run "mkdir -p #{shared_path}/sitemaps"
run "rm -rf #{release_path}/public/sitemaps"
run "ln -s #{shared_path}/sitemaps #{release_path}/public/sitemaps"
end
end
end
And now I'm getting Don't know how to build task 'deploy:update_code' and I'm at loss.
While I couldn't solve precisely the issue I posted above, the solution is actually very simple. If using Capistrano 3 just add public/sitemaps to your :linked_dirs setting as such:
set :linked_dirs, %w{bin log tmp vendor/bundle public/system public/sitemaps}
This will create a symbolic link between #{release_path}/public/sitemaps and #{shared_path}/public/sitemap creating the latter if needed.

Problems deploying with Capistrano (symlink error)

When trying to deploy to my staging server I`m getting an error when what it seems like the moment at which Capistrano is trying to create a database.yml file.
Currently I have the database.yml file in my gitIgnore file (I`ve tried with it included).
I've never had this problem before so I don`t really know how to go about debugging this issue.
Any help is deeply appreciated.
Deployment Log:
executing `deploy:symlink'
* executing "rm -f /home/deploy/example.com/current && ln -s /home/deploy/example.com/releases/20130219164028 /home/deploy/example.com/current"
servers: ["example.com"]
[example.com] executing command
command finished in 1893ms
triggering after callbacks for `deploy:symlink'
* executing `deploy:copy_database_yml'
* executing "ln -s /home/deploy/example.com/shared/data/development /home/deploy/example.com/releases/20130219164028/solr/data/live"
servers: ["example.com"]
[example.com] executing command
command finished in 1867ms
* executing "ln -s /home/deploy/example.com/shared/config/database.yml /home/deploy/example.com/releases/20130219164028/config/database.yml"
servers: ["example.com"]
[example.com] executing command
** [out :: example.com] ln: creating symbolic link `/home/deploy/example.com/releases/20130219164028/config/database.yml'
** [out :: example.com] : File exists
command finished in 2086ms
*** [deploy:symlink] rolling back
*** no previous release to rollback to, rollback of symlink skipped
*** [deploy:update_code] rolling back
* executing "rm -rf /home/deploy/example.com/releases/20130219164028; true"
servers: ["example.com"]
[example.com] executing command
command finished in 2640ms
failed: "env PATH=/home/deploy/example.com/bin:$PATH GEM_HOME=/home/deploy/example.com/gems sh -c 'ln -s /home/deploy/example.com/shared/config/database.yml /home/deploy/example.com/releases/20130219164028/config/database.yml'" on example.com
The contents of my deploy.rb file
set :stages, %w(staging integration live)
set :default_stage, "staging"
require 'capistrano/ext/multistage'
require "bundler/capistrano"
default_run_options[:pty] = true
set :application, "Example"
set :use_sudo, false
set :keep_releases, 5
# If you aren't using Subversion to manage your source code, specify
# your SCM below:
#set :people, 'root'
#set :ssh_options, { :forward_agent => true }
namespace :deploy do
desc "Restarting mod_rails with restart.txt"
task :restart, :roles => :app, :except => {:no_release => true} do
run "touch #{current_path}/tmp/restart.txt"
end
[:start, :stop].each do |t|
desc "#{t} task is a no-op with mod_rails"
task t, :roles => :app do
;
end
end
# Avoid keeping the database.yml configuration in git.
desc "task to create a symlink for the database files."
task :copy_database_yml do
run "ln -s #{shared_path}/data/development #{release_path}/solr/data/live"
run "ln -s #{shared_path}/config/database.yml #{release_path}/config/database.yml"
run "rm -rf #{release_path}/public/photos"
run "ln -s #{shared_path}/public/photos #{release_path}/public/photos"
run "rm -rf #{release_path}/public/hobby_photos"
run "ln -s #{shared_path}/public/hobby_photos #{release_path}/public/hobby_photos"
run "chmod 777 -R #{release_path}"
#run "cd #{release_path}; bundle exec rake db:migrate RAILS_ENV=staging"
# run "cd #{release_path}/ & rake db:migrate RAILS_ENV=staging"
end
end
after "deploy:symlink", "deploy:copy_database_yml"
It looks like you have answer right in front of your eyes.
** [out :: example.com] ln: creating symbolic link `/home/deploy/example.com/releases/20130219164028/config/database.yml'
** [out :: example.com] : File exists
Modify your deploy:copy_database_yml to remove symbolic link first, or to create it only if it doesn't exist.
BTW this task does much more than its name suggest, which is bad.
It seems to be a problem of Capistrano when creating the symbolic links.
Try to comment the roles in your deploy.rb file:
#role :app, %w{deploy#yourhost.com}
#role :web, %w{deploy#yourhost.com}
#role :db, %w{deploy#yourhost.com}

Whenever Gem Fails to Run "bundle exec" Correctly in Capistrano

I'm having problems deploying the Whenever gem to my production environment using Capistrano.
The problem is stemming from a bundle exec whenever command that's triggering some 'missing gem' issues (yet running bundle install from the shell shows that everything is in fact there!).
My feeling is that one of two things is happening: that Bundler isn't fully loading before bundle exec is being called, or that somehow there's a path issue that's running it in the wrong place.
Here's what's going on:
* executing `deploy'
* executing `deploy:update'
** transaction: start
* executing `deploy:update_code'
executing locally: "git ls-remote git#my-source-repository:mysource.git HEAD"
command finished in 1847ms
* executing "git clone -q git#my-source-repository:mysource.git /domains/myapp/releases/20130124211036 && cd /domains/myapp/releases/20130124211036 && git checkout -q -b deploy 90238bbcb993e3e7df2374ffaa13e7ed701c202e && (echo 90238bbcb993e3e7df2374ffaa13e7ed701c202e > /domains/myapp/releases/20130124211036/REVISION)"
servers: ["myip"]
[myip] executing command
** [myip :: out] Enter passphrase for key '/home/deploy/.ssh/id_rsa':
** [myip :: out]
command finished in 9294ms
* executing `deploy:finalize_update'
triggering before callbacks for `deploy:finalize_update'
* executing `whenever:update_crontab'
* executing "cd /domains/myapp/releases/20130124211036 && bundle exec whenever --update-crontab My App --set environment=production --roles db"
servers: ["myip"]
[myip] executing command
** [out :: myip] Could not find carrierwave-0.5.8 in any of the sources
** [out :: myip] Run `bundle install` to install missing gems.
command finished in 1967ms
*** [deploy:update_code] rolling back
* executing "rm -rf /domains/myapp/releases/20130124211036; true"
servers: ["myip"]
[myip] executing command
command finished in 998ms
failed: "rvm_path=/usr/local/rvm /usr/local/rvm/bin/rvm-shell '1.9.3' -c 'cd /domains/myapp/releases/20130124211036 && bundle exec whenever --update-crontab My App --set environment=production --roles db'" on myip
You can see that the failure is coming from this line:
bundle exec whenever --update-crontab
Which is in turn being run by my Capistrano deploy.rb.
# Your Application's "Name"
set :application, [My App]
# The URL to your application's repository
set :repository, [repo]
set :scm, :git
set :scm_passphrase, [password]
# The user you are using to deploy with (This user should have SSH access to your server)
set :user, "deploy"
# We want to deploy everything under your user, and we don't want to use sudo
set :use_sudo, false
# Where to deploy your application to.
set :deploy_to, "/domains/myapp/"
# Load RVM's capistrano plugin.
require "rvm/capistrano"
set :rvm_ruby_string, '1.9.3'
set :rvm_type, :system # use system-wide RVM
# Require that we use bundler to install necessary gem dependencies
require "bundler/capistrano"
set :whenever_command, "bundle exec whenever"
require "whenever/capistrano"
# -------------------------------- Server Definitions --------------------------------
# Define the hostname of your server. If you have multiple servers for multiple purposes, we can define those below as well.
set :server_name, [server]
# We're assuming you're using a single server for your site, but if you have a seperate asset server or database server, you can specify that here.
role :app, server_name
role :web, server_name
role :db, server_name, :primary => true
# -------------------------------- Final Config --------------------------------
# This configuration option is helpful when using svn+ssh but doesn't hurt anything to leave it enabled always.
default_run_options[:pty] = true
namespace :deploy do
desc "Tell Passenger to restart the app."
task :restart do
run "touch #{deploy_to}current/tmp/restart.txt"
end
task :start do
run "cd #{deploy_to} && ln -s current/public public_html"
run "touch #{deploy_to}current/tmp/restart.txt"
end
task :symlink do
run "cd #{deploy_to} && rm current ; ln -s releases/#{release_name} current"
end
task :stop do
run "rm #{deploy_to}public_html"
end
namespace :web do
task :disable do
run "cd #{deploy_to} && rm public_html && ln -s static_site public_html"
end
task :enable do
run "cd #{deploy_to} && rm public_html && ln -s current/public public_html"
end
end
desc "Symlink shared configs and folders on each release."
task :symlink_shared do
run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml"
run "ln -nfs #{shared_path}/assets #{release_path}/public/assets"
# create symlink to uploads folder for Carrierwave resources (i.e., book cover images)
run "ln -nfs #{shared_path}/uploads #{release_path}/public/uploads"
end
desc "Sync the public/assets directory."
task :assets do
system "rsync -vr --exclude='.DS_Store' public/assets #{user}##{application}:#{shared_path}/"
end
end
after "deploy", "deploy:migrate"
after 'deploy:update_code', 'deploy:symlink_shared'
after(:setup) do
run "rm -rf #{deploy_to}public_html"
run "mkdir #{deploy_to}static_site"
end
Now, the error that I'm getting about CarrierWave is bogus - I've been using CarrierWave for ages - and the issue clearly stems (as you can see by the command that triggers the error) from either Whenever itself or Bundler.
And I'm thinking that it actually may have something to do with paths related to RVM. At any rate, the bundle exec whenever isn't working.
Any ideas?
It turns out there were a couple problems, both of which needed addressing:
Whenever ~> 0.8 introduced a bug with Capistrano roles
The Whenever cap task was firing before bundle:install (this is confirmed in your code above)
See issues here: https://github.com/javan/whenever/issues/275 and here: https://github.com/javan/whenever/issues/269
To fix the problem, you most likely need to
Update bundler on your server (-v 1.2.3)
Update bundler locally
Update to the newest Whenever (-v 0.8.2)
#ideaoforder's answer helped me too, previously though I had the same trouble caused by require 'whenever/capistrano' being on the top of the config/deploy.rb followed by require 'bundler/capistrano'. Swapping those two helped.
I have require 'bundler/capistrano' always on top of the deploy script as a rule.

Resources