A Place for my head

On Ruby, Rails, Concurrency and fiction

Archive for the ‘rails’ Category

Posted in : backgroundrb, rails, ruby | 1 comment

Assuming nobody is reading this, I would quietly mention that, I released new version of BackgrounDRb plugin today.
Checkout full announcement here:

http://rubyforge.org/pipermail/backgroundrb-devel/2007-November/001043.html

Here is a sample worker:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class FooWorker < BackgrounDRb::MetaWorker
  set_worker_name :foo_worker
  attr_accessor :count
 
  def create
    puts "Starting Foo Worker"
    @count = 0
    add_periodic_timer(4) { increment_status}
  end
 
  def process_request p_data
    user_input = p_data[:data]
    result = self.send(user_input[:method],user_input[:data])
    send_response(p_data,result)
  end
 
  def increment_status
    puts "Registering status"
    register_status("stuff #{rand(10)}")
  end
 
  def foobar
    puts "Invoking foobar at #{Time.now}"
  end
 
  def add_values user_input
    p user_input
    return eval(user_input)
  end
end
 
=begin
  problems, with existing things.
=end
Posted in : rails, ruby | No comments

I was reading on twitter experience of scaling rails and use of dtrace. Unfortunately for GNU/Linux users “dtrace” is just a unreality. But if you just concentrate on the results, then you will find that, a major source of bottleneck was use of:

  foo = @someobj.amazing rescue "not_amazing"

Above code is in vogue, because it takes care of a lot of things like:

  • if @someobj is nil, then foo defaults to “not_amazing”.
  • if @someobj doesn’t support “amazing” method, foo defaults to “not_amazing”.
  • And of course the obvious case.

But that code succinctness comes at a cost and you may end up with deep stacktraces. This was a major bottleneck for twitter team.
Above snippet can be written as:

foo = @someobject.blank? ? "not_amazing" : (@someobject.respont_do?(:amazing) ? @someobject.amazing : "amazing")

Not so succinct, but does its job.

Read the details http://blogs.sun.com/ahl/entry/dtrace_for_ruby_at_oscon .