Home Home > 2009 > 05 > 14 > Ruby on rails, Ajax and memory watching
Sign up | Login

Deprecation notice: openSUSE Lizards user blog platform is deprecated, and will remain read only for the time being. Learn more...

Ruby on rails, Ajax and memory watching

May 14th, 2009 by

As work on webinterface for YaST is in progress we must learn new technologies suitable for web development. WebYast will be written in ruby and ruby on rails framework. Also because WebYast is new interface it could contain AJAX features for better user comfort. Today I found that ajax support in RoR is on good level and with documentation it takes few minutes to create first example which show current used memory on server. It is not connected anyhow to YaST because I want to focus on AJAX.
And here is a code. It update page every fifth second (but not refresh only update div, on bigger page it is really significant):
/views/home/index.html.erb:
<%= javascript_include_tag :defaults %>
<h1>Hello world!</h1>
<%= periodically_call_remote(:url => { :action => 'get_averages' }, :update => 'avg',:frequency => '5') %>
<div id="avg">
Memory usage is X MB </div>
controllers/home_controller.rb:
class HomeController < ApplicationController
   def index
   end
   def get_averages
     output = `free -m` # bash solution - | sed 's/Mem:[^0-9]+[0-9]+[^0-9]+([0-9]+).*$/1/;2q;1d'`
     output = $1 if (output =~ /.*n(.*)n.*n.*n/) #let live second line
     output = $1 if (output =~ /.*:s+S+s+(S+)s+/) #second field
     render :text => "Usage mem is "+output+"MB RAM."
   end
end

And thats all to watch your server usage.

Both comments and pings are currently closed.

Comments are closed.