Make Background Job (bj) works in Windows XP


I am a fan of Ruby on Rails (RoR) and I recently have to do a lot of data loading to MySQL and the client requires to have a button to click from a web page, and I just found out doing record by record insert in Rails was extremely painful. User will have to wait until it is done and there is no way I can push the job to be performed in background. One way I was thinking to get around the problem was to have start another AJAX page and poll status from application log but this is not guaranteed to work (Rails just simply stop to response in many cases), so left me with a few choices Background Job (bj), BackgrounDRB, spawn and a few more.

Most of these background task tools, however,  seem to work only in *nix environment, but my client works only with Windows, so bj is the best choice as it claims to be relatively platform independent (even with Windows).

That is why I started using bj, and it seems to work fine with me, easy to use and works flawlessly with my development environment which happens to be Mac OSX Leopard.

I started to find avproblem when start testing on Windows, and problem started from the very beginning — when doing ‘bj setup‘ and returned code was ’42’ instead of ‘0’ and bj stopped!. I tried on another XP and Vista and the same problem found.

(NOTE: I moved on to a few more XP, some of them seem to work fine, not consistent but some really worked but I just could not live with this inconsistency!)

I started to search on web pages to see if I was the only one, and found others having the same problem and nobody seems to care to answer (probably very few people actually works with Windows XP) such as:

I, [..] INFO -- : cmd
I, [..] INFO -- : status <0>
I, [..] INFO -- : generated <./db/migrate/042_bj_migration0.rb>
I, [..] INFO -- : cmd
I, [..] INFO -- : status <42>;
F, [..] FATAL -- : "rake RAILS_ENV=development db:migrate" failed with #(RuntimeError)
C:/../vendor/plugins/bj/lib/bj/util.rb:28:in `spawn'
C:/../vendor/plugins/bj/lib/bj/api.rb:147:in `migrate'
C:/../vendor/plugins/bj/lib/bj/bj.rb:47:in `call'
C:/../vendor/plugins/bj/lib/bj/bj.rb:47:in `chroot'
C:/../vendor/plugins/bj/lib/bj/api.rb:146:in `migrate'
C:/../vendor/plugins/bj/lib/bj/api.rb:110:in `setup'
C:/../vendor/plugins/bj/lib/bj/bj.rb:54:in `call'
C:/../vendor/plugins/bj/lib/bj/bj.rb:54:in `chroot'
C:/../vendor/plugins/bj/lib/bj/bj.rb:52:in `chdir'
C:/../vendor/plugins/bj/lib/bj/bj.rb:52:in `chroot'
C:/../vendor/plugins/bj/lib/bj/api.rb:108:in `setup'
script/bj:349:in `run'
C:/../vendor/plugins/bj/lib/main/base.rb:25:in `call'
C:/../vendor/plugins/bj/lib/main/base.rb:25:in `run'
C:/../vendor/plugins/bj/lib/main/base.rb:13:in `catch'
C:/../vendor/plugins/bj/lib/main/base.rb:13:in `run'
C:/../vendor/plugins/bj/lib/main/base.rb:436:in `mode_run!'
C:/../vendor/plugins/bj/lib/main/base.rb:10:in `run'
C:/../vendor/plugins/bj/lib/main/factories.rb:11:in `run'
C:/../vendor/plugins/bj/lib/main/factories.rb:16:in `Main' script/bj:6

So I  started looking through ‘bj‘ code (I have no other choice but fix it or choosing another language and framework!) and found out that although bj tried to find the what ‘rake’ command is on the platform it is running e.g. whether it is a ‘bat’ file or executable ‘rake’ like in *nix and stored this value in ‘Bj.rake’, it did not use this and simply use ‘rake’ as in

your_project\vendor\plugins\bj\lib\bj\api.rb (from bj 1.0.1)
147         util.spawn "rake RAILS_ENV=#{ Bj.rails_env } db:migrate", options

Note I installed bj via plugin

ruby script\plugin install

What I did was simply tell ‘bj’ to use ‘Bj.rake’ instead of just ‘rake’

147         util.spawn "#{Bj.rake} RAILS_ENV=#{ Bj.rails_env } db:migrate", options

and everything seems to work fine. I tested on ruby 1.8.6 and rails 2.3.3 on XP and Vista (a few machines).

Below is a sample output on my machine

C:\workspace\bgp4>rake db:create
(in C:/workspace/bgp4)                                                          

C:\workspace\bgp4>ruby script\bj setup
I, [2009-08-12T12:16:55.500000 #4304]  INFO -- : cmd
I, [2009-08-12T12:16:59.890000 #4304]  INFO -- : status <0>
I, [2009-08-12T12:16:59.890000 #4304]  INFO -- : generated <./db/migrate/2009081 2051659_bj_migration2.rb>
I, [2009-08-12T12:17:00.531000 #4304]  INFO -- : cmd <rake.bat RAILS_ENV=development db:migrate>
I, [2009-08-12T12:17:05.453000 #4304]  INFO -- : status <0>

So now bj really Rocks even in XP and Vista. Hope people who use XP will continue to use RoR and bj more!


One Response to “Make Background Job (bj) works in Windows XP”

  1. 1 jelbourn

    Thanks loads – simple google to find this, simple change (although mine was in C:\ruby\lib\ruby\gems\1.8\gems\bj-1.0.1\lib\bj\api.rb) and all OK 🙂

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: