每当gem在部署时失败

时间:2014-01-28 02:39:18

标签: ruby-on-rails cron capistrano whenever

在终端中执行 cap qa deploy 后,我在部署结束时收到以下错误:

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

我首先感到困惑的是当我使用capistrano和deploy时,为什么它在db角色上运行when命令?它不应该自动在app角色上运行命令吗?

在服务器上: Bundler版本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

当我进入服务器并在那里运行命令时:

[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

我通过运行:crontab -l检查了crontab文件,这是我在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

我因为第一个cron作业(通过电子邮件发送到我的工作电子邮件)而收到此错误:

/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>'

1 个答案:

答案 0 :(得分:7)

要在使用方法时设置角色,请设置以下内容:

set :whenever_roles,        ->{ :app }

默认为:db

可以设置的其他选项:

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

或者

我倾向于创建如下的自定义任务:

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

然后让capistrano在我想要的时候执行它:

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

通过以下方式,您可以随时重复使用/重新运行/测试:

cap whenever:start