【问题标题】:How do I use transactions within custom capistrano tasks?如何在自定义 capistrano 任务中使用事务?
【发布时间】:2011-05-24 11:43:31
【问题描述】:

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

我已经阅读了文档,并认为我已经弄清楚了如何做到这一点,但它对我不起作用。

这是我所拥有的:

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 没有运行的任何想法?

【问题讨论】:

    标签: ruby-on-rails deployment rake capistrano


    【解决方案1】:

    看着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  
    

    【讨论】:

    • capistrano 3 还是这样吗?
    【解决方案2】:

    @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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-12
      • 1970-01-01
      • 1970-01-01
      • 2014-01-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多