# Fine-tuned scheduling: backgroundrb?

**URL:** <https://www.ruby-forum.com/t/fine-tuned-scheduling-backgroundrb/177892>\
**Category:** Rails\
**Created:** [October 26, 2009, 12:23pm UTC](https://www.ruby-forum.com/t/fine-tuned-scheduling-backgroundrb/177892 "2009-10-26T12:23:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jhaagmans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@jhaagmans](https://www.ruby-forum.com/u/jhaagmans)\
**Post date:** [October 26, 2009, 12:23pm UTC](https://www.ruby-forum.com/t/fine-tuned-scheduling-backgroundrb/177892/1 "2009-10-26T12:23:00Z")

</div>

Hi,

I’d like to make a few requests per second to another method using,  
for example, backgroundrb (any other suggestions are welcome as  
well!).

I have considered using jRuby to enhance the use of threads and I’m  
quite sure that’s the way to go. So what I’d like Rails to do is,  
based on a value in a database, calculate the number of requests it  
has to make in a certain timeframe. For example, when it has to make  
300 requests in 60 seconds, I’d like Rails to execute the method in a  
new thread every 200 milliseconds. It can be off by a bit, but I want  
to make sure it has 25 requests done by 5 seconds and 150 by 30.  
That’s important. It can’t be finished by 50 seconds and it can’t be  
resting for a whole second, for example.

The only solutions I’ve found so far are basically making Rails  
execute the method and then sleep for a given time, but that doesn’t  
feel right. I’m not even sure whether that accepts a time in  
milliseconds.

I hope you can help. Thanks.

---

<div class="post-metadata">

**Author:** ![jhaagmans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@jhaagmans](https://www.ruby-forum.com/u/jhaagmans)\
**Post date:** [October 26, 2009, 11:04pm UTC](https://www.ruby-forum.com/t/fine-tuned-scheduling-backgroundrb/177892/2 "2009-10-26T23:04:42Z")

</div>

I got a little bit further. I could make Rails sleep for just a few  
milliseconds between executing the threads of course. It’s not pretty  
though. If anyone knows of a better way, please tell me!

Will the following give me any problems? I can imagine ActiveRecord  
could clot up when some of the threads write at the same time, or  
won’t I have to worry about that?

Worker FooBar  
def scheduler(queries\_per\_minute, collection)  
sleeptime = 60 / queries\_per\_minute  
collection.each do |item|  
Thread.new do  
executor(collection)  
sleep sleeptime  
end  
end  
end

def executor  
#execute request  
end  
end

Thank you!

---

<div class="post-metadata">

**Author:** ![jhaagmans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@jhaagmans](https://www.ruby-forum.com/u/jhaagmans)\
**Post date:** [October 27, 2009, 1:42am UTC](https://www.ruby-forum.com/t/fine-tuned-scheduling-backgroundrb/177892/3 "2009-10-27T01:42:48Z")

</div>

Well, this all works now! It’s actually quite fast.

Talking about backgroundrb: I’d like to do a few whois requests every  
night for another application and I’m using Ruby’s Whois for that  
([http://github.com/jm81/whois](http://github.com/jm81/whois)). I cannot use that class inside a  
worker. For that to work, I need to create an object for that class in  
my controller and send it to my worker using a MiddleMan. However,  
that’s not how I want to do it, because I want to schedule this using  
my backgroundrb.yml. What am I doing wrong? require ‘whois’ in my  
worker or environment.rb won’t work. Am I overlooking something?

Thank you?
