When Rake goes "Ache!"

S

S. Robert James

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?
 
J

Jim Weirich

Robert said:
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
 
S

S. Robert James

Jim said:
Does something like this work ...


$migrate_failed = false
task :migrate do
begin
puts "Migrating"
fail "Oops"
rescue
$migrate_failed = true
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.
Wow! The famous Jim himself uses ruby-forum. Should quiet all those
who complain about it :)
 
J

Jim Weirich

Robert said:
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 Weirich
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top