如何在自定义capistrano任务中使用交易? [英] How do I use transactions within custom capistrano tasks?
问题描述
我正在编写一个自定义的capistrano任务来缩小我的javascripts,并希望通过回滚部署来处理minify失败的情况。
I'm writing a custom capistrano task to minify my javascripts and want to handle the case where the minification fails by rolling back the deploy.
我已经通过文档和思考,我想出了如何做到这一点,但它对我来说不起作用。
I've been through the documentation and thought I'd figured out how to do it, but it's not working for me.
这是我有的:
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
并将其设置为失败,我得到以下输出:
When I run cap staging deploy:bundle
and set it up to fail, I get the following output:
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
钩子没有运行。它似乎知道任务失败,因为它输出失败
在最后 - 即使我没有提出例外。
So it is in a transaction, but my on_rollback
hook doesn't get run. It does seem to know the task failed, as it outputs failed
at the end - even though I haven't raised an exception.
关于为什么我的 on_rollback
没有运行的任何想法?
Any ideas as to why my on_rollback
isn't running?
推荐答案
查看示例
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调用是不应该在事务块里面的,如
I wonder if the on_rollback call shouldn't go inside the transaction block, like
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任务中使用交易?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!