Error deploying Sidekiq to Ubuntu using Capistrano - ruby-on-rails

I'm following the directions here to integrate Sidekiq with Capistrano on Ubuntu: https://github.com/mperham/sidekiq/wiki/Deploying-to-Ubuntu
I'm having an issue with the Capistrano part.
I put this code at the end of deploy.rb:
# For capistrano 3
namespace :sidekiq do
task :quiet do
# Horrible hack to get PID without having to use terrible PID files
puts capture("kill -USR1 $(sudo initctl status workers | grep /running | awk '{print $NF}') || :")
end
task :restart do
execute :sudo, :initctl, :restart, :workers
end
end
after 'deploy:starting', 'sidekiq:quiet'
after 'deploy:reverted', 'sidekiq:restart'
after 'deploy:published', 'sidekiq:restart'
And I get this error when I run cap production deploy:
cap aborted!
NoMethodError: undefined method `capture' for main:Object
config/deploy.rb:67:in `block (2 levels) in <top (required)>'
/Users/user/.rvm/gems/ruby-2.1.2#project/gems/capistrano-3.2.1/lib/capistrano/dsl/task_enhancements.rb:12:in `block in after'
/Users/user/.rvm/gems/ruby-2.1.2#project/gems/capistrano-3.2.1/lib/capistrano/dsl.rb:15:in `invoke'
/Users/user/.rvm/gems/ruby-2.1.2#project/gems/capistrano-3.2.1/lib/capistrano/tasks/framework.rake:65:in `block (2 levels) in <top (required)>'
/Users/user/.rvm/gems/ruby-2.1.2#project/gems/capistrano-3.2.1/lib/capistrano/tasks/framework.rake:64:in `each'
/Users/user/.rvm/gems/ruby-2.1.2#project/gems/capistrano-3.2.1/lib/capistrano/tasks/framework.rake:64:in `block in <top (required)>'
/Users/user/.rvm/gems/ruby-2.1.2#project/gems/capistrano-3.2.1/lib/capistrano/application.rb:15:in `run'
/Users/user/.rvm/gems/ruby-2.1.2#project/gems/capistrano-3.2.1/bin/cap:3:in `<top (required)>'
/Users/user/.rvm/gems/ruby-2.1.2#project/bin/cap:23:in `load'
/Users/user/.rvm/gems/ruby-2.1.2#project/bin/cap:23:in `<main>'
/Users/user/.rvm/gems/ruby-2.1.2#project/bin/ruby_executable_hooks:15:in `eval'
/Users/user/.rvm/gems/ruby-2.1.2#project/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => sidekiq:quiet
(See full trace by running task with --trace)
The deploy has failed with an error: #<NoMethodError: undefined method `capture' for main:Object>
Obviously, it's the capture function that's undefined, but what is the problem? Did I put the code in the right file deploy.rb? Do I need to require something?

You have to specify which server to run the code on. For instance:
namespace :sidekiq do
task :quiet do
# Horrible hack to get PID without having to use terrible PID files
on roles(:app) do
puts capture("kill -USR1 $(sudo initctl status workers | grep /running | awk '{print $NF}') || :")
end
end
task :restart do
on roles(:app) do
execute :sudo, :initctl, :restart, :workers
end
end
end

Try to require the sidekiq gem, or take a look at capistrano with sidekiq integration

Related

Rails Tenant schema switching with background jobs not working

Setting up a scheduled rake task using Whenever gem.
Required to switch to a specific schema (Using Apartment gem in my project)
Code in config/schedule.rb
env :PATH, ENV['PATH']
env :GEM_PATH, ENV['GEM_PATH']
set :output, "#{Whenever.path}/log/scheduler.log"
every 1.minute do
rake "db:my_task"
end
Code in lib/tasks/my_task.rake
namespace :db do
task :my_task => :environment do
Apartment::Tenant.switch("subdomain") do
#My Code here
end
end
end
Produces the following error in logs log/scheduler.logs
rake aborted!
Apartment::TenantNotFound: One of the following schema(s) is invalid: "subdomain" "public"
/var/lib/gems/2.3.0/gems/apartment-1.2.0/lib/apartment/adapters/postgresql_adapter.rb:72:in `rescue in connect_to_new'
/var/lib/gems/2.3.0/gems/apartment-1.2.0/lib/apartment/adapters/postgresql_adapter.rb:65:in `connect_to_new'
/var/lib/gems/2.3.0/gems/apartment-1.2.0/lib/apartment/adapters/abstract_adapter.rb:91:in `block in switch!'
/var/lib/gems/2.3.0/gems/activesupport-5.1.3/lib/active_support/callbacks.rb:97:in `run_callbacks'
/var/lib/gems/2.3.0/gems/apartment-1.2.0/lib/apartment/adapters/abstract_adapter.rb:88:in `switch!'
/var/lib/gems/2.3.0/gems/apartment-1.2.0/lib/apartment/adapters/abstract_adapter.rb:105:in `switch'
/home/user1/Desktop/SPERICORN/kidversity/lib/tasks/dynamic_age_setter.rake:4:in `block (2 levels) in <top (required)>'
/var/lib/gems/2.3.0/gems/rake-12.0.0/exe/rake:27:in `<top (required)>'
ActiveRecord::StatementInvalid: Could not find schema kochi
/var/lib/gems/2.3.0/gems/apartment-1.2.0/lib/apartment/adapters/postgresql_adapter.rb:66:in `connect_to_new'
/var/lib/gems/2.3.0/gems/apartment-1.2.0/lib/apartment/adapters/abstract_adapter.rb:91:in `block in switch!'
/var/lib/gems/2.3.0/gems/activesupport-5.1.3/lib/active_support/callbacks.rb:97:in `run_callbacks'
/var/lib/gems/2.3.0/gems/apartment-1.2.0/lib/apartment/adapters/abstract_adapter.rb:88:in `switch!'
/var/lib/gems/2.3.0/gems/apartment-1.2.0/lib/apartment/adapters/abstract_adapter.rb:105:in `switch'
/home/user1/Desktop/SPERICORN/kidversity/lib/tasks/dynamic_age_setter.rake:4:in `block (2 levels) in <top (required)>'
/var/lib/gems/2.3.0/gems/rake-12.0.0/exe/rake:27:in `<top (required)>'
Tasks: TOP => db:dynamic_age_setter
(See full trace by running task with --trace)
you are missing ActiveRecord::Base.establish_connection.connection try this
sample code below :
namespace :debtor_email_data_fix do
desc 'update data'
task email_normalize_to_downcase: :environment do
create_connection_with_db
Apartment::Tenant.switch!('app')
Email.find_each do |t|
t.update(address: t.address.downcase)
end
end
task all: [:email_normalize_to_downcase] do
end
private
def create_connection_with_db
ActiveRecord::Base.establish_connection.connection
end
end

unable to run rake task with smarter_csv

I created a rake task for import a csv with smarter_csv gem.
Throws the following message, i added the entire error code if it helps:
rake import_csv:import_csv
Digest::Digest is deprecated; use Digest
I, [2017-03-01T11:46:08.551994 #7029] INFO -- : ** [Raven] Raven 0.14.0 configured not to send errors.
rake aborted!
NoMethodError: undefined method `close' for nil:NilClass
/home/my_user/.rvm/gems/ruby-2.1.1#my_app/gems/smarter_csv-1.1.4/lib/smarter_csv/smarter_csv.rb:223:in `ensure in process'
/home/my_user/.rvm/gems/ruby-2.1.1#my_app/gems/smarter_csv-1.1.4/lib/smarter_csv/smarter_csv.rb:223:in `process'
/home/my_user/my_app/lib/tasks/import_csv.rake:7:in `block (2 levels) in <top (required)>'
/home/my_user/.rvm/gems/ruby-2.1.1#my_app/bin/ruby_executable_hooks:15:in `eval'
/home/my_user/.rvm/gems/ruby-2.1.1#my_app/bin/ruby_executable_hooks:15:in `<main>'
Errno::ENOENT: No such file or directory # rb_sysopen - /csv_imports/pacientes.csv
/home/my_user/.rvm/gems/ruby-2.1.1#my_app/gems/smarter_csv-1.1.4/lib/smarter_csv/smarter_csv.rb:23:in `initialize'
/home/my_user/.rvm/gems/ruby-2.1.1#my_app/gems/smarter_csv-1.1.4/lib/smarter_csv/smarter_csv.rb:23:in `open'
/home/my_user/.rvm/gems/ruby-2.1.1#my_app/gems/smarter_csv-1.1.4/lib/smarter_csv/smarter_csv.rb:23:in `process'
/home/my_user/my_app/lib/tasks/import_csv.rake:7:in `block (2 levels) in <top (required)>'
/home/my_user/.rvm/gems/ruby-2.1.1#my_app/bin/ruby_executable_hooks:15:in `eval'
/home/my_user/.rvm/gems/ruby-2.1.1#my_app/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => import_csv:import_csv
(See full trace by running task with --trace)
This is the code on the rake task:
namespace :import_csv do
desc "import csv file"
task import_csv: :environment do
require 'smarter_csv'
filename = '/csv_imports/file.csv'
options = {:key_mapping => { :downcase_header => true }}
SmarterCSV.process(filename, options) do |array|
CsvImports.create( array.first )
end
end
end
This solved just witha dot. route issue.
./csv_imports/file.csv

undefined method `run' for main:Object

i'm getting the following output when deploying:
cap aborted!
NoMethodError: undefined method `run' for main:Object
config/deploy.rb:37:in `block (2 levels) in <top (required)>'
/var/lib/gems/1.9.1/gems/capistrano-3.2.1/lib/capistrano/dsl/task_enhancements.rb:12:in `block in after'
/var/lib/gems/1.9.1/gems/capistrano-3.2.1/lib/capistrano/application.rb:15:in `run'
/var/lib/gems/1.9.1/gems/capistrano-3.2.1/bin/cap:3:in `<top (required)>'
Tasks: TOP => deploy:permissions
(See full trace by running task with --trace)
The deploy has failed with an error: #<NoMethodError: undefined method `run' for main:Object>
i'm using Capistrano Version: 3.2.1 (Rake Version: 10.3.2).
the deploy works fine but i created a after deploy task to modify the owner of the deployed release which looks so:
namespace :deploy do
task :permissions do
run "chown -R :#{fetch(:group)} #{deploy_to} && chmod -R g+s #{deploy_to}"
end
end
after :deploy, "deploy:permissions"
vars are defined correctly (i fixed that error before) but i get this missing method error for the run method and i dont know why.
Your code uses 2.x syntax, while your version is 3.x. In 3.x, the syntax looks like this:
namespace :deploy do
on roles :all do
execute :chown, "-R :#{fetch(:group)} #{deploy_to} && chmod -R g+s #{deploy_to}"
end
end

rails script using whenever gem error: __rvm_add_to_path: command not found

I'm using capistrano and the whenever gem.
Capistrano deploys and updates the crontab on our ubuntu server with the cronjob detailed out in our schedule.rb file.
But the cronjob doesn't happen. It fails and emails me every 5 minutes this error:
/etc/profile.d/rvm.sh: line 67: __rvm_add_to_path: command not found
/home/pkatepalli/.rbenv/versions/1.9.3-p448/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- bundler/setup (LoadError)
from /home/pkatepalli/.rbenv/versions/1.9.3-p448/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /[REMOVED]/releases/20140204194143/config/boot.rb:6:in `<top (required)>'
from /home/pkatepalli/.rbenv/versions/1.9.3-p448/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /home/pkatepalli/.rbenv/versions/1.9.3-p448/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from script/rails:4:in `<main>'
The weird part is we're deploying with a different username than pkatepalli. pkatepalli is my username on the server. That explains why it's emailing me the error - I think.
Also, we're using ruby 1.9.3 not 1.9.1. I'm not sure why it's not using the right ruby version.
On server:
Bundler version 1.3.5
ruby 1.9.3p448 (2013-06-27 revision 41675) [x86_64-linux]
$ rbenv version
1.9.3-p448 (set by /[REMOVED]/current/.ruby-version)
But interestingly enough in the current directory there's ruby 1.9.1, which I'm confused by:
/current/vendor/bundle/ruby/1.9.1
gemfile:
gem 'whenever', require: false
schedule.rb:
I've tried uncommenting: env 'PATH', ENV['PATH'], but the PATH that gets put into the schedule.rb file doesn't help.
Locally we're using RVM instead of rbenv (which is being used on the server). I'm not sure if that screws up the path if we set: env 'PATH', ENV['PATH']
#env 'PATH', ENV['PATH']
#set :output, "/log/cron.log"
#set :stage, :environment_variable
every 5.minutes, :roles => [:app] do
runner "Model.method"
end
deploy.rb:
require 'capistrano/log_with_awesome'
require "bundler/capistrano"
set :application, "[REMOVED]"
set :scm, :git
set :repository, "[REMOVED]"
set :branch, "master"
set :deploy_via, :remote_cache
set :user, "[REMOVED]"
set :password, "[REMOVED]"
set :deploy_to, "[REMOVED]"
set :keep_releases, 5
task :qa do
set :domain, "[REMOVED]"
role :web, "[REMOVED]", {:port => [REMOVED]} # Your HTTP server, Nginx
role :app, "[REMOVED]", {:port => [REMOVED]} # This may be the same as your `Web` server
set :env, "test"
end
task :production do
set :domain, "[REMOVED]"
role :web, "[REMOVED]", {:port => [REMOVED]} # Your HTTP server, Nginx
role :app, "[REMOVED]", {:port => [REMOVED]} # This may be the same as your `Web` server
set :env, "production"
end
set :use_sudo, false
default_run_options[:pty] = true
role :db, "[REMOVED]", {:port => [REMOVED], primary: true, :no_release => true}
after "deploy:setup", "deploy:chown"
namespace :bundle do
task :install, {:roles => :app} do
run "cd #{release_path} && bundle install --deployment --without development test"
end
end
before "deploy:assets:precompile" do
transfer :up, "config/application.yml", "#{shared_path}/application.yml", :via => :scp
run "ln -nfs #{shared_path}/application.yml #{release_path}/config/application.yml"
end
namespace :whenever do
task :start, :roles => :app do
run "cd #{release_path} && bundle exec whenever --update-crontab"
end
end
namespace :deploy do
task :execute_migrations, :roles => :app do
puts "RUNNING DB MIGRATIONS"
run "cd #{current_path}; bundle exec rake RAILS_ENV=#{env} db:migrate"
end
task :chown do
run "#{try_sudo} chown -R #{user} #{deploy_to}"
end
task :restart_nginx, {:roles => :web} do
run "#{try_sudo} touch #{File.join(current_path,'tmp','restart.txt')}"
end
before "deploy:restart_nginx", "deploy:execute_migrations"
after :deploy, "deploy:restart_nginx"
after "deploy:restart_nginx", "deploy:cleanup"
after "deploy:update", "whenever:start"
end
When I ssh into the server and run crontab -l in the app's current directory:
# Begin Whenever generated tasks for: /[REMOVED]/releases/20140204194143/config/schedule.rb
#0,5,10,15,20,25,30,35,40,45,50,55 * * * * /bin/bash -l -c 'cd /[REMOVED]/releases/20140204194143 && script/rails runner -e production '\''Model.method'\'''
# End Whenever generated tasks for: [REMOVED]/releases/20140204194143/config/schedule.rb
When I run the cron job on the server like this I get this result:
:/[REMOVED]/current$ rails runner -e production '\''Model.method'\'''
Rails Error: Unable to access log file. Please ensure that /[REMOVED]/releases/20140204194143/log/production.log exists and is chmod 0666. The log level has been raised to WARN and the output directed to STDERR until the problem is fixed.
/[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/commands/runner.rb:53:in `eval': (eval):1: syntax error, unexpected $undefined (SyntaxError)
\Model.method'
^
(eval):1: unterminated string meets end of file
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/commands/runner.rb:53:in `<top (required)>'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/commands.rb:64:in `require'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/commands.rb:64:in `<top (required)>'
from script/rails:5:in `require'
from script/rails:5:in `<main>'
When I do it like this. I get this:
[REMOVED]/current$ rails runner Model.method
Rails Error: Unable to access log file. Please ensure that [REMOVED]/releases/20140204194143/log/development.log exists and is chmod 0666. The log level has been raised to WARN and the output directed to STDERR until the problem is fixed.
[REMOVED]/20140204194143/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/postgresql_adapter.rb:1208:in `initialize': could not connect to server: Connection refused (PG::ConnectionBad)
Is the server running on host "localhost" ([REMOVED]) and accepting
TCP/IP connections on port 5432?
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/postgresql_adapter.rb:1208:in `new'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/postgresql_adapter.rb:1208:in `connect'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/postgresql_adapter.rb:326:in `initialize'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/postgresql_adapter.rb:28:in `new'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/postgresql_adapter.rb:28:in `postgresql_connection'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/abstract/connection_pool.rb:315:in `new_connection'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/abstract/connection_pool.rb:325:in `checkout_new_connection'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/abstract/connection_pool.rb:247:in `block (2 levels) in checkout'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/abstract/connection_pool.rb:242:in `loop'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/abstract/connection_pool.rb:242:in `block in checkout'
from /usr/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/abstract/connection_pool.rb:239:in `checkout'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/abstract/connection_pool.rb:102:in `block in connection'
from /usr/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/abstract/connection_pool.rb:101:in `connection'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/abstract/connection_pool.rb:410:in `retrieve_connection'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/abstract/connection_specification.rb:171:in `retrieve_connection'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/abstract/connection_specification.rb:145:in `connection'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/model_schema.rb:308:in `clear_cache!'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/railtie.rb:97:in `block (2 levels) in <class:Railtie>'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.12/lib/active_support/callbacks.rb:418:in `_run__249672195884464632__prepare__2220886522034318467__callbacks'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.12/lib/active_support/callbacks.rb:405:in `__run_callback'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.12/lib/active_support/callbacks.rb:385:in `_run_prepare_callbacks'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.12/lib/active_support/callbacks.rb:81:in `run_callbacks'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.12/lib/action_dispatch/middleware/reloader.rb:74:in `prepare!'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.12/lib/action_dispatch/middleware/reloader.rb:48:in `prepare!'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/application/finisher.rb:47:in `block in <module:Finisher>'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/initializable.rb:30:in `instance_exec'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/initializable.rb:30:in `run'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/initializable.rb:55:in `block in run_initializers'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/initializable.rb:54:in `each'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/initializable.rb:54:in `run_initializers'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/application.rb:136:in `initialize!'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/railtie/configurable.rb:30:in `method_missing'
from /[REMOVED]/releases/20140204194143/config/environment.rb:5:in `<top (required)>'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.12/lib/active_support/dependencies.rb:251:in `require'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.12/lib/active_support/dependencies.rb:251:in `block in require'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.12/lib/active_support/dependencies.rb:236:in `load_dependency'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.12/lib/active_support/dependencies.rb:251:in `require'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/application.rb:103:in `require_environment!'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/commands/runner.rb:44:in `<top (required)>'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/commands.rb:64:in `require'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/commands.rb:64:in `<top (required)>'
from script/rails:5:in `require'
from script/rails:5:in `<main>'
In response to Leonid Shevtsov's answer:
I ran:
rails runner -e production 'Model.method'
and I got this:
[REMOVED]/current$ rails runner -e production 'Model.methodName'
/[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/commands/runner.rb:53:in `eval': undefined method `methodName' for #<Class:0x0000000570d668> (NoMethodError)
from (eval):1:in `<top (required)>'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/commands/runner.rb:53:in `eval'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/commands/runner.rb:53:in `<top (required)>'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/commands.rb:64:in `require'
from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/commands.rb:64:in `<top (required)>'
from script/rails:5:in `require'
from script/rails:5:in `<main>'
when I run crontab -l, I get the whenever generated crons
a) 1.9.1 is the "base" version of Ruby 1.9.3 (1.9.3 is considered to be a version of Ruby 1.9.1); it's normal that you see it in the paths.
b) the command line in cron is escaping quotes; the proper way to check it manually is rails runner -e production 'Model.method'
c) are you sure the error isn't arriving from a locally installed cron job? (check with crontab -l)
Ok this is kind of complicated and some debugging info is missing so I will try to take a guess.
First of all it seems that you have both rvm and rbenv installed on your system which is not a very good idea. Try uninstalling one of the two, or better yet uninstall both and reinstall the one you prefer.
In your deploy script it seems you have rolled your own whenever capistrano tasks. This is not bad, but whenever already provides this out of the box with better support (e.g. rollbacks). You can also get better feedback on your future problems.
Those are potential pitfalls but their are not probably the cause of your problems. You seem to have messed things up regarding the linux users and their permissions. As far as I understand there are two users on your production system: pkatepalli and another one which you commented out, lets call him deployer.
From what I see you are trying to set everything up to work from the perspective of deployer which is a good practice.
I assume the capistrano user variable is set to deployer. If not this is the root of your problem. This is the user which capistrano uses to log in and issue your commands.
Also it seems that when you run crontab -l you see the cron jobs correctly but if you are logged in with pkatepalli when you run the command then this is your problem. You have written your tasks to the wrong user's crontab. If this has happened it more likely means either that you run the whenever command from the wrong user during cap deploy (in which case you need to check which is the logged in user during cap) or you have actually run the whenever command from both users and you are only getting errors for pkatepalli (clear your crontab before experimenting again). If it still looks it is ok then you can try passing the -u username option to whenever command (you probably need sudo for this).
To set up the deployer user properly you can follow the instructions by capistrano

Capistrano 3 upgrade problems

I've been trying to upgrade from Capistrano 2 to Capistrano 3, but have been working through all the errors. I'm stuck on one that I don't know how to solve now. I'm using Unicorn and Nginx.
Error trace:
cap production deploy:cold --trace
** Invoke production (first_time)
** Execute production
** Invoke load:defaults (first_time)
** Execute load:defaults
cap aborted!
Task Argument Error
/Users/devanb/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/task_manager.rb:96:in `resolve_args_with_dependencies'
/Users/devanb/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/task_manager.rb:62:in `resolve_args'
/Users/devanb/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/task_manager.rb:24:in `define_task'
/Users/devanb/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/task.rb:360:in `define_task'
/Users/devanb/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/dsl_definition.rb:32:in `task'
config/deploy.rb:31:in `block (2 levels) in <top (required)>'
config/deploy.rb:29:in `each'
config/deploy.rb:29:in `block in <top (required)>'
/Users/devanb/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/task_manager.rb:196:in `in_namespace'
/Users/devanb/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/dsl_definition.rb:104:in `namespace'
config/deploy.rb:28:in `<top (required)>'
/Users/devanb/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/capistrano-3.0.1/lib/capistrano/setup.rb:12:in `load'
/Users/devanb/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/capistrano-3.0.1/lib/capistrano/setup.rb:12:in `block (2 levels) in <top (required)>'
/Users/devanb/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/task.rb:236:in `call'
/Users/devanb/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/task.rb:236:in `block in execute'
/Users/devanb/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/task.rb:231:in `each'
/Users/devanb/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/task.rb:231:in `execute'
/Users/devanb/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/task.rb:175:in `block in invoke_with_call_chain'
/Users/devanb/.rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
/Users/devanb/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/task.rb:168:in `invoke_with_call_chain'
/Users/devanb/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/task.rb:161:in `invoke'
/Users/devanb/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/application.rb:149:in `invoke_task'
/Users/devanb/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/application.rb:106:in `block (2 levels) in top_level'
/Users/devanb/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/application.rb:106:in `each'
/Users/devanb/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/application.rb:106:in `block in top_level'
/Users/devanb/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/application.rb:115:in `run_with_threads'
/Users/devanb/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/application.rb:100:in `top_level'
/Users/devanb/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/application.rb:78:in `block in run'
/Users/devanb/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/application.rb:165:in `standard_exception_handling'
/Users/devanb/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/application.rb:75:in `run'
/Users/devanb/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/capistrano-3.0.1/lib/capistrano/application.rb:12:in `run'
/Users/devanb/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/capistrano-3.0.1/bin/cap:3:in `<top (required)>'
/Users/devanb/.rbenv/versions/2.0.0-p247/bin/cap:23:in `load'
/Users/devanb/.rbenv/versions/2.0.0-p247/bin/cap:23:in `<main>'
Tasks: TOP => production
Deploy file:
set :application, 'liveinbham'
set :user, 'deployer'
set :deploy_to, "/home/#{fetch(:user)}/apps/#{fetch(:application)}"
set :scm, "git"
set :repo_url, 'git#github.com:DevanB/LIveInBham.git'
set :use_sudo, false
set :scm_verbose, true
set :git_shallow_clone, 1
set :git_enable_submodules, 1
set :ssh_options, { :forward_agent => true }
set :stage, :production
set :rails_env, "production"
set :branch, "master"
set :pty, true
set :linked_files, %w{config/database.yml}
set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system public/uploads}
set :keep_releases, 5
namespace :deploy do
%w{start stop restart}.each do |command|
desc "#{command} unicorn server"
task command, roles: :app, except: {no_release: true} do
run "/etc/init.d/unicorn_#{fetch(:application)} #{command}"
end
end
task :setup_config, roles: :app do
sudo "ln -nfs #{current_path}/config/nginx.conf /etc/nginx/sites-enabled/#{fetch(:application)}"
sudo "ln -nfs #{current_path}/config/unicorn_init.sh /etc/init.d/unicorn_#{fetch(:application)}"
end
after "deploy:setup", "deploy:setup_config"
after :finishing, 'deploy:cleanup'
end
desc "tail log files"
task :tail, :roles => :app do
run "tail -f /home/#{fetch(:user)}/apps/#{fetch(:application)}/current/log/production.log" do |channel, stream, data|
puts "#{channel[:host]}: #{data}"
break if stream == :err
end
end
desc "Open the rails console on one of the remote servers"
task :console, :roles => :app do
hostname = find_servers_for_task(current_task).first
exec "ssh -l #{fetch(:user)} #{hostname} -t 'source ~/.profile && #{current_path}/script/rails c #{fetch(:rails_env)}'"
end
Could be a few things but deploy:cold is depreciated in Capistrano 3, I've just tried it and I just get "don't know how to build task" which is what I'd expect. Potentially running deploy:setup_config and then just a straight up deploy may work.
There's a few other things which will need tweaking like you can no longer use that approach for streaming log files, it's just a simple execute instead. E.g.:
namespace :logs do
desc "tail rails logs"
task :tail_rails do
on roles(:app) do
execute "tail -f #{shared_path}/log/#{fetch(:rails_env)}.log"
end
end
end
In general the task definition format also needs updating, e.g:
task :setup_config, roles: :app do
# some stuff
end
would become
task :setup_config do
on roles(:app) do
#some stuff
end
end
It looks like your Cap 2 configuration is based quite heavily on the Railscast about it. Mine also was and I recently went through the process of migrating all the apps using it to a new Cap 3 one. I've documented the approach I ended up with here including the code.
The stack trace makes it pretty clear. The error comes from line 31 of your file, where you try to define a task using the old Capistrano 2 syntax. The new Capistrano 3 uses Rake, so you must use Rake syntax to define tasks, with the aid of some Capistrano methods. Rake does not recognize "roles" and "except". TalkingQuickly gave a good-looking example of the new way to define tasks.

Resources