如何在自定义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 – 即使我没有引发exception。

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

看着这个例子

 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 

@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