It’s about timing baby!

Posted by Andreas Fast on November 15, 2011

Yeah, it’s about timing.

There was a problem in one of our projects at moove-it related to slow processing. There is a daemon spawning new threads to process certain new entries to the database. The entries come from a different system, that’s the reason for this program which processes each new entry. Sometimes at certain hours of a day there are peaks in the entries to the database and the process will fall behind by about 20.000 entries or more. So we started analyzing the code to understand what was happening and why it took so long. We noted that each new thread the daemon spawned took about 5 seconds to complete its task. As we narrowed the measurement we came up with some code that took 5 seconds to execute but it only involved access to the database. So thanks to Aaron Patterson’s (@tenderlove) talk at RubyConf Uruguay about “Who makes the best asado” where he talked about rails and how it manages threads and database connections, we knew where to look.

What he explained is that each new thread requests its own database connection from the connection pool, and if there isn’t a free connection, rails waits for about 5 seconds and if after that there is no free connection it iterates over all the threads to take back the connections of dead threads(more info). See the correlation with the 5 seconds I talked about in the previous paragraph? We immediately suspected that this was the problem. So we started searching the Rails API for a way to release the connection at the end of each thread’s execution. Surprisingly, we didn’t find an easy and understandable explanation anywhere at first googling ;) , so we digged deeper and came up with the following line:

ActiveRecord::Base.connection_handler.clear_active_connections!

The ActiveRecord::Base.connection_handler method returns the connection handler for the current thread and the clear_active_connections method does what it looks like, or from the Rails doc: “Returns any connections in use by the current thread back to the pool, and also returns connections to the pool cached by threads that are no longer alive.”

So this line returns the connections in use by a thread to the pool and enables the new threads spawned by the daemon to use the freed connections. This way we avoid the 5 second wait for rails to free the connections for us.

This one line picked up our performance from processing 1.000 entries in almost 2 hours to processing 10.000 in 5 minutes. Nice huh?!

That’s it. I’m not sure if this is the best way of doing it since this method also “… returns connections to the pool cached by threads that are no longer alive.” I guess this means it does the iteration over all the threads Aaron mentioned, but as you can see I’m happy with the performance improvement. We are using Rails 3.0.5, Aaron said that he will change the behavior, read more about it here.

Special thanks to @cheloeloelo who helped detecting the problems and digging through the Rails API finding the proper method to free the connections.

Image: Suat Eman / FreeDigitalPhotos.net

Second RubyConf in Uruguay – 11th and 12th November

Posted by Gabriela Isnardi on November 01, 2011

We are sponsoring one of the greatest technology events here in Uruguay. The Second RubyConf taking place within less than two weeks, the 11th and 12th November 2011, where many IT experts from all over the world get together in order to be immersed in this dynamic world and and up to date get with the latest trends of Ruby and Agile methodologies.

RubyConf Uruguay 2011

We are hungry for knowledge and refreshment, and we all want to be on the same train.

Please welcome all the new members to this awesome community. And help spreading the news, but even more important, do not miss the opportunity to meet the experts, discuss the future of RoR, and be Rail!

 

Ruby + Rails + Agile – sharing presentations

Posted by Martin Cabrera on July 09, 2011

The last Wednesday we made a workshop in the ORT University.
Pablo Ifran, Gianfranco Zas, Ariel Ludueña and I

We share the presentations to everybody

All_in_One_Agile_Development (in spanish)

Ruby_and_Rails (in spanish)

Workshop of Ruby + Ruby on Rails + SCRUM at ORT University – July 6, 2011

Posted by Martin Cabrera on June 28, 2011

Moove-IT is now part of the Ruby on Rails Workshops and Conferences around the world.

Even when Ruby seems to be the most popular modern programming language in America, there was nowhere in Uruguay you could actually learn it. Moove-IT will give you a heads up on this exciting language and Rails framework on July 6, 2011 (please see ad below).

This will be the first in a series of workshops focusing on Ruby, RoR, Scrum and hands on training exercises.

We are so proud to have made so much progress working with this technology and methodology that we feel the need to contribute to the local Ruby community.

Join us! Be part.

Ruby on rails for mobiles

Posted by sebastian.sassi on May 06, 2011

Time ago, one of our customers asked us to implement a new web project with support for smartphones. The objective was to support the most common operating systems: iOS, android, blackberry, symbian, with the possibility of using the phone capabilities (gps, sound and vibration).
After researching on different frameworks and technologies we have arrived to some conclusions that I would like to share with people who have had the same dilemma of choosing a good framework to use.
I must say that this research was based on data taken from different sources (both official and unofficial), and also having taken some particular decisions while evaluating the alternatives, that do not have to match with your own criteria.

First of all, we classified the available options into two different categories:

  • ‘Native’ Development frameworks: Rhodes, NimbleKit, Titanium (this is new to the list), PhoneGap
  • Javascript libraries: Sencha, JQTouch, JQuery mobile

Rhodes

According to the official description, The Rhodes application framework allows developers to create native mobile applications with portability of editing HTML templates and the power of the Ruby programming language. Applications written in Rhodes exhibit the performance and richness of apps written to the native device operating systems with local data but enable developers to have the productivity of web interfaces in HTML. Developers write their applications once and they then run on all major mobile device operating systems: iPhone, Windows Mobile, Blackberry and more.

Advantages:

  • Programming in Ruby
  • Full access to the Device API (Application looks different in each device)
  • Several devices supported: IPhone, BlackBerry, Android, Windows mobile
  • RhoSync: RhoSync retrieves data via web services (REST or SOAP) from backend enterprise applications for distribution to downstream mobile devices.

Disadvantages:

  • Licensing: It is a commercial product. Costs arround U$S 500 per project.

NimbleKit

NimbleKit says that it is a framework to create applications for iPhone and iPod touch fast. Programmers do not need to know Objective-C or iPhone SDK. All they need is to know how to write an HTML page with Javascript code.

Advantages:

  • HTML, CSS & Javascript

Disadvantages

  • It only works for iphone, ipods & ipads.

Titanium & PhoneGap

From architectural standpoint, these two frameworks are very similar. Titanium and PhoneGap expose the smartphone features through a set of Javascript APIs, while the application’s logic (html, css, javascript) runs inside a native WebView control. Through the javascript APIs, the “web app” has access to the mobile phone functions such as Geolocation, Accelerometer Camera, Contacts, Database, File system, and so on.

The differences:

PhoneGap does not expose the native UI components to javascript. Titanium, on the other hand, has a comprehensive UI API that can be called in javascript to create and control all kinds of native UI controls. Utilizing these UI APIs, a Titanium app can look more “native” than a PhoneGap app. Second, PhoneGap supports more mobile phone platforms than Titanium does. PhoneGap APIs are more generic and can be used on different platforms such as iPhone, Android and Blackberry. Titanium is primarily targeting iPhone and Android. Some of its APIs are platform specific (like the iPhone UI APIs). The use of these APIs will reduce the cross-platform capability of your application.
If your concern for your app is to make it more “native” looking, Titanium is a better choice. If you want to be able to “port” your app to another platform more easily, PhoneGap will be better.

Advantages:

  • Multi device
  • Programming in HTML, CSS & Javascript
  • MIT License

Disadvantages:

  • Some devices native features are not exposed in the API

Sencha

According to the official documentation, Sencha Touch is the first app framework built specifically to leverage HTML5, CSS3, and Javascript for the highest level of power, flexibility, and optimization. They make specific use of HTML5 to deliver components like audio and video, as well as a localStorage proxy for saving data offline. They have made extensive use of CSS3 in our stylesheets to provide the most robust styling layer possible.
Altogether, the entire library is under 80kb (gzipped and minified), and it is trivial to make that number even smaller by disabling unused components or styles.

Advantages

  • Great UI Controls
  • Multi device

Disadvantages

  • Commercial license (U$S 1.800 for 5 developers)

JQTouch
It is “A jQuery plugin for mobile web development on the iPhone, iPod Touch, and other forward-thinking devices.” [1]

Advantages

  • Great UI Controls
  • Multi device
  • MIT License

Disadvantages

  • Is focused on small screen devices
  • It’s a jquery plugin
  • It was created in 2009 by David Kaneda and has been relatively quiet until release of beta 3 (April 24th 2011).

JQuery Mobile
It is a unified user interface system across all popular mobile device platforms, built on jQuery and jQuery UI foundation. Its lightweight code is built with progressive enhancement, and has a flexible, easily themeable design.

Advantages

  • Multi device (See http://jquerymobile.com/gbs/) (supports much more devices than JQTouch)
  • Very simple to use
  • John Resig, JQuery’s founder is one of the main developers and it is a high activity project
  • It is not a jquery plugin, it is a new framework
  • Dual licensed under MIT and GPL Version 2

Disadvantages

  • It is still in Alpha version

CONCLUSION
After evaluating the different frameworks, we arrived at the next conclusions:
We should use a framework that generates html + css + js, because this technology guarantees a long term support on different devices
We should use a framework that supports access to devices specific capabilities (i.e. sound, gps, camera, vibration, data, etc.) in case we need them in a near future
Because we prefer unrestrictive licenses (MIT, BSD, LGPL and others) our options are reduced to: PhoneGap, Titanium & JQTouch.

Finally, we choose PhoneGap + JQuery mobile because they are open technologies and support multiple platforms. Also, resulting interfaces are similar across different devices and that is a desired feature for our users.
JQTouch and JQuery mobile are awesome frameworks to develop mobile applications, but we prefer to choose JQuery Mobile since it support much more devices (altough it is in alpha version), has high activity and is not just optimized for WebKit as jQtouch does.
We also think that JQuery Mobile is pretty easy to use, the resulting interfaces are usable and has many components to use.

The rest of the story is pretty easy ( just because we use Ruby on Rails :P ), you should create some helpers that adds the JQuery Mobile features to the pages, or use some existent plugin, you can find a starting guide here: http://goo.gl/5Soyq.

I hope you find this post useful, have a nice coding!

[1]: forward-thinking: Someone who is forward-thinking is thinking progressively and possesses the ability to look beyond the “now” and formulate strategies for future success. They are constantly asking “what’s next?”.

Sources:

Sass why is useful

Posted by Pablo Ifran on November 26, 2010

Sass is used for generating css files based on his own syntax, it’s very useful because allows you to avoid repeating code across the different scss files using import (allows you to import code from other scss file), or mixins(scss functions). Also, allows you to write varibles, for using in different scss, this makes easier to do different templates based on colors or images for example.

_sprites.scss

@mixin sprite_for($image_class, $x, $y, $image: "/images/sprite_image.png") {
  .sprite_image.#{$image_class} {
    background: transparent url($image) no-repeat scroll $x $y;
    height:16px;
    width:16px;
  }
}
_images.scss
$my_sprite_image_path: "/images/my_sprite_image_path.png";

default.scss

@import "sprites", "images";

@include sprite_for("my_image", 0, 0, $my_sprite_image_path);

That generates

default.css

  .sprite_image.my_image {
    background: transparent url(/images/my_sprite_image_path.png) no-repeat scroll 0 0;
    height:16px;
    width:16px;
  }

So, this is best sorted using this method, because you have the image path in a single file and you use it in other files only by importing the file with the images path, it’s also simpler to remove an image because you only need to find the references to that variable and delete them.

The sass framework also allows you to use the DRY principle because you can write nested selector, I’ll show you with an example:

default.scss

.some_class {
  width: 100px;
  .some_other_class {
    display: block;
  }
}

default.css

.some_class {width:100px;}
.some_class .some_other_class {display:block;}

Doesn’t affect the performance because it generates a css file at development time.

It also allows you to benefit from some of the advantages of css 3 right now.

Juggernaut: Chat on Rails

Posted by ivan.etchart on November 19, 2010

To implement chat on Rails we need the Juggernaut gem. The newest version of Juggernaut is build upon nodeJS.
1. Install Juggernaut:
First, install nodejs (http://nodejs.org), redis (http:/code.google.com/p/redis/) and Juggernaut gem (gem install juggernaut).
Then download server git clone …
2. Integrate with Rails to implement chat!
After Juggernaut gem is installed, add it to environment.rb:
config.gem "juggernaut"
Suppose you have a system with user authentication, the idea is to generate an environment where you’re able to chat with other users and see to what extent features can be added in order to create an awesome chat!
Note: Providing a step by step guide to create a working chat isn’t the idea of this tutorial. It’s to show different possibilites to develop on
Juggernaut.

Connecting to Juggernaut from Rails

It’s necessary to add the file WebSocketMain.swf to the public folder. This hasn’t to be exactly this way, you can change its location, but if you do, then you’ll have to change the WEB_SOCKET_SWF_LOCATION, due to an issue with Firefox (… for further information visit juggernaut git page).
Juggernaut serves every file necessary for running by default, so you can leave them on the server where Jaggernaut is installed and configure everything in your client (mainly due to Firefox) in order to avoid adding extra files to your project.
Add to your view this line:
<script src="http://localhost:8080/application.js" type="text/javascript" charset="utf-8"></script>
Note : you can add this file in another way or eventually add it to your project if you want.
Inside another script tag:

var jugger = new Juggernaut;
** Connection events provided by Juggernaut
Juggernaut provides you with three different events : Connect, Disconnect o Reconnect.
jugger.on("connect - disconnect - reconnect", callback)
You can use this events to implement whatever function you want to execute, i.e. alerting when a client has connected:
jug.on("connect", function() {alert("I'm connected!!");});
At this point you have the connection of your client ready, you can test it, you have to start Juggernaut. * First run redis :
./redis-server redis.conf

(whereever you have installed it) *

Then go to the folder where you’ve downloaded the server to and run juggernaut:
node server.js
Let’s chat!
Now that we have everything going, would be a good time to start chatting.
First you need to subscribe to a channe. Inside the channel you’ll be able to connect and user messages scope is restricted to channel.
Then :
 jug.subscribe("name_or_channel_id", function(data) { .. here you'll handle all messages coming from channel ... })
Clarifying a bit… with this function we subscribe to the channel and can pass a function as a parameter to handle data coming from it, i.e. any user’s messages, let’s give an example:
First you need to have a div tag with an id (id_div), what you want is to append a list element containing message content to the div element everytime someone sends a message.
jug.subscribe("name_or_channel_id", function(data){
var li = $("<li />");
li.text(data);
$("#id_div").append(li);
});
You can have a div or textearea element, adding text to a textarea element is relatively simpler, it’s enough to do

textarea.value += (data + "\n");

but you’ll lose customizing power. li elements can be enriched with style, making the chat more attractive.

You’ve received data and added to a div element, but you’re still unable to send data. Let’s setup a form:
<% form_remote_tag(:url => path_to_send_message_method, :success => "$('#msg_body').value=''" do >
<= text_field_tag 'msg_body', '', :size => '50' >
<= submit_tag 'Send Message' >
<% end %>
And the method :
def send_message
Juggernaut.publish("name_or_channel_id", parse_chat_message(params[:msg_body], current_user))
end
parse_chat_message? … Yes, before publishing data, you can process it and why not modify it a bit.
def parse_chat_message(msg, user)
return "#{user.login} says: #{msg}"
end
We are sending information to Juggernaut with this format : ‘usuario says: something’
Done!, you have your server up and running, and know how to send and receive data, there’s nothing else to try.
Extras:
Private chat case :*

You can’t call a chat a chat if you can’t have a private conversation with someone. A good way of implementing this, is subscribing your users to a particular chatroom identified by user’s id. Simple add another subscribe:
 jug.subscribe("name_or_channel_id", function(data){ .. handle private message .. }
The receiving user must be able to subscribe to your chat and send messages to you.
To achive that, you should write a function that when you click on the user you want to send a private message to, subscribes to its chatroom and allows you to send messages to the user. This can be implemented in many different ways! You’ll have to simply make your choice according to your needs!

Rails 3: New features & Changes

Posted by Michel Golffed on November 18, 2010

In this post I’ll show you some basic examples about the new features and changes of the web framework Rails 3, for this I’ll use two models: User and Task, having a relationship between them where a User has_many :tasks.

ActiveRecord finder methods

Let’s start by finding the five most recently created users:

Rails 2

User.find(:all, : order => "created_at desc", :limit => 5)
What’s new about this method call in rails 3?
Rails 3 looks at the hash of options that is being passed to find(:all, : order, and :limit) and replace each item in the hash with an equivalent method.

New API methods:

where, having, select, group, order, limit, offset, joins, includes, ...
So, in Rails 3 this becomes:
User.order("created_at desc").limit(5)
Let’s continue with this second example:
Rails 2:
User.find(:all, :conditions => ["created_at <= ?", Time.now], :include => :tasks)
Rails 3:
User.where("created_at <= ?", Time.now).includes(:tasks)
In this example the “where” method substitutes the :conditions parameter.

Named Scopes

Changes to named_scopes in Rails 3:
User model in rails 2 with two named scopes:
class User < ActiveRecord::Base
named_scope :active, :conditions => '!is_deleted'
named_scope :south_american,
:conditions => ["nationality IN (?)", Nationality::south_american_countries]
end
The same in rails 3 would be:
class User < ActiveRecord::Base
scope :active, where('!is_deleted')
scope :south_american,
where("nationality IN (?)", Nationality::south_american_countries)
end
Note that the method that we use to define as named_scope has become to just “scope”. Also, we no longer pass the conditions as a hash but, as with find, we use methods.
Chainability

Another new feature is the ability to build up scopes. If we want to create a scope called “recent”, that will return the most recently created active south american users ordered by their creation date, we can do so by reusing the two scopes we already have.
Chaining together the two scopes we already have and add an order method to create the new scope, is all we need to do.
Here is the result:
scope :recent, active.south_american.order("created_at desc")

ActiveRecord Validation

Separate validations in Rails 2
validates_presence_of :email
validates_uniqueness_of :email
validates_length_of :email, :maximum => 30
All validations together for a field in Rails 3
validates :email, :presence => true,
:uniqueness => true,
:length => {:maximum => 30}
Hope you enjoyed it, some other features of Rails 3 will be published on upcoming posts.

Bullet: a gem to help reduce N+1 queries

Posted by Lucía Escanellas on November 11, 2010

Bullet is a plugin written by Richard Huang, that helps reducing the number of queries an application makes. It was first posted on 2009, but it is still a pretty useful gem to monitor your application for performance improvements.
It has several ways of notifying problems: by Growl notifications, JavaScript alerts by default, and even using XMPP too. Additionally, it saves on its own bullet.log the exact line and stack trace of what caused the alert, and if you want to, it can also write to the application log.

The project is on GitHub: http://github.com/flyerhzm/bullet

You are probably familiar with the N+1 query and cache counter problems, so let’s look how Bullet will detect these by monitoring the database queries.

The N+1 query problem

Suppose we have our application with accounts having many rate plans:

class RatePlan < ActiveRecord::Base
  belongs_to :account
end

If we iterate over the rate plans, accessing rate plans attributes, like:

RatePlan.all.each {
  |post| puts "#{rate_plan.name}, by #{rate_plan.account.name}"
}

Will be using N+1 queries:

  • one query to get all the rate plans
  • then, for each rate plan, one query to get the corresponding account.

(This means that if we have 1000 posts, we’ll be doing 1000+1 queries)

By using the :include option, we can retrieve the rate plans and their corresponding accounts on the same query:

RatePlan.find(:all, :include => :account).each {
   |post| puts "#{rate_plan.name}, by #{rate_plan.account.name}"
}

By detecting if we forgot to use eager loading or a counter cache, Bullet can save time looking for problems on the fly.
For example, Bullet will show this message:

N+1 Query Detected Message

We have to be careful not to abuse the :include option. If the rate_plans table is big enough, it could take too much server memory. Also, we could end up with big, slow requests, or getting from the server a lot of data that won’t be used. Bullet will show a message if it detects an unused eager loading.

Cache counters

Cache counters is another improvement in a one-to-many relationship. Because the _has_many_ relationship defines an attribute that is a collection, if we use the size property on this attribute, it will trigger a select count(*) on the child table. This is generally acceptable, except when we are using frequently count and we end up going to the database unnecessarily.
In order to avoid that, we can use counter caching: Active Record will maintain for a parent table the number of child references. Bullet also shows a message if it finds a cache counter is needed.

Configuration

You have to first install the gem, by adding it to the Gemfile, or by installing manually:

sudo gem install bullet --pre

The next thing is to add some configuration to the development environment. Bullet won’t do anything unless you configure it explicitly, and also note that it’s not a good idea to configure it on the production environment, because on that case your users would also receive those N+1 query alerts.

Configuration allows Growl notifications, and sending messages by XMPP (Jabber), but since the default JavaScript alerts are good enough for me, I configured it like this:

config.after_initialize do
  Bullet.enable = true
  Bullet.alert = true
  Bullet.bullet_logger = true
  Bullet.console = true
  Bullet.growl = false
  Bullet.rails_logger = true
  Bullet.disable_browser_cache = true
end

Note the last option, because disabling browser cache can save you some trouble on some configurations. In any case, if Bullet is not working, try first disabling browser cache.

With this configuration, it will show a JavaScript alert, the same message on the console, and the detail and stack trace on the bullet.log.

Rails security

Posted by Pablo Ifran on October 14, 2010

When you are working with svn (pulling your project from the svn to the web server) and you want to deploy a system into production with apache (mod_rails), you must filter the svn folders (to prevent that other users view your svn files).

To do that task, you must add the following lines to the apache configuration.


<DirectoryMatch "^/.*/\.svn/">
  ErrorDocument 403 /404.html
  Order allow,deny
  Deny from all
  Satisfy All
</DirectoryMatch>