mindsifter

sifting through the thoughts in my mind
Copyright Michael D'Auria © 2010

Projects

My first open source accepted patch! 05 June 2008

Call me a loser, but I just hopped on the open source committing bandwagon. I recently started using merb 0.9.x for a new project and there were a few things that I were off, so I used the awesome GitHub, cloned the projects i needed to tweak and went to town. My first official commit was a small tweak to merb-core that correctly took in a custom configured session_id_key and you can see it here :)

800x600 is for wussies 20 February 2008

Yea that's right i said it, for wussies! I checked my Google Analytics and noticed that the smallest resolution used to view the site was 1024x768, therefore i took a few minutes to expand the layout by 130 pixels. This enables the longer code segments to fit inside of the images that contain them; I also find it much easier to read. That is all for now...

Gotcha When Upgrading Ruby 11 January 2008

I decided to upgrade my ruby version from 1.8.5 p114 to 1.8.6 p111 today and came across an error when I tried to run "merb":http://merbivore.com. Whenever I would try and run the merb command witihin my project it would get as far as "Compiling routes...", then just stop output. I decided to install rdebug to inspect:


 $ sudo gem install rdebug

Then I could check out where it was failing:


 merb_project_dir:$ rdebug -d -x ./script/merb

I use ./script/merb here because I had frozen merb to the project so I poke around a bit. After a solid minute of output, there was something that stood out:


 bad version, 1.8.6 != 1.8.5

This might not trigger anything to you at first glance, but some of the "rubygems":http://rubygems.org you depend on are actually compiled to the version of ruby that you have. Offhand I can think of mysql, datamapper, sqlite3, do_mysql, do_sqlite3, and of course the reason merb was failing, RubyInline and ParseTree. Once I reinstalled these two i was golden:


 $ sudo gem install RubyInline ParseTree

Shoot me a comment if you ran into this as well because I couldn't find anythnig on Google.

Generate Local Ruby Core RDocs 08 January 2008

I like being able to go to "ruby-doc.org/core":http://ruby-doc.org/core if i want to look up some Ruby 1.8 docs, but what if you don't have the internet? I always compile Ruby myself so I can have the latest security fixes and after I compile I always issue a:


 $ rdoc --template=jamis --op 

That gives me a local copy of the RDocs so can serve it up via a webserver and look up whatever I desire without the internet, pretty neat. Oh that --template=jamis bit comes from Jamis' awesome RDoc template that he made in 2005 for use with the Rails RDocs. For its use, take a look at the "original article":http://weblog.jamisbuck.org/2005/4/8/rdoc-template from the man himself.

Generate Local Rails RDocs 08 January 2008

As a follow up to my quick article on creating RDocs for the Ruby Core, you can very easily do the same for Rails and even have generate with Jamis' template. It's as easy as:


 $ rake rails:freeze:gems
 $ template='jamis' rake doc:rails
 

Run rake doc:rerails if you have already created the Rails RDocs before. You already froze Rails to your app for easier deployment right?

No More PC Speaker Beep Please 03 January 2008

I hate that damn loud beeping noise coming from any machine, even more so from a VM that decided to crash my computer, but that's another article.

Here's how you remove the noises from an Ubuntu machine:


 $ sudo rmmod pcspkr
 $ sudo vi /etc/modprobe.d/blacklist

Now simply add the following line to the end:


 blacklist pcspkr

Done and done.

Add hg revision number to page titles for QA 17 December 2007

Recently after my article about Bazaar I switched to Mercurial. Bazaar is just WAY too slow for remote repositories, which is what I use so my partner can pull changes, whereas Mercural is very fast. Now let me show you how to go from the SVN code to Mercurial this time.


Here is the snippet of Brian's original code:


  def revision
    @revision ||= if svn_info_from_working_copy 
      svn_info_from_working_copy["Revision"].value
    else
      last_revision_in_log
    end
  rescue
    @revision = "UNKNOWN"
  end
  
  def svn_info_from_working_copy
    @svn_info ||= YAML.parse(`svn info #{RAILS_ROOT}`)
  end
  
  def last_revision_in_log
    File.readlines(RAILS_ROOT + "/../../revisions.log").last.split[3]
  end
  

And here is my modified:


  def revision
    @revision ||= if hg_log_from_working_copy 
      hg_log_from_working_copy['changeset'].value.split(':').first
    else
      last_revision
    end
  rescue
    @revision = "UNKNOWN"
  end
  
  def bzr_revno_from_working_copy
    @hg_log ||= YAML.parse(`hg log #{RAILS_ROOT}`)
  end
  
  def last_revision
    File.read(RAILS_ROOT + "/REVISION").strip
  end
  

Bazaar made this easier, but the remote repository performance is just too subpar for my use. So there you have it, enjoy.

Multiple Rails Apps, 1 Domain 05 December 2007

For development I have been using a VM these days with a web server hosting multiple apps under the same VHost via different directories. What i need to use what this under my 'config/development.rb' files:


ActionController::AbstractRequest.relative_root_url = '/app'

Hope this saves some people time...

Add bzr revision number to page titles for QA 27 November 2007

I came across a "little article":http://www.brynary.com/2007/8/30/add-svn-revision-number-to-page-titles by Brian Helmkamp on his blog. I thought this was a good idea for SVN, so why not update it for my use with Bazaar? Also, his fallback is to resort to the Capisrano revisions.log file, which is no longer in use with the release of 2.0, so I use the REVISION file.


Here is the snippet of Brian's original code:


  def revision
    @revision ||= if svn_info_from_working_copy 
      svn_info_from_working_copy["Revision"].value
    else
      last_revision_in_log
    end
  rescue
    @revision = "UNKNOWN"
  end
  
  def svn_info_from_working_copy
    @svn_info ||= YAML.parse(`svn info #{RAILS_ROOT}`)
  end
  
  def last_revision_in_log
    File.readlines(RAILS_ROOT + "/../../revisions.log").last.split[3]
  end
  

And here is my modified:


  def revision
    @revision ||= if bzr_revno_from_working_copy 
      bzr_revno_from_working_copy
    else
      last_revision
    end
  rescue
    @revision = "UNKNOWN"
  end
  
  def bzr_revno_from_working_copy
    @bzr_revno ||= `bzr revno #{RAILS_ROOT}`
  end
  
  def last_revision
    File.read(RAILS_ROOT + "/REVISION").strip
  end
  

Of course let's not forget his suggestion of using this in the title of the page:


  r<%= @revision  %> | ...Normal page title...
  

Thanks Brian!

Slice Host, how I love thee 25 May 2007

Anyway, I was speaking with Brendan about my current host at the time, Hostik (whom I do not recommend at all), and how expensive it was. I had a dedicated server to run about a handful of sites that saw very little traffic. I have "cacti":http://cacti.org running at all times and noticed that the server usage was pretty minimal and I was basically throwing money out. This is when Brendan brought up his host, "Slice Host":http://slicehost.com. I love these guys, quick to answer all of your questions in an email or a forum and their prices are just, well great. I currently use 2 of their slices, one 256MB and a 512MB and it costs me half of what 1 dedicated server cost me. Now that's amazing! It gets better though, I have a pretty drastic performance boost from moving to the slice, which makes me very happy.

"Slice Host":http://slicehost.com, thank you for being a great host with great service and great prices.

I suck at blogging 25 May 2007

I told myself that once i started this thing that I would actually use it, looks like I was totally wrong.

I found it difficult at times to come up with something smart to say and toss it on here. I want to use this blog as a tool to increase my writing abilities as well as give some people interesting topics to read over. So I guess what I am saying is that I am going to write about everything whenever it dawns on me. The way I see it is, no one is reading this thing anyway...

Clearing of Session Data 26 January 2007

You can do it this way:


  class SessionCleaner
    def self.remove_stale_sessions
      CGI::Session::ActiveRecordStore::Session.
        destroy_all( ['updated_on < ?', 20.minutes.ago] ) 
    end
  end

Then just call via cron:


  */10 * * * * ruby /full/path/to/script/runner 
     -e production "SessionCleaner.remove_stale_sessions"

Above was found via: http://www.realityforge.org/articles/2006/03/01/removing-stale-rails-sessions

Or you can do it all one 1 line:


  */5 * * * *  ruby /full/path/to/script/runner -e 
    production 'CGI::Session::ActiveRecordStore::Session.destroy_all
    ( ["updated_at < ?",20.minutes.ago ] )' 2>&1

Both will clear out sessions that are 20 minutes old every 10 minutes. Both will work, but they have to load up Rails every time it runs. I came up with the following solution to avoid loading up Rails thus alleviating some server load:


  require 'rubygems'
  require 'mysql'
  
  db = Mysql.new("localhost", "db_user", "db_user_pw", "db")
  query = db.prepare("DELETE FROM `sessions` WHERE 
    updated_at < NOW() - INTERVAL 20 MINUTE")
  query.execute
  puts "Removed #{query.affected_rows} Sessions\n"
  db.close
  

Please note that I assume that you have a MySQL database ,have the mysql gem installed and that you are storing your sessions in a table named 'sessions'.


Enjoy!


Stay tuned for a more flexible version that will read in your config/database.yml for your db settings so you don't have to put them in the script itself.

Who am I? 26 December 2006

Welcome to my small, never heard of, yet fun blog! The content I aim to blog about is anything related to software engineering, web design, travel and any weird websites or links I come across. Hopefully you will find everything to be entertaining :) I very much like to research new technologies and methods regarding computer engineering, including new emerging programming languages, web server software as well as proper coding etiquette. I jumped onto the Ruby and Rails bandwagon back in June and since then I have helped create trebleNation. Hopefully you find the content that I post amusing so that you come back again for another visit.