如何在自定义capistrano任务中使用事务?

时间:2010-12-13 05:34:27

标签: ruby-on-rails deployment rake capistrano

我正在编写一个自定义的capistrano任务来缩小我的javascripts,并希望通过回滚部署来处理缩小失败的情况。

我一直在阅读文档,并认为我已经想出了如何做到这一点,但它对我不起作用。

这就是我所拥有的:

desc 'Minify all javascript files'
task :bundle, :roles => :app, :except => { :no_release => true } do
  on_rollback do
    run "rm #{current_path}/public/javascripts/all.js"
    puts "ROLLBACK"
  end 

  transaction do
    run "cd #{current_path}; RAILS_ROOT=#{current_path} rake bundle:js"
  end 
end 

after 'deploy:update', 'deploy:bundle'

当我运行cap staging deploy:bundle并将其设置为失败时,我得到以下输出:

    triggering start callbacks for `staging'
  * executing `staging'
    triggering start callbacks for `deploy:bundle'
  * executing `multistage:ensure'
  * executing `deploy:bundle'
 ** transaction: start
  * executing "cd /path/to/app/current; RAILS_ROOT=/path/to/app/current rake bundle:js"
    servers: ["example.com"]
    [example.com] executing command
*** [err :: example.com] rake aborted!
*** [err :: example.com] invalid byte sequence in US-ASCII
# Trace here - removed for brevity
    command finished
failed: "sh -c 'cd /path/to/app/current; RAILS_ROOT=/path/to/app/current rake bundle:js'" on example.com

因此事务中 ,但我的on_rollback挂钩无法运行。它确实知道任务失败了,因为它最后会输出failed - 即使我没有提出异常。

关于为什么我的on_rollback没有运行的任何想法?

2 个答案:

答案 0 :(得分:4)

@bgates是正确的,回滚需要在事务中。这是我的一个apache食谱的例子:

task :update_and_test_config, :roles => [:app, :search] do
  transaction do
    on_rollback do
      apache.link_previous_config
      deploy.rollback.revision
      apache.restart
      deploy.rollback.cleanup
    end
    apache.render_config
    apache.link_config
    apache.configtest
  end
end

答案 1 :(得分:3)

查看example

task :deploy do
  transaction do
    update_code
    symlink
  end
end

task :update_code do
  on_rollback { run "rm -rf #{release_path}" }
  source.checkout(release_path)
end
...

我想知道on_rollback调用是否不应该进入事务块,比如

  transaction do
    on_rollback do
      run "rm #{current_path}/public/javascripts/all.js"
      puts "ROLLBACK"
    end 
    run "cd #{current_path}; RAILS_ROOT=#{current_path} rake bundle:js"
  end  
相关问题