Thursday, August 20, 2009

How to use Stomp with activemq

In my previous post on activemq and ROR I have shown how can we use activemq with rest adapter. In this post I will discuss on "how we can use stomp in place or rest".

First you have to install stomp gem
gem install stomp

and you have to just change the broker.yml of configuration folder to use the stomp protocol. Here I am just showing a simple broker.yml

development:
adapter: stomp
login: ""
passcode: ""
host: localhost
port: 61613
reliable: true
reconnectDelay: 5


and rest of the thing is same what i have discussed in my previous post.



Saturday, August 15, 2009

Importance of code review

Few days ago in a seminar one guy was talking regarding code review. At least somebody is talking about importance of code review. Most of the companies don’t have this practice. I like the way that guys had been told about code review but he was pursuing that it is Tester job. But I don’t think it is a job for the tester. Let me discuss why I think so. Let’s answer two questions

  1. Why? (Why we need code review )
  2. How? (How can we do code review)

Why?

If somebody asks me why we need code review then the following things come into my mind
  1. Obviously get rid of some silly bugs
  2. To improve the quality of code and to improve the quality of developers as well
  3. For group learning

Let me explain the things, those I have pointed out with some examples. These examples may seems to be silly but believe me, these sort of things always happen


def update
user = User.find_by_id(params[:id])
user.name = params[:name]
user.save!
end


Here user.find_by_id may return nil value if user_id is not a valid value or don’t have the corresponding record in the database as a result user.name will raise an error and it because it will become something like nil.name. As a result it will break the whole application moreover to get rid of this there is no try catch block. This sort of silly mistakes can be found during the time of code review. And this silly bug with huge impact.

Now let us look at the following example


def completed?
is_completed = nil

if ((self.status == NOT_STARTED)
or (self.status == IN_PROGRESS))
is_completed = false
elsif (self.status == completed)
is_completed = true
else
is_completed = false
end
return is_completed
end


after code review

def completed?
return self.status == completed
end

This code is more readable, number of operation is optimized both in the sense of memory operation and if then block operation and more meaningful. So as a result your code quality has been improved and the developer has learnt it. From the next time that guy will not do the same time of mistake. As a result your developer will come to same stage.

That is a bad example I have put here but its true. On the other hand it is true that some body writes a nice block of code or impalements a new thing of course from that code block other will learn as well. So this is a team learning as well.


How?

Lets talk about how we can do code review.

First let me describe the agile style of code review. After finishing the work, the developer will chose a partner for code review and inform that guy for code review and they sit together and review the code.

But in code71 we found that if we do the code review in this way then the same mistake is done by other developer. I want to say suppose I make some mistakes in this sprint but except the guy who review my code and me, nobody is aware of it and a result in the next sprint somebody else is doing the same mistake. So what we do???

On the last day of sprint we all sit together and setup a projector where we go through the code.

When we start doing this way first we made a check list to review the code you can also think about this. Let me give some key point here.

  1. Naming convention of classes and methods (noun, verb and whether the names are self explanatory )
  2. Whether the code has complex “if else” block
  3. Reduce the number of complex brunches to make it readable as I have show you
  4. Whether the behavior is distributed to the right methods or not. I mean whether one method is too long and moreover is it working with other behavior which can be the responsibility of other method.

I think initially if you go through these things that will be enough. As you go you can add more things with it. May be after couple of sprint you may find that everybody is working in a correct way.

And I hope after few sprint you will find your all team members are on the same level. And team work is extra ordinary.

Tuesday, July 21, 2009

Problem you may find during runing the cronjob for EBS

In one of my previous blog "how to run EBS(create snapshot) by using cron" I have shown how take incremental snapshot by using cron job on a linux machine. But I have found that there are some problem guys are facing when the want to run the cron job.
Let me discuss the most common mistake we are doing when we want to tak the snapshot.

Some guys were questioning when the guys still logged in to the server at this moment that cron job is taking the snapshot perfectly but when he/she logged out from the server it is not taking the snapshot.

Don't forget you have export some variables such as “EC2_PRIVATE_KEY” , “EC2_CERT” , “JAVA_HOME” as environment at the moment of taking the snapshot. When you export a variable that variable temporarily become environment variable for the time you are logged in to the server. As a result when you logged out the cron job does not get those variables.

Some people add these variables to their profile bash rc file. Remember that the system will load that file only when you will logged into the server at that moment these variables will be available for the cron job.

What you can do?

  1. To add those variable to global environment file so that it can become available from every where. But in this case it will be available to every one.
  2. What did I do??? I made another file and export the variables from there and also invoke the “ec2-snapshot-xfs-mysql” file from there. And now I have set this file to my cron job

Let’s have a look how this new file looks like

#!/bin/bash
export EC2_PRIVATE_KEY=path to your pk-XXXXXXXXXX.pem
export EC2_CERT=path to your cert-XXXXXXX.pem
export EC2_HOME=path to your api tools folder /ec2-api-tools
export JAVA_HOME=path to your java jdk bin folder /java-6-sun
path to your /ec2-snapshot-xfs-mysql /mount_folder(in our case /vol ) your_volume_id(vol-xxxxxx)

Suppose the name of the file is test_snapshot is usr/test folder

Then our cron job will be looks like following

* * 1 * * usr/test/test_snapshot


this will take the snapshot on the 1st day of each month.




Friday, July 10, 2009

Active MQ and ROR

Pre-Installation Requirements

1. Java 5

2. Apache

3. JAVA_HOME environment variable must be set.

Installation Process

For Windows :

1. download the latest release of activeMQ from activemq.apache.org/

2. extract the downloaded file

For Linux :

1. download the latest release of activeMQ from activemq.apache.org/

2. tar zxvf activemq-x.x.x.tar.gz

Start ActiveMQ

First go to your activemq directory

> cd [activemq dir]\bin

Change the permission if needed

> chmod 755 activemq

for windows :

> activemq

For linux :

> bin/activemq

or

> bin/activemq > /tmp/smlog 2>&1 &;

For running the process as background process

> nohup bin/activemq > /tmp/smlog 2>&1 &

 
  Testing the process   
    

> netstat -an|find "61616" (for window)

      
                Or
 
           > netstat -an|grep 61616    (for linux)
        
             As default port of activemq is 61616
 

Monitoring Active MQ

From web console anyone can monitor active by simply typing the following url

http://localhost:8161/admin

anyone can further change the activemq configuration by changing the broker configuration URI, activemq default configuration and other thing.

Now configure the ROR projects for using the activemq

In our scrumpad all things are there what we have to do is to change the adapter of broker yml.

Change the adapter to rest

development:

adapter: rest

port: 8161

host: localhost


rest adapter is in the plug in at this moment. Download the rest.rb from http://people.apache.org/~dennisbyrne/a13g/rest.rb

and copy that file to

vendor/plugins/activemessaging/lib/activemessaging/adapters/




In my next post I will show how can we use STOMP in place of REST adapter

Monday, June 22, 2009

inplace editor and restful

while working on scrumpad the next generation project management tools for scrum we stated that project on ROR 1.8.6. while we decided to update the project to ROR 2.3.2 and make it restful at this time we found that inplace editor is not working for update or edit. because its default method is set to 'get'. So we have to change the following lines of the controls.js under the public/javascript folder to make it workable with the restful architecture.

handleFormSubmission: function(e){

// these lines we have changed of this method (line 636)
new Ajax.Updater({ success: this.element }, this.url, options);
} else {
var options = Object.extend({ method: 'get' }, this.options.ajaxOptions);
}

to
handleFormSubmission: function(e){

// after changing
new Ajax.Updater({ success: this.element }, this.url, options);
} else {
var options = Object.extend({ method: ((this.options.method != null) ? this.options.method.toString() : 'get') }, this.options.ajaxOptions);

}
and its works fine. for inplace editor with the restful for updating a value.

Saturday, May 9, 2009

take a snapshot of a video using ffmpeg

you can use ffmpeg to take the snapshot of a video using ffmpeg. following code block will show you how to do this.


function makethumbnail()
{
//this is the path of the video that you want to take snapshot
$video = $this->baseDir."/".$this->video_name;
// where you'll save the image
JFolder::Create($this->baseDir."/images");
// after how many second you want to take the snapshot
$second= $this->image_duration;
//this the duration of the video
$duration = $this->getDuration();

$no_of_image = intval($duration /$second);

for($i=1; $i<=$no_of_image; $i++)
{
$image = $this->baseDir."/images/".$i.'.jpg';

$cmd = "$this->ffmpeg -i $video -deinterlace -an -ss $second -t 00:00:01 -r 1 -y -vcodec mjpeg -f mjpeg $image 2>&1";
$return = `$cmd`;
$second += $this->image_duration;
}
}


these lines of code will take the snapshot after a particular time

Friday, April 3, 2009

Deploy Multiple ROR sites on IIS

To deploy multiple ROR sites we will need help of Mongrel Server and ISAPI Rewriter. To run Multiple ROR please follows the steps.

Install Mongrel server

  1. First enter into your Ruby folder from command prompt. For an instance if your installed ruby folder is c:\ruby then enter into that folder
  2. Then run the following command to install mongrel
   c:\ruby>gem install mongrel-1.0.1-mswin32 
                                             or 
        c:\ruby>gem install mongrel-1.0.1  
and chose the version of mswin32
  1. Then you have to install mongrel service by using the following command
           c:\ruby>gem install mongrel_service

Install ISAPI Rewriter

1. run the isapi rewriter installer

Patch Action Controller - CGI Process

When performing a re-direct the header is inspected for the real host when request has been forward, however rails does not strip any extra white space.

# Patch cgi_process.rb
  C:\ruby> cd lib\ruby\gems\1.8\gems\actionpack-1.13.3\lib\action_controller
  C:\ruby> edit cgi_process.rb
      # Line ~83: forwarded.split(/,\s?/).last ==> forwarded.split(/,\s?/).last.strip # strip extra spaces/tabs 

Host Using Mongrel service

Perform the following steps to host rails application using mongrel

NOTE: We will be hosting this rails application under the relative url "/rapp"

NOTE: We only bind to (127.0.0.1), since IIS will eventually forward locally to mongrel. The port chosen is 4004

  1. C:> mongrel_rails service::install -N rails_rapp -a 127.0.0.1 -c C:\rails\rapp -p 4004 -e production --prefix /rapp


2.    C:> sc config rails_rapp start= auto
3.    C:> net start rails_rapp
 
#To remove the service use: mongrel_rails service::remove -N rails_rapp

Test using mongrel service

Browse to http://localhost:4004/rapp/hello/world, Note all url's are now relative to rapp, This allows multiple rails apps to be hosted behind iis

Now Host Behind IIS

1. Open your IIS manager

2. Goto-> Default Web Site -> New -> Virtual Directory

Alias/Name -> rapp

Directory/Path -> C:\rails\rapp\public

Permissions -> Read + Scripts + Executables

Test static portion

Browse to http://localhost/rapp/hello/world - You should see the standard welcome to rails page

Configure ISAPI_Rewrite3 to forward to Mongrel

Confiure ISAPI_Rewrite to forward all http://host/rapp/* to mongrel on http://127.0.0.1:4004/rapp/*.

NOTE: We do not forward requests containing a full stop (.), So files like .js and .css etc will be serviced up by iis

   1.   Open configuration file of ISAPI_Rewriter (httpd.conf or INI)  
        Click -> Edit
        Enter your new rule
           RewriteEngine on
           RewriteBase /
           RewriteProxy rapp/([^.]+)$ http://127.0.0.1:4004/rapp/$1
 
   2.   Rails .htaccess legacy
Remove/Rename the c:\rails\rapp\public\.htaccess file, as this is read by ISAPI_Rewrite but is designed for Apache !!!
 
 

Rename the .htaccess file of your rails project to apache.htaccess

Restart IIS

Restar your IIS server

And browse http://localhost/rapp/hello/world

For another project we have to follow the steps from “Host Using Mongrel service” with new prefix, new port number and a new service name.

NOTE: Because of the prefix the site does not get the image’s URLS and javascript’s URLS properly. So it does not show the images properly. (If we don’t use prefix in that case, in ISAPI_Rewrite the Two sites will be redirect to same configuration file and it will not work. So we have to spend some more time investigate on this two issues)

Monday, March 23, 2009

video duration using ffmped

you can get the video duration in second using ffmped following code block will show you how to do it.

function getDuration()
{
//this is the path of the video that you want to get the file duration
$filepath = $this->baseDir."/".$this->video_name;
ob_start();
passthru("$this->ffmpeg -i \"". $filepath . "\" 2>&1");
$duration = ob_get_contents();

ob_end_clean();
preg_match('/Duration: (.*?),/', $duration, $matches);
$duration = $matches[1];
$duration_array = split(':', $duration);
$duration = $duration_array[0] * 3600 + $duration_array[1] * 60 + $duration_array[2];
return $duration;
}

this will return the duration in second

Monday, February 23, 2009

convert a video file to FLV format using FFMPEG

you can user FFMPEG to convert a video file to flv format. following is the code block that will show you how to convert a video to flv format.


function makeflv()
{
//this is the path of the video file that you want to convert
$video = $this->baseDir."/".$this->video_name;

//this is the path of the converted flv file with name
$flv_video = $this->baseDir."/".strtok($this->video_name,".").'.flv';

$cmd = "$this->ffmpeg -i $video -deinterlace -ar 44100 -r 25 -qmin 3 -qmax 6 $flv_video";
$return = `$cmd`;

$flv = strtok($userfile_name,".").'.flv';

return $flv;
}



this will return the converted flv file