# When Rake goes "Ache!"

**URL:** <https://www.ruby-forum.com/t/when-rake-goes-ache/76272>\
**Category:** Ruby\
**Created:** [November 17, 2006, 4:00am UTC](https://www.ruby-forum.com/t/when-rake-goes-ache/76272 "2006-11-17T04:00:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![robertjames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9c0ed/32.png) [@robertjames](https://www.ruby-forum.com/u/robertjames)\
**Post date:** [November 17, 2006, 4:00am UTC](https://www.ruby-forum.com/t/when-rake-goes-ache/76272/1 "2006-11-17T04:00:56Z")

</div>

What is the best way to set something to do when a Rake task fails?

I’d like something like:

task :migrate\_safely =\> [:backup, :migrate] do  
rescue  
if :backup  
:restore  
end  
end  
end

Is there a way to do anything similar?

---

<div class="post-metadata">

**Author:** ![robertjames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9c0ed/32.png) [@robertjames](https://www.ruby-forum.com/u/robertjames)\
**Post date:** [November 17, 2006, 8:48pm UTC](https://www.ruby-forum.com/t/when-rake-goes-ache/76272/2 "2006-11-17T20:48:19Z")

</div>

Robert J. wrote:

> What is the best way to set something to do when a Rake task fails?
> 
> I’d like something like:
> 
> task :migrate\_safely =\> [:backup, :migrate] do  
> rescue  
> if :backup  
> :restore  
> end  
> end  
> end
> 
> Is there a way to do anything similar?

Does something like this work …

$migrate\_failed = false

task :default =\> :migrate\_safely

task :backup do  
puts “Backing Up”  
end

task :migrate do  
begin  
puts “Migrating”  
fail “Oops”  
rescue  
$migrate\_failed = true  
end  
end

task :restore do  
puts “Restoring”  
end

task :migrate\_safely =\> [:backup, :migrate] do  
Rake::Task[:restore].invoke if $migrate\_failed  
puts “Migrated Safely”  
end

---

<div class="post-metadata">

**Author:** ![robertjames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9c0ed/32.png) [@robertjames](https://www.ruby-forum.com/u/robertjames)\
**Post date:** [November 17, 2006, 9:01pm UTC](https://www.ruby-forum.com/t/when-rake-goes-ache/76272/3 "2006-11-17T21:01:21Z")

</div>

Jim W. wrote:

> end  
> end

Thanks - will try it out. Not all fails throw exceptions (right? it’s  
considered a fail if sh “false”), but I can always catch that and throw  
an exception manually.

> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).  
> Wow! The famous Jim himself uses ruby-forum. Should quiet all those  
> who complain about it 🙂

---

<div class="post-metadata">

**Author:** ![robertjames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9c0ed/32.png) [@robertjames](https://www.ruby-forum.com/u/robertjames)\
**Post date:** [November 17, 2006, 9:10pm UTC](https://www.ruby-forum.com/t/when-rake-goes-ache/76272/4 "2006-11-17T21:10:44Z")

</div>

Robert J. wrote:

> Jim W. wrote:
> 
> > end  
> > end
> 
> Thanks - will try it out. Not all fails throw exceptions (right? it’s  
> considered a fail if sh “false”), but I can always catch that and throw  
> an exception manually.

If the “sh” command fails, it will throw an exception that terminates  
rake processing. So you shouldn’t have a problem with that.

change the fail line in the example to:

sh “ruby -e ‘exit(1)’”

to try it.

– Jim W.
