<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <description>Artha42 Blog</description>
    <link>http://www.artha42.com/blog</link>
    <title>Artha42 Blog</title>
    <item>
      <title>Behavior Driven Development with Cucumber</title>
      <link>http://www.artha42.com#/blog/behavior_driven_development_with_cucumber</link>
      <pubDate>Mon, 07 Mar 2011 09:31:04 -0000</pubDate>
      <guid>http://www.artha42.com/blog/behavior_driven_development_with_cucumber</guid>
      <description>
        <![CDATA[<p><a href="http://cukes.info">Cucumber</a> is a good  <a href="http://en.wikipedia.org/wiki/Behavior_Driven_Development">Behavior Driven Development</a> tool used for testing your application.  I think it's the only framework that will enable both developers and non-technical people to write test cases. Isn't this pretty neat? In order to work with cucumber, the basic thing you need to have is good English. That's all.</p>

<p>It's very easy to setup Cucumber. First create your rails application
(we are using Rails 3.0.3), here we need to skip over creating the test directory by giving the following command:</p>

<pre class="brush: rb">  
 $ rails new projectname -T
</pre>


<p>-T option will skip the Test::Unit framework as we plan to use RSpec.</p>

<p>Next add this to your Gemfile:</p>

<pre class="brush: rb">  
    group :test do
      gem 'rspec-rails'
      gem 'cucumber-rails'
      gem 'capybara'
      gem 'database_cleaner'
    end 
</pre>


<p>For a Sinatra Application, you need to install the following gem:</p>

<pre class="brush: rb">
gem 'cucumber-sinatra'
</pre>


<p>Then install everything using bundler.</p>

<pre class="brush: rb">  
 $ bundle install
</pre>


<p>The above installation will provide two generators.
To setup cucumber in your Rails application, use the following commands:</p>

<pre class="brush: rb"> 
  cucumber:feature
  cucumber:install
</pre>


<p>We are using capybara for testing ajax, java-script. Since the integration testing would involve testing ajax/javascript and rails/sinatra, we would need both capybara and cucumber. Capybara has built-in drivers such as rack-test, Culerity, Celerity and Selenium for running your tests.</p>

<p>For testing a rails application, we would need <a href="https://github.com/jnicklas/capybara">capybara</a> and <a href="https://github.com/aslakhellesoy/cucumber-rails">cucumber-rails</a>  for testing our rails application.<br/>
The below command will generate the necessary code to use capybara and rspec with cucumber for your rails project.</p>

<pre class="brush: rb"> 
$ rails g cucumber:install --capybara --rspec
</pre>


<p>For testing a Sinatra Application, we need to give the following command to set the cucumber environment:</p>

<pre class="brush: rb"> 
$ cucumber-sinatra init Myapp lib/my_app.rb 
</pre>


<p>It will set the environment for cucumber. This command will generate the step definition and support folders. In the step definition folder, we have a file named web_steps.rb, which provides a broad set of steps that can be used in our specific steps file. In the support folder we have two files env.rb and paths.rb. This env.rb is a configuration file and paths.rb is used for mapping the corresponding path in the feature file. Feature file will have the scenarios in plain english. The <steps>.rb file contains the corresponding code for the scenarios in the feature file.</p>

<p>After setting the environment we need to create a feature file. That is, we can use cucumber generator for generating feature</p>

<pre class="brush: rb"> 
$ rails g cucumber:feature user email:string password:string confirm_password:string
</pre>


<p>It is generally recommended that you do not create feature by using generators. It's better to write your own feature file. It will help you to define the scenarios on your own and helps in turn in implementing step definitions for the scenarios.</p>

<p>Let us consider an example. I am going to test the signup feature for my application. Since it's generic for any application, I took this as an example.</p>

<p>First we are going to describe the behavior in plain text. That is, create a feature file for signup. In this example, I am going to test one scenario while signing up. The user enters input in the following fields-e-mail, password and password confirmation. If the input in password and password confirmation fields do not match, an error message should be displayed. I am going to write a test case for this.</p>

<pre class="brush: rb"> 
#sign_up.feature
Feature: Sign up
As an unauthorized user
I want to signup with my details
So that I can login

Scenario: Password doesn't match confirmation
  Given I am on the signup page
  When I fill in "Email" with "manisiva19@gmail.com"
  And I fill in "Password" with "Secret"
  And I fill in "Password confirmation" with "Password"
  And I press "Sign Up" 
  Then the Sign up form should be shown again
  And I should see "Password doesn't match confirmation"
  And I should not be registered
</pre>


<p>Next we need to run the features by the following command:</p>

<pre class="brush: rb">
#To run full test suite
$ rake cucumber

#To run a single feature
$ cucumber features/signup.feature
</pre>


<p>When you run the above command, you will get the following output in your console:</p>

<pre class="brush: rb">
$ cucumber features/sign_up.feature
Using the default profile...
F-------

(::) failed steps (::)

Can't find mapping from "the "signup" page" to a path.
Now, go and add a mapping in /home/manivannan/myproj/Projects/test/features/support/paths.rb (RuntimeError)
./features/support/paths.rb:36:in `rescue in path_to'
./features/support/paths.rb:30:in `path_to'
./features/step_definitions/web_steps.rb:20:in `/^(?:|I )am on (.+)$/'
features/sign_up.feature:7:in `Given I am on the "/signup" page'

Failing Scenarios:
cucumber features/sign_up.feature:6 # Scenario: Password doesn't match confirmation

1 scenario (1 failed)
8 steps (1 failed, 7 skipped)
0m0.362s
</pre>


<p>The output is usually color coded to indicate failures. Now we need to map the corresponding path in paths.rb. It looks like as follows,</p>

<pre class="brush: rb">
#paths.rb
      when /signup/
        new_user_registration_path
</pre>


<p>I had used <em>devise</em> (ruby gem) for signup, and so I give the corresponding path. new_user_registration_path is generated when I use devise_for:users in the routes file.</p>

<p>After mapping the path, run the feature again as follows:</p>

<pre class="brush: rb"> 
$ cucumber features/signup.feature
Using the default profile...
.....U-U

1 scenario (1 undefined)
8 steps (1 skipped, 2 undefined, 5 passed)
0m2.295s

You can implement step definitions for undefined steps with these snippets:

Then /^the Sign up form should be shown again$/ do
  pending # express the regexp above with the code you wish you had
end

Then /^I should not be registered$/ do
  pending # express the regexp above with the code you wish you had
end
</pre>


<p>It will indicate the undefined steps for your feature. You just need to copy and paste it in the step definition file for signup feature. This is how my step definition file looks.</p>

<pre class="brush: rb"> 
#sign_up.rb
Then /^the Sign up form should be shown again$/ do
  get "users/new"
end

Then /^I should not be registered$/ do
  assert_nil User.find_by_email("manisiva19@gmail.com")
end
</pre>


<p>Now run the test again,</p>

<pre class="brush: rb"> 
$ cucumber features/sign_up.feature
Using the default profile...
........

1 scenario (1 passed)
8 steps (8 passed)
0m1.477s
</pre>


<p>Now you can see that all the tests have passed. Hurray!</p>

<h2>Testing Javascript &amp; AJAX</h2>

<p>While testing an application, it is hard to test the javascript and AJAX. But, cucumber makes it easy. Since capybara is integrated with cucumber-rails, capybara takes care of these javascript and AJAX using various drivers such as Selenium or Culerity/Celerity.</p>

<p>We will cover the topic of Javascript and AJAX in a different post. Go and play around your application using Cucumber, RSpec and Capybara and let me know if you like it.</p>
]]>
      </description>
    </item>
    <item>
      <title>A 404 maybe - A Heisenbug Story</title>
      <link>http://www.artha42.com#/blog/a_heisenbug_story</link>
      <pubDate>Fri, 24 Dec 2010 14:07:30 -0000</pubDate>
      <guid>http://www.artha42.com/blog/a_heisenbug_story</guid>
      <description>
        <![CDATA[<p>We were deploying our webapp on one of the client's servers today. Our webapp is frontended by Nginx which then proxies to a bunch thin servers. Initially everything just worked fine. We cloned the repo from github, copied the nginx config to sites-enabled, ran the rake task to compile our Sass files to CSS files and start the thin servers. The app ran without a hitch. All this time we had been logged in to the system and watching htop, tailing logs and such. Once we were convinced that everything was working fine, we shot an email to the client that they could start using the server now and logged out of the ssh session.</p>

<p>The moment we logged out, the stylesheets and other static assets were not being served by nginx. The client called up and said that the UI looks screwy. We visited the site and we could not believe that all the static resources where throwing up 404s. We logged in again to the machine wondering if any of us had accidentally deleted the files but we found that they were all there. We checked the server again and we found everything was working. We looked at the logs and we found that when we were logged in the requests are served directly by Nginx however when we were logged out of the ssh session, the requests were not served by nginx and as a fallback were attempted to be served by the thin cluster, which failed as well.</p>

<p>This was really odd. They were just static assets - images, js and css. And the worst part is that any attempt made to study the bug rectified it. We knew were dealing with a <a href="http://en.wikipedia.org/wiki/Unusual_software_bug#Heisenbug">Heisenbug</a>. We figured that there had to be something that was running when we login and killed when we log out. We looked at .bashrc, .profile and everywhere else. We could not find it. Out of sheer guess work, we looked at /etc/mtab. There it was the home directory of the user was encrypted using <a href="https://launchpad.net/ecryptfs">ecryptfs</a>. We maintain different apps on the servers under different user accounts. During ubuntu server installation the person who installed it from the client end had chosen to encrypt the home folder by mistake. Thin was serving fine as it had loaded the code on to memory while Nginx read files on demand and was unable to serve any static assets.</p>

<p>All we had to do was move the code out of the home folder and it started serving fine. One heisenbug successfully squashed. Or as <a href="http://www.phdcomics.com/comics/aboutcomics.html">Cecelia</a> likes puts it, we squished it with a 10 ton hammer.</p>

<p><a href="http://www.phdcomics.com/comics/archive.php?comicid=180" target="_blank"><img src="http://www.phdcomics.com/comics/archive/phd042800s.gif" title="From phdcomics.com"/></a></p>

<p>Comic shamelessly stolen from <a href="http://phdcomics.com">PhD Comics</a>.</p>
]]>
      </description>
    </item>
    <item>
      <title>Frankenware</title>
      <link>http://www.artha42.com#/blog/frankenware</link>
      <pubDate>Wed, 22 Dec 2010 10:59:01 -0000</pubDate>
      <guid>http://www.artha42.com/blog/frankenware</guid>
      <description>
        <![CDATA[<p>A recent post on <a href="http://www.mcsweeneys.net/links/dreamjobs/dreamjobs5.html">McSweeney's blog</a> struck a chord with me. We are building a Personal Health Record for one of our clients and we're having a tough time integrating with HIS systems with a particularly large hospital chain in India. The HIS app is based on Windows and Microsoft.NET with code written in VB.net which was auto-migrated from VB 6. They construct SQL strings from forms and the app has 1500 tables of which only 50-70 tables are actually used. The application is rife with all sorts of security issues and is a nightmare to maintain. The design, if I may call it so, looked more like a result of natural selection rather than a well engineered product. You know humans have several unnecessary body parts and mechanisms as vestiges of our evolution, the HIS was no different from that. Most of the tables and forms were there because they were part of the evolutionary process and they had to fight the brutal evolutionary cycle to survive on the front-desks and the database server.</p>

<p>On closer inspection of the code, one can only conclude that the HIS was written by a bunch of intoxicated simians. It seems that their manager was aiming for the <a href="http://xkcd.com/323/">Ballmer peak</a> but over shot it by a fair amount.</p>

<p><a href="http://xkcd.com/323/" target="_blank"><img src="http://imgs.xkcd.com/comics/ballmer_peak.png" title="XKCD: ballmer peak" alt="XKCD: Ballmer Peak"/></a></p>

<p>There are a few gems that I want to share with you. Guess where the bill details are stored</p>

<ul>
<li>bill_main</li>
<li>bill_details</li>
<li>opd_bill_main</li>
<li>opd_bill_main_details</li>
<li>opbill</li>
<li>ipbill</li>
<li>ipd_bill_main</li>
<li>ipd_bill_main_details</li>
<li>dbo.xxxx_bill (where xxxx is a random 0-7 characters string)</li>
</ul>


<p>The answer: Depending on when the patient enrolled</p>

<p>This table is for storing "special" test results.</p>

<ul>
<li>lab_regno</li>
<li>some</li>
<li>irrelevant</li>
<li>fields</li>
<li>item1</li>
<li>value1</li>
<li>item2</li>
<li>value2</li>
<li>...</li>
<li>...</li>
<li>item100</li>
<li>value100</li>
</ul>


<p>The values may be or may not be stored in contiguous item numbers. I was interested how this came about and after a little digging around I got the answer. They copy-pasted existing  forms to create new forms and did not want to alter the binding of the old controls so the created new ones and deleted the old ones. In short, you need to know which columns to select depending on the test that you want to load.</p>

<p>This one is a classic. Each one of the modules is a separate executable but is supposed to act like a single app. Once you click on a menu item or a button it is supposed to open a new executable. Guess where the state is stored. Exactly... the registry. If you locate the individual executable and open it, no authentication, viola. But the IT department has given strict orders that their front-desk guys login with the shortcut on the desktop.</p>

<p>In the end, you feel like gouging out your eyes after seeing such frankenware.</p>

<p><a href="http://www.flickr.com/photos/28979809@N03/2707499884/" target="_blank"><img src="http://i.imgur.com/2VaiW.jpg" alt="" title="Hosted by imgur.com" /></a></p>

<p>I cannot believe that people are dumb enough to buy such software. So, what should a IT manager do before they buy software? You should read <a href="http://www.joelonsoftware.com/articles/fog0000000043.html">Joel Test</a>. If the company does not follow these practices, do not buy their software. I repeat. <strong> DO NOT BUY THEIR SOFTWARE </strong>. You will be making this world a much better place and saving a lot of good developers from undeserved grief.</p>

<p>Images licensed under creative commons. Click on the images to know the source.</p>
]]>
      </description>
    </item>
    <item>
      <title>Jack and the Beanstalkd</title>
      <link>http://www.artha42.com#/blog/jack_and_the_beanstalkd</link>
      <pubDate>Mon, 20 Dec 2010 05:09:30 -0000</pubDate>
      <guid>http://www.artha42.com/blog/jack_and_the_beanstalkd</guid>
      <description>
        <![CDATA[<p>Every webapp needs to do some background tasks like sending emails, processing stats, creating PDF documents and so on. The rule of the thumb is that if a requests takes more than a couple of seconds to process, turn it into a background process. There are several alternatives in the ruby world. From <a href="https://github.com/tobi/delayed_job">delayed_job</a> to <a href="http://www.rabbitmq.com/">RabbitMQ</a>. However, we at <a href="http://www.artha42.com/">Artha42</a> love simplicity. One such really simple solution for processing background jobs is <a href="http://kr.github.com/beanstalkd/">beanstalkd</a>.</p>

<blockquote><p>Beanstalk is a simple, fast work queue.</p>

<p>Its interface is generic, but was originally designed for reducing the latency of page views in high-volume web applications by running time-consuming tasks asynchronously.</p></blockquote>

<p>It has a simple text based protocol inspired by memcached to put in messages that can then be run with workers. It is persistent and most of its clients support connections to multiple beanstalkd servers. There are different choices of ruby clients. While there is the standard ruby <a href="http://beanstalk.rubyforge.org/">beanstalk client</a>, there is also an asynchronous EventMachine version called <a href="http://dj2.github.com/jack/doc/index.html">em-jack</a>. You can choose your pick depending on the type of jobs. If the jobs are IO bound and not too much processing, EM-Jack is a good choice. If the jobs are fairly performance intensive, then you are better off using the standard loopy constructs or implement your own threaded client.</p>

<h4>Tubes</h4>

<p>In addition to the standard global queue, beanstalk supports the concept of tubes. Tubes are independent job queues. You can imagine an app having multiple producers each pushing jobs to a queue and multiple consumers consuming and processing the jobs from these queues. This is not as exhaustive as RabbitMQ's queues, exchanges and keys but it is simple enough for most web apps.</p>

<h4>Usage</h4>

<p>Connecting to beanstalkd and creating a job is is fairly simple.</p>

<pre class="brush: rb">
email={:to=>['email@example.com'], :subject=>'Some Subject', :body=>body}
beanstalk = Beanstalk::Pool.new(['localhost:11300'])
beanstalk.use "emailtube"
beanstalk.yput(email) # yput converts the object to yaml before putting it in the queue
</pre>


<p>Processing the job is equally simple.</p>

<pre class="brush: rb">
beanstalk = Beanstalk::Pool.new(['localhost:11300'])
beanstalk.use "emailtube"
loop do
  job = beanstalk.reserve
  email=YAML.load(job.body)
  # send the email
  job.delete
end
</pre>


<p>We have used beanstalkd in a couple of applications as a replacement to RabbitMQ. And so far, we have had a great experience using it. Try it out and let me know if it works for you.</p>
]]>
      </description>
    </item>
    <item>
      <title>How to store passwords</title>
      <link>http://www.artha42.com#/blog/how_to_store_passwords</link>
      <pubDate>Mon, 13 Dec 2010 12:08:10 -0000</pubDate>
      <guid>http://www.artha42.com/blog/how_to_store_passwords</guid>
      <description>
        <![CDATA[<p>Recently Gawker's database got <a href="http://lifehacker.com/5712785/faq-compromised-commenting-accounts-on-gawker-media">hacked</a>. About 1.3 million passwords are out in the open. This has led to much furore on various sites. In fact, (some sites)[http://nakedsecurity.sophos.com/2010/12/13/gawker-gizmodo-lifehacker-password-change/] have suggested users to set strong passwords and not reuse passwords.</p>

<p>Although this is good advice for users, I would like to give 3 important points for developers storing passwords in their database.</p>

<ul>
<li>Use BCrypt</li>
<li>Use BCrypt</li>
<li>Use BCrypt</li>
</ul>


<p>BCrypt is a hashing scheme based on the Blowfish algorithm. It is excellent for password hashing as it is dog slow. The usual general purpose hashing algorithms like MD5 or SHA1 are really fast. They are meant to hash a lot of content and return a unique hash efficiently. This makes them an easy target for brute force attacks. Bcrypt on the other hand is very slow. On my machine, it is about 4 orders slower than MD5 when the number of rounds is 10. This is what wikipedia has to say about <a href="http://en.wikipedia.org/wiki/Crypt_(Unix)#Blowfish-based_scheme">bcrypt</a>.</p>

<blockquote><p>Blowfish is notable among block ciphers for its expensive key setup phase. It starts off with subkeys in a standard state, then uses this state to perform a block encryption using part of the key, and uses the result of that encryption (really, a hashing) to replace some of the subkeys. Then it uses this modified state to encrypt another part of the key, and uses the result to replace more of the subkeys. It proceeds in this fashion, using a progressively modified state to hash the key and replace bits of state, until all subkeys have been set.</p>

<p>Provos and Mazieres took advantage of this, and actually took it further. They developed a new key setup algorithm for Blowfish, dubbing the resulting cipher "Eksblowfish" ("expensive key schedule Blowfish"). The key setup begins with a modified form of the standard Blowfish key setup, in which both the salt and password are used to set all subkeys. Then there is a configurable number of rounds in which the standard Blowfish keying algorithm is applied, using alternately the salt and the password as the key, each round starting with the subkey state from the previous round. This is not cryptographically significantly stronger than the standard Blowfish key schedule; it's just very slow.</p>

<p>The number of rounds of keying is a power of two, which is an input to the algorithm. The number is encoded in the textual hash.</p></blockquote>

<p>Ruby developers can use <a href="http://bcrypt-ruby.rubyforge.org/">bcrypt-ruby</a>. bcrypt-ruby also salts your passwords to ensure that it is safe from <a href="http://en.wikipedia.org/wiki/Rainbow_table">Rainbow Table</a> attacks. You can install it using the following command.</p>

<pre class="brush: bash">
$ gem install bcrypt-ruby
</pre>


<p>And here is a quick example of how to use it.</p>

<pre class="brush: ruby">
require 'bcrypt'

class User
  include BCrypt
  attr_accessor :password_hash

  def password
    @password||=Password.new(password_hash)
  end

  def password=(passwd)
    @password = Password.create(passwd)
    password_hash = @password
  end

end

#usage

user = User.new
user.password = "password"

user.password == "password" # true
</pre>


<p>Let us make the web safer for our users.</p>
]]>
      </description>
    </item>
    <item>
      <title>NoSQL vs SQL</title>
      <link>http://www.artha42.com#/blog/nosql_vs_sql</link>
      <pubDate>Wed, 08 Dec 2010 01:15:39 -0000</pubDate>
      <guid>http://www.artha42.com/blog/nosql_vs_sql</guid>
      <description>
        <![CDATA[<p>There has been a lot of talk about NoSQL vs SQL. People talk as if NoSQL is just one database. It is not, it is a whole slew of databases that do not use SQL as its querying language. But more importantly there are SQL based datastores like <a href="http://voltdb.com/">VoltDB</a> and <a href="http://www.nimbusdb.com/NimbusDb.html">NimbusDB</a> that do not have the same operational characteristics of a SQL datastore as imagined by the proponents of these systems.</p>

<p>There are several aspects to consider before selecting a database solution for your app. In fact, it has come to point that if your application is sufficiently complex, then you would have to use more than one database for your app depending on the kind of problem you would like to solve. To be clear, databases like MySQL and Postgres have served us well for the larger part of this century and will continue to do so. The class of problems that require a different datastore are statistically outliers. But many of the problems that were eariler solved with standard databases with intelligent hacks can now be solved with a wide variety of NoSQL databases.</p>

<p>Before understanding the why of each database, you need to understand the CAP theorem.</p>

<blockquote><p>The CAP theorem, also known as Brewer's theorem, states that it is impossible for a distributed computer system to simultaneously provide all three of the following guarantees:</p>

<ul>
<li>Consistency (all nodes see the same data at the same time)</li>
<li>Availability (node failures do not prevent survivors from continuing to operate)</li>
<li>Partition Tolerance (the system continues to operate despite arbitrary message loss)</li>
</ul>


<p>According to the theorem, a distributed system can satisfy any two of these guarantees at the same time, but not all three.
<a href="http://en.wikipedia.org/wiki/CAP_theorem">(from wikipedia)</a></p></blockquote>

<p>Most of the NoSQL solutions focus on the AP slice instead of consistency. However, databases like VoltDB focus on CA. HBase chooses CP and so on and so forth. I will not bore you with the details but you can read all about the charecteristics of your favorite database at <a href="http://nosql.org">http://nosql.org</a>.</p>

<p>But you need to understand your requirement before you can pick your database. Most of the SQL databases wait for a <a href="http://en.wikipedia.org/wiki/Two-phase_commit_protocol">2-phase commit</a>. It is a blocking call. If you have a website or a solution with hundreds/thousands of users with only a few number of concurrent connections, you might still be fine. However, you have to understand that each write is basically a blocking operation. So, if you have an application that is write intensive, then you pretty much have to wait on the database to write to the disk and update the indices for every request. This is the case for most apps. Since, we are designing apps which we can control, now organizations can harvest a lot of data like usage patterns and such. Writing such data to a SQL database during the request is wasteful and considered extremely harmful for your user experience. Now it is easy to understand why so many NoSQL systems like <a href="http://couchdb.apache.org/">CouchDB</a>, <a href="http://mongodb.org">MongoDB</a> and others sacrifice on consistency and focus on availability and performance.</p>

<p>One of the other things that you would want to choose a NoSQL system is for its flexibility with schema design. Assume that you are running a web app where you would want users to store custom fields for the same data model. Usually you will have to provision a database for each customer as their schema would change. Or, if you are using a slightly more evolved database like Postgres, you can use <a href="http://www.postgresql.org/docs/current/static/ddl-schemas.html">schemas and schema search paths</a>. But then you cannot collate results across different schemas or different customer databases. Depending on your requirement, you may or you may not have to. But using a NoSQL datastore like CouchDB or MongoDB, you can have a flexible schema.</p>

<p>The third thing about NoSQL systems is the ability to perform DB level operations in a distributed manner. Yes I am talking about the elephant in the room - Map/Reduce. Some of the NoSQL databases include a computational layer that can scale and run in a distributed manner across many machines. Databases like CouchDB push it to the extreme while a few like MongoDB use it in moderation. But this is an important piece nevertheless. You can always replicate this by rolling out your hadoop cluster. But I feel going the DB way might be more efficient. After all, the data is already there.</p>

<p>All in all, I believe that NoSQL and SQL both have its uses. It is no more NoSQL vs SQL it is NoSQL and SQL. In fact, I am looking forward to new application frameworks that support polyglot persistence.</p>
]]>
      </description>
    </item>
    <item>
      <title>Being Relentlessly Resourceful</title>
      <link>http://www.artha42.com#/blog/being_relentlessly_resourceful</link>
      <pubDate>Mon, 06 Dec 2010 07:26:43 -0000</pubDate>
      <guid>http://www.artha42.com/blog/being_relentlessly_resourceful</guid>
      <description>
        <![CDATA[<p>Today we went through a bad infrastructure day. It is raining cats and dogs in chennai. The fall out of which are the following. Both the BSNL and Airtel lines are down. One phase of power is down. And all of these had to happen on the same day. Imagine the odds. The funny part is, on this particularly cool day in chennai, all the Air Conditioning points work but some of the power sockets on the wall decided not to work. But we are hackers, we are not going down that easy. After all, we were all there. Ananth was drenched completely wet when he came in. So we decided to work from couches and bean bags. Threw in a couple of extension boxes and configured one of our laptops as a DHCP server and connected it to our WiFi and shared the Tata Indicom Photon+ (broadband USB) from that.</p>

<p>Usually configuring a DHCP server and a firewall for connection sharing is a pain. But Ubuntu has a piece of software called <a href="http://www.fs-security.com/">FireStarter</a>. A small quote from their website.</p>

<blockquote><p>Firestarter is an Open Source visual firewall program. The software aims to combine ease of use with powerful features, therefore serving both Linux desktop users and system administrators.</p>

<p>We strongly believe that your job is to make the high level security policy decisions and ours is to take care of the underlying details. This is a departure from your typical Linux firewall, which has traditionally required arcane implementation specific knowledge.</p></blockquote>

<p><img src="http://i.imgur.com/C5uYhl.jpg" alt="" title="Hosted by imgur.com" /></p>

<p>We hooked up the USB modem to Jai Keerthi's laptop and installed firestarter on it. Although not the ideal setup, it works. As Paul Graham puts it, India forces entrepreneurs to be <a href="http://www.paulgraham.com/relres.html">relentlessly resourceful</a>.</p>
]]>
      </description>
    </item>
    <item>
      <title>Scheduling jobs with quartz-jruby</title>
      <link>http://www.artha42.com#/blog/scheduling_jobs_with_quartz_jruby</link>
      <pubDate>Fri, 03 Dec 2010 05:21:23 -0000</pubDate>
      <guid>http://www.artha42.com/blog/scheduling_jobs_with_quartz_jruby</guid>
      <description>
        <![CDATA[<p>There would be times when you would want to run repetitive tasks once in every few minutes. Like for example, downloading twitter feeds or importing data from one system to another system and so on. Most operating systems have an utility called Cron to do that. Sometimes you may have so many of these small processes that it may not be feasible to fork off a new process every time. It might be easier to have it running as a server. Java has an excellent library called <a href="http://www.quartz-scheduler.org/">Quartz</a>.</p>

<blockquote><p>Quartz is a full-featured, open source job scheduling service that can be integrated with, or used along side virtually any Java EE or Java SE application - from the smallest stand-alone application to the largest e-commerce system. Quartz can be used to create simple or complex schedules for executing tens, hundreds, or even tens-of-thousands of jobs; jobs whose tasks are defined as standard Java components that may executed virtually anything you may program them to do. The Quartz Scheduler includes many enterprise-class features, such as JTA transactions and clustering.</p></blockquote>

<h4>The Java Way</h4>

<p>To have a simple cron job, you need to write something like this. First you write the job. For example, here we have a cron job that gets fired every 20 seconds.</p>

<pre class="brush: java">
//SimpleJob.java
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class SimpleJob implements Job {

    private static Logger _log = LoggerFactory.getLogger(SimpleJob.class);
    public SimpleJob() {
    }

    public void execute(JobExecutionContext context)
        throws JobExecutionException {
        // do something complicated
        _log.info("hello job");
    }

}
</pre>


<p>And then you have to schedule it.</p>

<pre class="brush: java">
//CronScheduler.java
import java.util.Date;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;

public class CronScheduler{

    public void run() throws Exception {
        Logger log = LoggerFactory.getLogger(CronScheduler.class);
        
        // First we must get a reference to a scheduler
        SchedulerFactory sf = new StdSchedulerFactory();
        Scheduler sched = sf.getScheduler();

        // jobs can be scheduled before sched.start() has been called

        JobDetail job = new JobDetail("job1", "group1", SimpleJob.class);
        CronTrigger trigger = new CronTrigger("trigger1", "group1", "job1",
                "group1", "0/20 * * * * ?");
        sched.addJob(job, true);
        Date ft = sched.scheduleJob(trigger);
        //start the scheduler
        sched.start();
    }

    public static void main(String[] args) throws Exception {
        CronScheduler example = new CronScheduler();
        example.run();
    }

}
</pre>


<h4>The Ruby Way</h4>

<p>Although the above code works, this seems very .... umm... verbose. I have written a wrapper around Quartz that makes it a lot more palatable. Now presenting <a href="https://github.com/artha42/quartz-jruby">quartz-jruby</a>. You can install it like below.</p>

<pre class="brush: bash">
$ jruby -S gem install quartz-jruby
</pre>


<p>Now for some ruby code. This sets up the logger, manages jobs and schedules them with a friendly syntax.</p>

<pre class="brush: ruby">
# cron_scheduler.java
require 'java'
require 'rubygems'
gem 'quartz-jruby'
require 'quartz'

# configure log4j the way you want. I am lazy. :-)
org.apache.log4j.BasicConfigurator.configure

class CronScheduler
  
  # this adds all the scheduler stuff to your class
  include Quartz::Scheduler
  schedule(:hello_job, :every=>20.seconds) do 
    # any ruby code
    info "I fire every 20 seconds"
  end

end

# run the scheduler
CronScheduler.instance.run
</pre>


<h4>Feedback</h4>

<p><a href="http://twitter.com/ikai/status/10621876120322049" title="Hosted by imgur.com"><img src="http://i.imgur.com/ZqC3l.png" alt="" title="Hosted by imgur.com" /></a></p>

<p>If you would like to check the source, you can visit <a href="http://github.com/artha42/quartz-jruby" title="">quartz-jruby on github</a>. Your comments are welcome.</p>
]]>
      </description>
    </item>
    <item>
      <title>MongoMapper Plugin - Versionable</title>
      <link>http://www.artha42.com#/blog/mongomapper_plugin_versionable</link>
      <pubDate>Mon, 29 Nov 2010 11:10:41 -0000</pubDate>
      <guid>http://www.artha42.com/blog/mongomapper_plugin_versionable</guid>
      <description>
        <![CDATA[<p>There was a requirement at one of our projects here at <a href="http://www.artha42.com/">Artha42 Innovations Private Limited</a> that our Mongo Mapper documents have inherent versioning so as to be able to track the changes being made and also if required revert the changes to a previous state. Since we're using MongoMapper, I set out to create a simple plugin for the same to add the required functionality.</p>

<p>Initially, from the initial research I did, I found <a href="http://shapado.com/">Shapado</a> had an implementation of exactly what I wanted, however when I studied and understood their implementation in detail, I realized that it doesn't really work that well in most scenarios, especially the ones we were tackling in our project. Shapado's versionable allows you to define a set of keys for a document, whose changes would trigger creation of new versions. However in our project we needed to track changes at the complete document level, i.e. even for changes in the associations. Also the versions were embedded within the document itself, which implied that the document would grow in size very rapidly, even though our changes in each iteration could be minor, since each version would literally be a snapshot of the complete document itself.</p>

<p>So we made quite a few design changes in the same, externalized the versions, changed the way one would enable versioning since in our case we wouldn't be keeping track of changes only in specific keys. Even here, we found that there were certain bottlenecks in the case if there were a whole lot of versions, for which we introduced the :limit feature which allows us to define how many of versions would need to be loaded instead of loading all of them at once.</p>

<p>The result of it is this <a href="https://github.com/artha42/mm-versionable">mm-versionable</a>.</p>

<p>If you have any comments, suggestions, requests, queries or feedback, please do let us know in the comments section below.</p>
]]>
      </description>
    </item>
    <item>
      <title>Software and broken windows</title>
      <link>http://www.artha42.com#/blog/software_and_broken_windows</link>
      <pubDate>Tue, 23 Nov 2010 07:15:08 -0000</pubDate>
      <guid>http://www.artha42.com/blog/software_and_broken_windows</guid>
      <description>
        <![CDATA[<p>I came across this <a href="http://groups.google.com/group/chennaiocc/browse_thread/thread/c8e8698aaf311add/2c6210156f69aeae">mail</a> on the ChennaiOCC mailing list. It is from a web design company which needs "PHP Resources". I am sick and tired of people looking for "N X resource(s)", where N is the number of people and the X is the technology. Whenever I see a job ad like this, I get furious. I consider it a personal insult to the hackers working in the industry.</p>

<p>This thinking comes from a flawed assumption that programmers are somehow interchangeable cogs in a system. I used to use the term hackers in my previous job descriptions but I got too many people looking for roles specific to computer security and most of them did not know anything. So I now use the term programmer/developer which gets the point across. However, when I say I am looking for programmers/developers, it means that I am looking for a kick ass hacker who is equally comfortable in coding, testing his code and deploying it on to a server and debug issues with it. You see, a hacker does not write a piece of code to a specification. A hacker creates. It is that creativity that we are looking for which the current education system and companies like these are fighting so hard to kill.</p>

<p>So coming back to this company, I visited their website just for the heck of it. It landed me on a blank page, with their name and a "click here" link. I followed the link and I got assaulted with a flash based image gallery. I then went to their <a href="http://www.metistech.co.in/index.php?option=com_content&amp;view=article&amp;id=146&amp;Itemid=76">SEO page</a>. Please notice the URL. Would you trust this company with your SEO? I then looked at careers page and found no job openings. <surprise/> The thing is that they have opened the module to add a job opening for any visitor. I do not mean to pick on Mentis. Their website might be a work in progress but it just sounds bad when client or prospective employees visit your site and a lot of this stuff is broken.</p>

<p>We should not forget that <a href="http://en.wikipedia.org/wiki/Broken_windows_theory">Broken Windows Theory</a> applies to software as well. Bad code begets bad development practices and there is no escaping the death spiral.</p>
]]>
      </description>
    </item>
    <item>
      <title>How I learned to stop worrying and love EC2/Linode</title>
      <link>http://www.artha42.com#/blog/how_i_learned_to_stop_worrying_and_love_iaas</link>
      <pubDate>Mon, 22 Nov 2010 17:44:32 -0000</pubDate>
      <guid>http://www.artha42.com/blog/how_i_learned_to_stop_worrying_and_love_iaas</guid>
      <description>
        <![CDATA[<p>I have tried very hard to like platform as a service solutions like Windows Azure or Heroku or GAE. Now most of these systems are a marvel of engineering and may be right for many scenarios. But when you are building applications and want to keep your technology choices open and adaptable to change, these might become your biggest worries. PaaS vendors often sell the story of simplicity and the ease of use of their platform. But in reality, when you are going to scale, you will have to deal with the complexities of scaling one way or the other.</p>

<p>This is what I think would work better. Invest in people instead of investing on the platform. It seems to be Microsoft and Windows Server all over again. The promise of windows was that it made it so easy that any MCSE would be able to manage the network. But for really large networks we needed good admins nevertheless who could have done a better job with Linux and their tools of choice. Also PaaS solutions tie you down to the platform. Even if the language is open, you will have to architect your application to the design choices of the platform. This makes the app very hard to port if you ever want to move out of the platform. Imagine rewriting an app that has been written for GAE to a normal Django/Postgres stack. It is insanely difficult. You are better off rewriting it from scratch.</p>

<p>Also if you would like to integrate your app with other software that is not supported on the stack, you are pretty much left to the mercy of the company building the platform. We have just learned not to build on closed source platforms and to embrace open source. But PaaS vendors want to take their closed source platforms all over again and they preach lean startups and cloud computing to mask their closed source monopolistic intentions.</p>

<p>We have an interoperable choice with IaaS vendors. EC2, Rackspace Cloud, Linode, Slicehost and others provide an interoperable solution. Although their service interfaces are different, they offer a similar service. You can reasonably port your app across EC2 to Rackspace to Linode and so on. They have varying degrees of flexibility, but at the core, they do not shackle you on to their platforms and ask for your first born. I have been seduced by GAE and have even thought seriously about using that as a platform to build applications. Then I realized that I was just being lazy. Scaling apps is hard but building apps that need that scale is much harder. If you can get to that stage, I reckon that you can figure out the scaling problem as well.</p>

<p>I am an open source developer and never will I give away my freedom to companies even as benevolent as Google. I wonder who else is using Microsoft Azure apart from Bing?</p>
]]>
      </description>
    </item>
    <item>
      <title>Vampires, Werewolves and Hybrids</title>
      <link>http://www.artha42.com#/blog/vampires_werewolves_and_hybrids</link>
      <pubDate>Sat, 28 Aug 2010 06:29:07 -0000</pubDate>
      <guid>http://www.artha42.com/blog/vampires_werewolves_and_hybrids</guid>
      <description>
        <![CDATA[<p>There has been some <a href="http://blog.serverfault.com/post/893001713/should-developers-have-access-to-production">recent</a> <a href="http://www.codinghorror.com/blog/2010/08/vampires-programmers-versus-werewolves-sysadmins.html">controversy</a> regarding vampires and werewolves.</p>

<blockquote><p>Programmers are like vampires. They're frequently up all night, paler than death itself, and generally afraid of being exposed to daylight. Oh yes, and they tend think of themselves (or at least their code) as immortal.</p>

<p>System Administrators are like werewolves. They may look outwardly ordinary, but are incredibly strong, mostly invulnerable to stuff that would kill regular people -- and prone to strange transformations during a moon "outage".</p></blockquote>

<p>However, in my opinion,  there are a new breed of engineers. The so called hybrids.</p>

<p><img src="http://imgur.com/oVUNIl.jpg" alt="" title="Hosted by imgur.com" /></p>

<p><a href="http://en.wikipedia.org/wiki/Hybrid_(Underworld)#Hybrids">Hybrid</a> is a pseudo-race from underworld evolution. They are both a vampire and a lycan (werewolf) at a cellular level and are stronger but have more craving for blood (to get things done). They switch roles between a programmer and a system administrator at a drop of a hat. They are comfortable configuring firewall rules, setting up log rotation, backing up and restoring databases and can comfortably snap into developing the next big feature once this is done. If you do not believe me, look at <a href="http://github.com">github.com</a> or read about <a href="http://sheddingbikes.com/">Zed Shaw</a>. It is difficult to evolve into a hybrid. It takes years of practice but it is possible.</p>

<p>Once you are a hybrid, many people tend to call you the architect. And I do not mean the bald, old ones dabbling with abstract diagrams but people with common sense who can come up with pragmatic solutions that most people are blindsided to. Also the newer frameworks like Rails/Sinatra and databases like MongoDB  provide ample space for either of the species to evolve into a hybrid. I always encourage the vampires at Artha42 to become werewolves and the vice versa. If you think you are a good vampire or a werewolf and would like to join our clan, you can <a href="/join_us">apply</a> here.</p>
]]>
      </description>
    </item>
    <item>
      <title>Vim Productivity</title>
      <link>http://www.artha42.com#/blog/vim_productivity</link>
      <pubDate>Wed, 25 Aug 2010 16:13:16 -0000</pubDate>
      <guid>http://www.artha42.com/blog/vim_productivity</guid>
      <description>
        <![CDATA[<p>We use everything from Emacs to Eclipse to Vim. But I feel that Vim is the best editor ever. But that is just me. I am constantly on the lookout for tools and plugins that make me more productive with Vim.</p>

<p>If you use Rails then you must use the awesome <a href="http://www.vim.org/scripts/script.php?script_id=1567">rails.vim</a>. Git integration is just unbelievable with the <a href="http://www.vim.org/scripts/script.php?script_id=2975">fugitive</a> plugin. A good programmer should be able to navigate the code regardless of the project he is working on. I found that when diving into new projects, the <a href="http://www.vim.org/scripts/script.php?script_id=1658">NERDTree</a> plugin is really helpful. Grep is the swiss army knife of search but <a href="http://betterthangrep.com/">ack</a> is even better. Ack helps you search your sub-tree very quickly. Ack has even got an awesome <a href="http://www.vim.org/scripts/script.php?script_id=2572">vim plugin</a> to make it easy to search your code while you are working on it. If you know the path to the files but want to quickly open it, there is no better candidate than the <a href="https://wincent.com/products/command-t/">Command-T</a> plugin. It emulates the Goto File feature of Textmate. You need to have vim compiled with ruby to use this plugin.</p>

<p>By the way, I use <a href="http://slinky.imukuppi.org/zenburn/">zenburn</a> as my color theme. This website's grey theme is an inspiration from zen burn. What are the plugins that you use with vim? Do let me know.</p>
]]>
      </description>
    </item>
    <item>
      <title>Why the name "Artha42"?</title>
      <link>http://www.artha42.com#/blog/why_the_name_artha42</link>
      <pubDate>Fri, 18 Jun 2010 19:25:06 -0000</pubDate>
      <guid>http://www.artha42.com/blog/why_the_name_artha42</guid>
      <description>
        <![CDATA[<p>When we were naming the baby, oops, the company, we came with a list of names. Vagmi came up with some coinage of English words and I googled for Sanskrit words (and their translation) and came up with a list.</p>

<p>Well why Sanskrit? It is said that the difference between other languages (both Indian and foreign) and Sanskrit is that this language was not created by humans, but discovered. It is said in the yogic lore that each sound has a corresponding form and each form has a corresponding sound. So the sound corresponding to the form had been discovered by the yogis and made as a collection to form the Sanskrit language. Usually Sanskrit names also carry a meaning. Infact Samskritam(which is now anglicised to Sanskrit) means "well-created". Vagmi means one who speaks well.  Shanthi means peace. Well this sound actually may not correspond to the form ;-)</p>

<p>Vagmi finally agreed to go with a Sanskrit word and liked Artha and Tejas the most. Artha means "meaning". It also means "wealth", but that is not the context we chose it. However we couldn't get artha.com or tejas.com. So we were looking at alternatives. The domain names weren't available for most of them. So then I "deepthought" of some combinations and landed up with 42. (Those who don't understand deepthought or 42, refer "<a href="http://en.wikipedia.org/wiki/Phrases_from_The_Hitchhiker%27s_Guide_to_the_Galaxy#Answer_to_the_Ultimate_Question_of_Life_the_Universe_and_Everything_.2842.29">Hitchhiker's guide to the galaxy</a>"). So I came up with 42artha. Vagmi initially liked it, but then said it sounded like 37signals. I didn't know such name combinations really existed and was a little disappointed that there was a pioneer. So I proposed finally "Artha42". Artha42 Private Limited was rejected by the Registrar of Companies saying a company Artha already exists and merely numbers can't differentiate company names. So we gave them options of Artha42 Software, Artha42 Consulting, Artha42 Technology Solutions and finally they approved Artha42 Technology Solutions.</p>

<p>Each time I call a candidate or a vendor, in one breath, I say "I am Shanthi from Artha42 Technology Solutions". The response is "Shanthi from what?" The name has to be spelt and repeated thrice and they "correctly" note it down as "Artha 42 Technologies". I cannot avoid a grin when Vagmi introduces himself "I am Vagmi from Artha42 Technology Solutions" as I could picture the response would be "Hmm?" I am sure they can't figure out both names and they would not have even heard the tail end (Technology Solutions). So, what can they ask? :-)</p>

<p>Ok, I thought I can atleast fix one in three(Vagmi, Artha42, Technology Solutions). Like most developers, I chose the easiest. ;-) I came up with choices like Artha42 Innovations, Artha42 Crafting, Artha42 Labs to express what we do or intend to. We are glad that ROC accepted the first choice itself. Hence, we now are "Artha42 Innovations Private limited" since June 3rd, 2010.</p>
]]>
      </description>
    </item>
    <item>
      <title>Artha42 is reborn</title>
      <link>http://www.artha42.com#/blog/artha42_is_reborn</link>
      <pubDate>Fri, 18 Jun 2010 18:20:15 -0000</pubDate>
      <guid>http://www.artha42.com/blog/artha42_is_reborn</guid>
      <description>
        <![CDATA[<p>We have overhauled ourselves. We have discarded Artha42 Technology Solutions and are reborn as Artha42 Innovations rooted by Vagmi Mudumbai and Shanthi. With this, we also want to say boldly, all we want to do is build awesome software with simplistic approach using the right tools.  So we revamped our website with a new look.</p>

<p>Initially we started with the thought process on who are the users of the website. Obviously it is you who have the inclination to either take our services or join our family to provide them. Even if not, to provoke such an interest is the aim. With this intention, we started with the simple home page sporting links for Hire us, why hire us, Join us, why join us.</p>

<p>As per the new trend, we started with a dark themed website (dark grey, close to black). But then I had the feeling, "why do the same colour as others?". Grey was never my favourite. So we then tried white background. The first colours we used in combination with white looked awesome on a MAC, but horrible on a DELL Vostro. So we went ahead playing around with different colours. We started with orange (the chosen colour for Artha42) made it darker to subdue the brightness.  Ultimately it landed in chocolate brown. It looked unconventional and it became my favourite of the three. However Vagmi had a differing opinion. So we then tried the theme (stylesheet) switcher and based on a poll within closed set of voters, we decided brown to be default. As we anyway want to give room to differences in taste, the theme switcher is left as intended design.</p>

<p>The website is powered by Apache with Phusion Passenger and is built on Sinatra and Mongo with aggressive caching. We had initially started off with a completely static site using jekyll but eventually chose the excellent Sinatra microframework to build this site.</p>

<p>We then felt the need to express our victories, challenges, the rough roads we travelled and the roller coaster rides. To cut it short, we always want to blog, but did not find the right platform or the opportune time until now. Your comments are most welcome.  Do let us know what you think of our posts.</p>
]]>
      </description>
    </item>
  </channel>
</rss>
