Setting up BackgrounDrb

I decided to use BackgrounDrb 0.2.1 to launch a periodic task that would run every 15 seconds.Warning: After going through the hassle of setting up BackgrounDrb, I found out that it isn’t even supported in Windows (my dev environment)! See step 8. So, these instructions haven’t been fully tested yet on a *.nix environment. Good luck and please post comments if you have any helpful suggestions.

  1. Install BackgroundDrb: svn co http://svn.devjavu.com/backgroundrb/tags/release-0.2.1 backgroundrb
  2. Setup BackgrounDrb: rake backgroundrb:setup
  3. Generate worker thread (Foo is capitalized): ruby script/generate worker Foo
  4. In one of your controllers, create new worker with Middleman object (:foo_worker is lowercase with underscore)
       MiddleMan.new_worker(:class => :foo_worker,
        :args => "Arguments used to instantiate a new HeartratepostWorker object",
  5. Instantiate Middleman object in environment.rb (you might not need this with version 0.2.1 of BackgrounDrb)
      require "drb"  DRb.start_service
    
      MiddleMan = DRbObject.new(nil, "druby://localhost:22222"
  6. Install the daemons gem if you haven’t already: gem install daemons
  7. Install the slave gem if you haven’t already: gem install slave
  8. Start the BackgroundDrb server in a separate command window: ruby script\backgroundrb start
    • If you get the error “ERROR: there is already one or more instance(s) of the program running”, delete log\backgroundrb.pid
    • Windows users are out of luck! You’ll get this message: `fork': the fork() function is unimplemented on this machine. Here’s a message from Ezra (the devleoper of BackgroundDrb) about Windows support:
      • “Unfortunately that won’t cut it in this case, I wish it would. Slave does more then just fork. It uses ipc and a few other things that just don’t work yet on windows. There is a possibility it could be made to work but right now it does not, even with win32 process (gem). -Ezra Wed Nov 29 16:19:03 EST 2006″
  9. Debugging methods if things don’t work well

Notes

  1. You might want to restart the backgroundrb server after you’ve modified the *_worker.rb file to lib/workers. It could cause an error otherwise.
  2. In case you get this error when starting Mongel, copy the vendor/plugins/backgroundrb directory to a temp directory and it will work

    already initialized constant OPTIONS

  3. To set up a periodic task, set up the Middleman object this way:
            session[:job_key] = worker
    
            MiddleMan.schedule_worker(
    
              :class => :heartrate_post_worker,
    
              :args => "some arg to do_work",
    
              :job_key => :simple_schedule,
    
              :trigger_args => {
    
                :start => Time.now,
    
                :end => Time.now + 10.minutes,
    
                :repeat_interval => 15.seconds
    
              }
    
            )

References

  1. Sending emails in the background with ActionMailer and BackgrounDRB
  2. Example using TTL (time to live)
Share