views:

16

answers:

1

I have a build task in rake defined with the following dependencies:

desc 'Builds the App'
task :rebuild_dev => ["solr:start", "db:drop", "db:create", "db:migrate", "spec", "solr:stop"]

The first task "solr:start" starts the Solr Indexing server. Now, if the build fails (may be in spec tests fail), the "solr:stop" task is not executed. And the server is not stopped.

Is there any way to specify a clean-up task or a task that always runs even if one of the dependent tasks fail? In my case, to always ensure that "solr:stop" executes...

+1  A: 

You just need use the ensure system of Ruby

desc "Builds the App"
task :rebuild_dev do
  begin
    ["solr:start", "db:drop", "db:create", "db:migrate", "spec"].each do |t|
      Rake::Task[t].execute
    end
  ensure
    Rake::Task["solr:stop"].execute
  end
end
shingara
Thanks for the response. One thing to note is it is better to run invoke on the tasks rather than execute. Coz invoke will execute the dependencies as well.
rc