When Rake goes "Ache!"

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?

Robert James 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

···

--
Posted via http://www.ruby-forum.com/\.

Jim Weirich wrote:

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.

--
Posted via http://www.ruby-forum.com/\.

Wow! The famous Jim himself uses ruby-forum. Should quiet all those
who complain about it :slight_smile:

Robert James wrote:

Jim Weirich 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 Weirich

···

--
Posted via http://www.ruby-forum.com/\.