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.
Post a Comment