Thursday, January 19, 2006

I prefer Rake to NAnt (#3)

I prefer Ruby's multi-threading to NAnt's lack of.
The current application I'm working on has lots of data. With every build we drop, create, and reload the objects in the database. The drop and create sql must be executed first, but the loading scripts can be run in parallel. Ruby makes this easy with the Thread class in the Core API.
task "setup_db" do
tasker = DbTasks.new
tasker.execute_osql("object_drop_and_creation.sql")

#execute in parallel
threads = []
threads << Thread.new { tasker.execute_osql("dummy_data1.sql") }
threads << Thread.new { tasker.execute_osql("dummy_data2.sql") }
threads << Thread.new { tasker.execute_osql("dummy_data3.sql") }
threads << Thread.new { tasker.execute_osql("dummy_data4.sql") }
threads << Thread.new { tasker.execute_osql("dummy_data5.sql") }

#join threads
while ( t = threads.shift ) ; t.join ; end
end
*DbTasks is simple class that contains common information (login info). The DbTasks.execute_osql method is just a wrapper for executing osql as a system command with DbTasks' static information.

**As usual, there may be a better way in Ruby. And, there may be multi-threading options in NAnt that I'm not aware of.

1 comment:

  1. You might want to check out the new multitask feature in Rake 0.7.0. It allows you to rewrite the above with:

    ---------------------------------------
    Tasker = DbTasks.new

    task :drop_and_create do
      Tasker.execute_osql("object_drop_and_creation.sql")
    end

    SubTasks = (1..5).collect { |i|
      task "load#{i}" => :drop_and_create do
        Tasker.execute_osql("data_dummy#{i}.sql")
      end
    }

    multitask :setup_db => SubTasks
    ----------------------------------------

    -- Jim Weirich

    ReplyDelete

Note: Only a member of this blog may post a comment.