質問

after doing cap qa deploy in the terminal, I get the following error at the end of the deploy:

failed: "sh -c 'cd [removed]/releases/[removed] && bundle exec whenever --update-crontab [removed] --set environment=production --roles db'" on [removed]

The first thing I'm confused about is when I use capistrano and deploy, why is it running the whenever command on the db role? Shouldn't it automatically run the command on the app role?

On server: Bundler version 1.3.5

gemfile:

gem 'whenever', require: false

schedule.rb:

env 'PATH', ENV['PATH']

set :output, "/log/cron.log"
set :stage, :environment_variable

every 5.minutes, :roles => [:app] do
  runner "[removed]"
end

every 1.day, :at => '0:01 am' do
    command "[removed]"
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 the command there:

[REMOVED]@[REMOVED]/current$ bundle exec whenever --update-crontab
/var/lib/gems/1.9.1/gems/bundler-1.3.5/lib/bundler/runtime.rb:216: warning: Insecure world writable dir [REMOVED] in PATH, mode 040777
[write] crontab file updated

I checked the crontab file by running: crontab -l and this is what I got in the crontab:

[REMOVED]@[REMOVED]/current$ crontab -l
PATH=/[REMOVED]/releases/[REMOVED]/vendor/bundle/ruby/1.9.1/bin:/home/[REMOVED]/.rbenv/shims:/home/[REMOVED]/.rbenv/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games

#Begin Whenever generated tasks for: /[REMOVED]/releases/[REMOVED]/config/schedule.rb
PATH=/[REMOVED]/releases/[REMOVED]/vendor/bundle/ruby/1.9.1/bin:/home/[REMOVED]/.rbenv/shims:/home/[REMOVED]/.rbenv/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games

0,5,10,15,20,25,30,35,40,45,50,55 * * * * /bin/bash -l -c 'cd /[REMOVED]/releases/[REMOVED] && script/rails runner -e production '\''/[REMOVED]/current/script/[REMOVED].rb'\'''

1 0 * * * /bin/bash -l -c '/[REMOVED]/current/script/[REMOVED].rb'

#End Whenever generated tasks for: /[REMOVED]/releases/[REMOVED]/config/schedule.rb

I then get this error because of the first cron job (emailed to my work email):

/etc/profile.d/rvm.sh: line 67: __rvm_add_to_path: command not found
/home/[REMOVED]/.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/[REMOVED]/.rbenv/versions/1.9.3-p448/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
    from /[REMOVED]/releases/20140203065556/config/boot.rb:6:in `<top (required)>'
    from /home/[REMOVED]/.rbenv/versions/1.9.3-p448/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
    from /home/[REMOVED]/.rbenv/versions/1.9.3-p448/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
    from script/rails:4:in `<main>'
役に立ちましたか?

解決

To set the roles when using your method, set the following:

set :whenever_roles,        ->{ :app }

It defaults to :db

Other options that can be set:

set :whenever_roles,        ->{ :db }
set :whenever_options,      ->{ {:roles => fetch(:whenever_roles)} }
set :whenever_command,      ->{  }
set :whenever_identifier,   ->{ fetch :application }
set :whenever_environment,  ->{ fetch :rails_env, "production" }
set :whenever_variables,    ->{ "environment=#{fetch :whenever_environment}" }
set :whenever_update_flags, ->{ "--update-crontab #{fetch :whenever_identifier} --set #{fetch :whenever_variables}" }
set :whenever_clear_flags,  ->{ "--clear-crontab #{fetch :whenever_identifier}" }

https://github.com/javan/whenever/blob/master/lib/whenever/capistrano/v3/tasks/whenever.rake

OR

I tend to create custom tasks like the following:

namespace :whenever do
  task :start, :roles => :app do
    run "cd #{release_path} && bundle exec whenever --update-crontab"
  end
end

Then have capistrano execute it when I want like so:

after "deploy:update", "whenever:start"

This way you can reuse/rerun/test this anytime with the following:

cap whenever:start
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top