[Backgroundrb-devel] Doing max of N tasks per given time

Chad Thatcher chad.thatcher at gmail.com
Tue Jan 27 07:12:49 EST 2009

Hi Ramon,

I'm also new to BGRB but I had a similar problem only very recently  
and I think a variation of my solution might help you.  Here is my  
idea for you (thanks mostly to a even guidance of Hemant himself):

class MailWorker < BackgrounDRb::MetaWorker
   set_worker_name :mail_worker
   def create(args = nil)
     # this method is called, when worker is loaded for the first time

   def create(args = nil)
    @mail_queue = []
    # double check that add_periodic_timer() accepts seconds - if so,  
3600 is one hour.
    add_periodic_timer(3600) { push_mail() }

   def add_mail(args = nil)
     # PS. this method submitted by Hemant, I haven't looked into the  
Struct stuff.
     klass = Struct.new(:cmd_args)
     # add mail to the queue
     @mail_queue << klass.new(args)

   def push_mail
     sleep 10 # I think this is important to make sure that we have  
definitely gone over an hour since the last push!

     # loop through the first 250 !NOTE: must specify a concrete range  
as it is possible that new mails are being queued while this methods  
       task = @mail_queue.shift
       # send mail here
     # end loop

   def check_count
      return @mail_queue.count

You can even add a "is_busy" flag of some sort to make absolutely sure  
this doesn't get run twice at the same time.  One scenario (if its at  
all possible) is that there is a huge delay in the sending of mail,  
meaning the process will lap over the hour.  All sorts of bad things  
could go wrong I suspect.

Anyway, thanks primarily to Hemants' help I hope this helps you too.


On 27 Jan 2009, at 09:14, Ramon Tayag wrote:

> Hey everyone,
> I need some backgroundrb help.  I have a Rails app that connects to an
> SMTP server that can only send up to 250 emails per hour.  If I try to
> send the 251st email, it will just ignore it.
> I almost bashed my head trying to setup sendmail so I can send my own
> emails, but not all emails were being sent.
> Looking at http://backgroundrb.rubyforge.org/scheduling/ , I can see
> that there are many things I can do with backgroundrb, but I can't
> seem to figure out how to do what's written on the subject.
> I basically want to send up to 250 emails immediately, and queue the
> 251st email til after the next hour since the max limit of 250 will be
> reset.  Is this possible with backgroundrb?
> Thanks,
> Ramon Tayag
> _______________________________________________
> Backgroundrb-devel mailing list
> Backgroundrb-devel at rubyforge.org
> http://rubyforge.org/mailman/listinfo/backgroundrb-devel

More information about the Backgroundrb-devel mailing list