[Backgroundrb-devel] Using BackgrounDRb to replace nested loops?

Ezra Zygmuntowicz ezmobius at gmail.com
Tue Jul 18 15:14:59 EDT 2006


On Jul 18, 2006, at 11:24 AM, Jacob Patton wrote:

> Hi Ezra,
>
> Thanks for your advice regarding using BackgrounDRb to handle long- 
> running emailing processes for my blog application.
>
> I'm having a little trouble getting BackgrounDRb to work--I'm just  
> using it to post a message to the logs for now, to make sure I can  
> get the code running.
>
> My questions are inline:
>
>> [You said:] I think you would be best served by creating a worker  
>> class that did the compilation of messages and sent the emails in  
>> the backgroundrb server. I also think that every 5 or 10 minutes  
>> is real time enough for most things like this. So you might be  
>> better served by building and sending out the email digests at  
>> intervals and not with every time a post gets updated.
>>
>> So since you need to use ActionMailer, which in turn needs  
>> ActionView, you will want to pull most of rails into your drb  
>> server so you can make all this happen. So add this line to your  
>> script/backgroundrb/start script at the top after the boot.rb  
>> require line:
>>
>> require File.dirname(__FILE__) + "/../../config/environment.rb"
>
> I've done this...
>
>> Then build a worker class that does the processing and sending of  
>> emails in the do_work method.
>>
>> class EmailWorker < BackgrounDRb::Rails
>>
>>   def do_work(args)
>>      # loop over emails and build digest
>>      # loop over subscribers and send email
>>      #
>>   end
>>
>> end
>
> I did this, too, but my file looks like:
>
> # in lib/workers/checker_worker.rb
>
> class CheckerWorker < BackgrounDRb::Rails
>
>   def do_work(args)
>     logger.debug "BackgrounDRb run at #{Time.now}"


   needs to be @logger


>   end
>
> end
>
>> You will need a cron job to fire off this workers method every 5  
>> or 10 minutes like this:
>>
>> #!/usr/bin/env ruby
>> require "drb"
>> DRb.start_service
>> MiddleMan = DRbObject.new(nil, "druby://localhost:22222")
>
> Here is the file I'm using
>
> # in lib/checker.rb
>
> #!/usr/bin/env ruby
> require "drb"
> DRb.start_service
> MiddleMan = DRbObject.new(nil, "druby://localhost:22222")
> MiddleMan.new_worker(:class => :checker_worker, :ttl => 600)
>
> I think I've set up everything correctly, and that by running  
> checker.rb by typing ruby checker.rb while within the lib  
> directory, the BackgrounDRb object should fire, and the debug  
> message should be posted to the log.
>
> When I run the command, though, I get this error message:
>
> (druby://localhost:22222) /usr/local/lib/ruby/gems/1.8/gems/ 
> activesupport-1.3.1/
> lib/active_support/dependencies.rb:100:in `const_missing':  
> uninitialized constan
> t CheckerWorker (NameError)
> ...

Hrmm.. I wonder if this is caused when you require the config/ 
environment.rb file? Can you test that for me? comment out the  
require File.dirname(__FILE__) + "/../../config/environment.rb" line  
and stop/start the drb server. Then try this again and see if you get  
errors.

Also you need to realize that Backgroundrb requires all your worker  
classes in lib/workers when it starts up. So if you didn't restart  
the drb server after you put the checker_worker.rb file in lib/ 
workers then that would cause this exact problem. So first restart  
your drb server. If that doesnt fix it then try what I stated above  
about the environemtn.rb require. Then report back if you still have  
errors.

THanks
-Ezra



>
> Can you, Ezra, or anyone else help me discover where I've gone wrong?
>
> Thanks very much for your help,
>
> Jacob Patton



More information about the Backgroundrb-devel mailing list