Friday, January 28, 2011

Rails Architecture/Performance issues

Typical Rails architecture:
Nginx/Apache --> HAProxy --> Mongrel cluster

HAProxy is a http proxy that forwards requests from web server to an available Mongrel instance. queue requests up if all Mongrels are busy.

HTTP options: Passenger, Mongrel, Thin, Unicorn

Monitoring tools:
New Relic, top/iostat, God, Monit

Great blog on reasons for memory bloat in Rails:
http://www.engineyard.com/blog/2009/thats-not-a-memory-leak-its-bloat/
Github architecture: https://github.com/blog/530-how-we-made-github-fast
Rack:Bug: https://github.com/brynary/rack-bug/

Thursday, January 27, 2011

Mailing list managers (Listserv, Mailman) vs MTAs (Sendmail, Postfix)

Some terminology:
Mail Transfer Agent: MTA implements both the client (sending) and server (receiving) portions of the Simple Mail Transfer Protocol.
An MTA receives a message from another MTA, MSA or MUA. If the recipient mailbox is not hosted locally, then it routes it to another MTA. The Domain Name System (DNS) associates a mail server to a domain with mail exchanger (MX) resource records containing the domain name of a host providing MTA services.
MX: A mail exchanger record (MX record) is a type of resource record in the Domain Name System that specifies a mail server responsible for accepting email messages on behalf of a recipient's domain and a preference value used to prioritize mail delivery.
MUA: Mail user agent - this is an email client like GMail, Outlook etc. MUA uses POP3 (Post Office Protocol) or IMAP (Internet Message Access Protocol) to retrieve messages from an MTA.
MSA: Mail submission agent - sits between the MUA and MTA. Functionally same as MTA.

Sendmail/Postfix use information from the Domain Name System (DNS) to figure out which IP addresses go with which mailboxes.
1. Setup a domain name -e.g. companyA.com
2. Configure name servers for your domain (primary and secondary)
3. Configure MX records for your domain.
4. After the name servers are setup, register your domain using one of the registries.
5. Configure sendmail to listen for mail/route outgoing mail.
6. Mailing lists are configured by setting up aliases. In Postfix, edit the /etc/aliases file. It has the format:
alias: address1,address2. After updating this file, you are usually required to run commands to update the internal db file used by Postfix/Sendmail.

Mailing list managers like Mailman integrate with an MTA like Sendmail or Postfix, so that when new lists are created, or lists are removed, Postfix's alias database will be automatically updated.

Wednesday, January 26, 2011

Nokogiri & Jruby - native library not found!

Nokogiri -

Function 'xmlSchemaValidateFile' not found in [libexslt.dylib] (FFI::NotFoundError)
file:/Users/Admin/Work/googleapps/ruby/teamsapp/WEB-INF/lib/jruby-stdlib-1.5.6.jar!/META-INF/jruby.home/lib/ruby/site_ruby/shared/ffi/ffi.rb:112:in `create_invoker'

Cucumber,Capybara & Webrat, Selenium

Cucumber is a BDD tool that provides a language - Given/When/Then syntax for describing "behavior"
The steps in this behavior description maybe implemented as pure Ruby code or in a variety of other DSL's like Capybara or Webrat for acceptance testing web applications. Both Capybara and Webrat are similar - the former has a cleaner/more flexible architecture. They both provide DSL e.g.
visit('/projects')
fill_in('First Name', :with => 'John')
click_link('id-of-link')
Here are reasons why Capybara is better than Webrat from the author himself)
"Webrat is fantastic, and it has done wonders for testing Ruby webapps,
it provides a very slick and elegant DSL for interacting with webapps,
but it also has a couple of problems:
* It is strongly tied to Rails and the Integration Testing built into
Rails
* It doesn't have (comprehensive) support for testing JavaScript
* It is difficult to extend
* There is no driver agnostic test suite to make sure that Selenium
mode for example behaves the same as Rails mode.
* It cannot run different drivers in the same process, so it can't run
one feature under selenium and another in simulation.
All of these pain points led me to tinker around with building a
driver agnostic solution with the following goals:
* Make it dead simple to switch between different drivers
* Support multiple drivers out of the Box
* Provide a comprehensive test suite which can run against any driver
* Make it work with any Rack based framework
* Make it as compatible as possible with the Webrat API
The result of this work is called Capybara and can be found at GitHub
here: http://github.com/jnicklas/capybara
It uses rack-test to simulate a browser instead of Rails' Integration
Testing, which means that interacting with the controller is out (it's
bad practice anyway, imho. It's an integration test after all). I also
intentionally didn't make have_tag and have_text work, since those by
virtue of how they work will never be useful with Selenium, Culerity
or any other browser simulator. Instead there's have_content,
have_xpath and have_css. Other than that it's very similar to Webrat. "

Monday, January 17, 2011

Ruby/Rails on Ubuntu

For apt-get to install packages on a new Ubuntu install, first run update: apt-get update

sudo apt-get install curl
sudo apt-get install build-essential (installs libraries required to compile C programs/make)
sudo apt-get install zlib1g-dev libreadline5-dev libssl-dev libxml2-dev
sudo apt-get install ruby1.8
ruby1.8 -ropenssl -rzlib -rreadline -e "puts:Hello"
sudo apt-get install rubygems
sudo gem install rails (rails script is found in /var/lib/gems/1.8)
export PATH=/var/lib/gems/1.8/bin/:$PATH

sudo apt-get install ruby1.8-dev << this package is essential for building ruby native extensions.

MYSQL:
sudo apt-get mysql-server mysql-client
libmysql-ruby: This is an API module that allows to access MySQL database from programs in Ruby.
libmysqlclient-dev: package includes development libraries and header files for MySQL
sudo apt-get libmysql-ruby libmysqlclient-dev
gem install mysql

APACHE & PASSENGER:
sudo apt-get apache2
sudo gem install passenger
sudo passenger-install-apache2-module