<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Silentale Devteam</title>
	
	<link>http://geeks.silentale.com</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Tue, 09 Mar 2010 16:03:19 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/SilentaleDevteam" /><feedburner:info uri="silentaledevteam" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>How to check streamed files in a Rails’ functional test</title>
		<link>http://feedproxy.google.com/~r/SilentaleDevteam/~3/QWzjRst-wmk/</link>
		<comments>http://geeks.silentale.com/2010/03/06/how-to-check-streamed-files-in-a-rails-functional-test/#comments</comments>
		<pubDate>Fri, 05 Mar 2010 23:54:23 +0000</pubDate>
		<dc:creator>florent</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[streaming]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://geeks.silentale.com/?p=129</guid>
		<description><![CDATA[For administration purposes, I had to rework an uploaded CSV file, and stream it back to the user. I started wondering how I could test this uncommon response body type.

I knew about:


fixture_file_upload


which allows the simulation of a file upload, so I looked at the Rails source, in ActionController&#8217;s TestProcess class, where the method lies. 

There [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;"><a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fgeeks.silentale.com%2F2010%2F03%2F06%2Fhow-to-check-streamed-files-in-a-rails-functional-test%2F" onclick="pageTracker._trackPageview('/outgoing/api.tweetmeme.com/share?url=http_3A_2F_2Fgeeks.silentale.com_2F2010_2F03_2F06_2Fhow-to-check-streamed-files-in-a-rails-functional-test_2F&amp;referer=');"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fgeeks.silentale.com%2F2010%2F03%2F06%2Fhow-to-check-streamed-files-in-a-rails-functional-test%2F" height="61" width="51" /></a></div><p>For administration purposes, I had to rework an uploaded CSV file, and stream it back to the user. I started wondering how I could test this uncommon response body type.</p>

<p>I knew about:</p>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">fixture_file_upload</pre></div></div>


<p>which allows the simulation of a file upload, so I looked at the Rails source, in ActionController&#8217;s TestProcess class, where the method lies. </p>

<p>There I found the:</p>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">binary_content</pre></div></div>


<p>method, which does the job by returning the entire streamed content in a string!</p>

<h2>How it works</h2>

<p>First, let&#8217;s take a look at the <code>send_file</code> sources:</p>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">...
<span style="color:#9900CC;">render</span> <span style="color:#ff3333; font-weight:bold;">:status</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:status</span><span style="color:#006600; font-weight:bold;">&#93;</span>, <span style="color:#ff3333; font-weight:bold;">:text</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#CC0066; font-weight:bold;">Proc</span>.<span style="color:#9900CC;">new</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>response, output<span style="color:#006600; font-weight:bold;">|</span>
  logger.<span style="color:#9900CC;">info</span> <span style="color:#996600;">&quot;Streaming file #{path}&quot;</span> <span style="color:#9966CC; font-weight:bold;">unless</span> logger.<span style="color:#0000FF; font-weight:bold;">nil</span>?
  len = options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:buffer_size</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">||</span> <span style="color:#006666;">4096</span>
  <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#40;</span>path, <span style="color:#996600;">'rb'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>file<span style="color:#006600; font-weight:bold;">|</span>
    <span style="color:#9966CC; font-weight:bold;">while</span> buf = file.<span style="color:#9900CC;">read</span><span style="color:#006600; font-weight:bold;">&#40;</span>len<span style="color:#006600; font-weight:bold;">&#41;</span>
      output.<span style="color:#9900CC;">write</span><span style="color:#006600; font-weight:bold;">&#40;</span>buf<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#006600; font-weight:bold;">&#125;</span>
...</pre></div></div>


<p>We see that the response is returned as a Proc. All that <code>binary_content</code> has to do is to:</p>

<ol>
    <li>create an IO object,</li>
    <li>call the proc and pass it the IO object</li>
    <li>read the IO and return the content as a string</li>
</ol>

<p>Here is the <code>binary_content</code> method:</p>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> binary_content
  <span style="color:#CC0066; font-weight:bold;">raise</span> <span style="color:#996600;">&quot;Response body is not a Proc: #{body.inspect}&quot;</span> <span style="color:#9966CC; font-weight:bold;">unless</span> body.<span style="color:#9900CC;">kind_of</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0066; font-weight:bold;">Proc</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'stringio'</span>
&nbsp;
  sio = <span style="color:#CC00FF; font-weight:bold;">StringIO</span>.<span style="color:#9900CC;">new</span>
  body.<span style="color:#9900CC;">call</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">self</span>, sio<span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
  sio.<span style="color:#9900CC;">rewind</span>
  sio.<span style="color:#9900CC;">read</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>


<p>StringIO&#8217;s for the win!</p>
]]></content:encoded>
			<wfw:commentRss>http://geeks.silentale.com/2010/03/06/how-to-check-streamed-files-in-a-rails-functional-test/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://geeks.silentale.com/2010/03/06/how-to-check-streamed-files-in-a-rails-functional-test/</feedburner:origLink></item>
		<item>
		<title>Cacti templates for beanstalkd</title>
		<link>http://feedproxy.google.com/~r/SilentaleDevteam/~3/qjWPgn_uiWg/</link>
		<comments>http://geeks.silentale.com/2010/01/04/cacti-templates-for-beanstalkd/#comments</comments>
		<pubDate>Mon, 04 Jan 2010 14:45:02 +0000</pubDate>
		<dc:creator>erwan</dc:creator>
				<category><![CDATA[sysadmin]]></category>
		<category><![CDATA[beanstalkd]]></category>
		<category><![CDATA[cacti]]></category>
		<category><![CDATA[github]]></category>
		<category><![CDATA[templates]]></category>

		<guid isPermaLink="false">http://geeks.silentale.com/?p=114</guid>
		<description><![CDATA[Following on the previous post about monitoring beanstalkd with cacti, I have just uploaded the corresponding templates and script to github.

I hope you&#8217;ll find them useful !
]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;"><a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fgeeks.silentale.com%2F2010%2F01%2F04%2Fcacti-templates-for-beanstalkd%2F" onclick="pageTracker._trackPageview('/outgoing/api.tweetmeme.com/share?url=http_3A_2F_2Fgeeks.silentale.com_2F2010_2F01_2F04_2Fcacti-templates-for-beanstalkd_2F&amp;referer=');"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fgeeks.silentale.com%2F2010%2F01%2F04%2Fcacti-templates-for-beanstalkd%2F" height="61" width="51" /></a></div><p>Following on the previous post about <a href="http://geeks.silentale.com/2009/12/08/monitoring-beanstalkd-with-cacti-creating-custom-cacti-templates/">monitoring beanstalkd with cacti</a>, I have just uploaded the corresponding templates and script to <a href="http://github.com/earzur/cacti-beanstalkd-templates" onclick="pageTracker._trackPageview('/outgoing/github.com/earzur/cacti-beanstalkd-templates?referer=');">github</a>.</p>

<p>I hope you&#8217;ll find them useful !</p>
]]></content:encoded>
			<wfw:commentRss>http://geeks.silentale.com/2010/01/04/cacti-templates-for-beanstalkd/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://geeks.silentale.com/2010/01/04/cacti-templates-for-beanstalkd/</feedburner:origLink></item>
		<item>
		<title>Multiple Elastic IP addresses on AWS instances</title>
		<link>http://feedproxy.google.com/~r/SilentaleDevteam/~3/jlHDu03s1i8/</link>
		<comments>http://geeks.silentale.com/2009/12/08/multiple-elastic-ip-addresses-on-aws-instances/#comments</comments>
		<pubDate>Tue, 08 Dec 2009 15:50:51 +0000</pubDate>
		<dc:creator>erwan</dc:creator>
				<category><![CDATA[AWS]]></category>
		<category><![CDATA[sysadmin]]></category>

		<guid isPermaLink="false">http://geeks.silentale.com/?p=91</guid>
		<description><![CDATA[Recently, we reworked our web frontend infrastructure to separate our corporate website from the Silentale web application. The main goal was to make sure we don&#8217;t have to involve our operations resources (a.k.a &#8230; me  ) too much when we update our corporate web site &#8230;

This involved setting up a few new virtual hosts [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;"><a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fgeeks.silentale.com%2F2009%2F12%2F08%2Fmultiple-elastic-ip-addresses-on-aws-instances%2F" onclick="pageTracker._trackPageview('/outgoing/api.tweetmeme.com/share?url=http_3A_2F_2Fgeeks.silentale.com_2F2009_2F12_2F08_2Fmultiple-elastic-ip-addresses-on-aws-instances_2F&amp;referer=');"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fgeeks.silentale.com%2F2009%2F12%2F08%2Fmultiple-elastic-ip-addresses-on-aws-instances%2F" height="61" width="51" /></a></div><p>Recently, we reworked our web frontend infrastructure to separate our corporate website from the Silentale web application. The main goal was to make sure we don&#8217;t have to involve our operations resources (a.k.a &#8230; me <img src='http://geeks.silentale.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> ) too much when we update our corporate web site &#8230;</p>

<p>This involved setting up a few new virtual hosts and names:
<ul>
    <li><a href="http://silentale.com" onclick="pageTracker._trackPageview('/outgoing/silentale.com?referer=');">http://silentale.com</a> and <a href="https://silentale.com" onclick="pageTracker._trackPageview('/outgoing/silentale.com?referer=');">https://silentale.com</a> for static web pages</li>
    <li><a href="https://my.silentale.com" onclick="pageTracker._trackPageview('/outgoing/my.silentale.com?referer=');">https://my.silentale.com</a> for the application (that&#8217;s where you are logging in now, dear beta users !)</li>
    <li>https://api.silentale.com for the firefox extension and all the future nifty stuff that we are preparing for our users&#8217; christmas</li>
</ul>
My original idea was to use virtual servers, and have the web servers running from the same host. I had prepared all the necessary valid certificates and DNS configuration, the configuration for nginx was ready and working, but I had forgot <strong>one little problem with SSL and virtual hosts</strong>: as the server tries to retrieve which virtual server should respond to the request by reading the value of the <code>Host:</code> request header, the SSL connection <strong>must have already been established</strong>, therefore a proper certificate must have been presented to the client, and nginx cannot decide which one to send (as it can&#8217;t read the Host: header) !</p>

<p>Ok, i&#8217;m dumb, and I shouldn&#8217;t forget you can&#8217;t host serveral SSL servers with different names from the same IP address unless you buy one of these expensive wildcard certificates (buying certificates is as close to extortion as to &#8216;renew&#8217; a domain name or trying to use one of these &#8220;limited unlimited 3G+&#8221; offers, but that will be the subject of another post &#8230;)
<h2>Listening to different ports ?</h2>
I could have decided to make the servers listen to different ports, but
<ul>
    <li>our platform is getting more and more complex, and we&#8217;d rather avoid doing that sort of things</li>
    <li>we don&#8217;t want to confuse our users (and especially the new beta users who have to get a very good impression of our service) with TCP ports that could be filtered on their end &#8230;</li>
</ul>
<h2>Off to elastic IP ?</h2>
Amazon web services provide a very useful tool for web servers which they call <a href="http://aws.amazon.com/ec2/" target="_blank" onclick="pageTracker._trackPageview('/outgoing/aws.amazon.com/ec2/?referer=');">Elastic IP</a>. You reserve a static, public IP address in their subnet, and you can seamlessly attach it to any instance. You can switch it from instance to instance, and in a matter of seconds, the new server will be reachable on this address, without you having to update DNS record or incur any downtime to your users. Very cool stuff when upgrading or migrating !</p>

<p>So my problem could have been solved if i could assign multiple elastic IPs to the same EC2 instance, and make each of my SSL servers listen on each one of them &#8230; unfortunately, <strong>you just can&#8217;t do that <img src='http://geeks.silentale.com/wp-includes/images/smilies/icon_sad.gif' alt=':-(' class='wp-smiley' /> </strong>. An EC2 instance gets an address on a RFC1918 subnet, and that&#8217;s the only one you can see from it. In order to be able to connect to it, amazon creates some sort of reverse NAT between the outside world and your instance (i&#8217;d like to know how they handle that in their border routers. It must be really interesting and challenging to operate <img src='http://geeks.silentale.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> )
<h2>Several instances &#8230;</h2>
So, in our case, the only proper solution is to start 3 instances when we could have used only one . This is really fustrating and unfortunate &#8230; If you came across the same problem on AWS and have found a better solution, i&#8217;d be very happy to hear about it.</p>

<p>If someone from Amazon reads me, this feature isn&#8217;t critical, but it prevents any kind of mutualization on your cloud. I don&#8217;t think it is part of your core business, but it may represent a significant share of it in the future (low cost hosters, etc &#8230;)</p>
]]></content:encoded>
			<wfw:commentRss>http://geeks.silentale.com/2009/12/08/multiple-elastic-ip-addresses-on-aws-instances/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://geeks.silentale.com/2009/12/08/multiple-elastic-ip-addresses-on-aws-instances/</feedburner:origLink></item>
		<item>
		<title>Monitoring beanstalkd with cacti &amp; creating custom cacti templates</title>
		<link>http://feedproxy.google.com/~r/SilentaleDevteam/~3/CJ9yo2NbMnA/</link>
		<comments>http://geeks.silentale.com/2009/12/08/monitoring-beanstalkd-with-cacti-creating-custom-cacti-templates/#comments</comments>
		<pubDate>Tue, 08 Dec 2009 15:43:30 +0000</pubDate>
		<dc:creator>erwan</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[sysadmin]]></category>

		<guid isPermaLink="false">http://geeks.silentale.com/?p=68</guid>
		<description><![CDATA[[update: the script and templates discussed below are available on github: http://github.com/earzur/cacti-beanstalkd-templates]

At Silentale, we use working queues to orchestrate the different services in our platform.

Passing messages between different processes is a pretty common pattern for distributing a process across many computers and our platform makes an extensive use of Beanstalkd, which is a fast, lightweight [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;"><a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fgeeks.silentale.com%2F2009%2F12%2F08%2Fmonitoring-beanstalkd-with-cacti-creating-custom-cacti-templates%2F" onclick="pageTracker._trackPageview('/outgoing/api.tweetmeme.com/share?url=http_3A_2F_2Fgeeks.silentale.com_2F2009_2F12_2F08_2Fmonitoring-beanstalkd-with-cacti-creating-custom-cacti-templates_2F&amp;referer=');"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fgeeks.silentale.com%2F2009%2F12%2F08%2Fmonitoring-beanstalkd-with-cacti-creating-custom-cacti-templates%2F" height="61" width="51" /></a></div><p>[update: the script and templates discussed below are available on github: <a title="http://github.com/earzur/cacti-beanstalkd-templates" href="http://github.com/earzur/cacti-beanstalkd-templates" onclick="pageTracker._trackPageview('/outgoing/github.com/earzur/cacti-beanstalkd-templates?referer=');">http://github.com/earzur/cacti-beanstalkd-templates</a>]</p>

<p>At Silentale, we use <a href="http://nubyonrails.com/articles/about-this-blog-beanstalk-messaging-queue" onclick="pageTracker._trackPageview('/outgoing/nubyonrails.com/articles/about-this-blog-beanstalk-messaging-queue?referer=');">working queues</a> to orchestrate the different services in our platform.</p>

<p>Passing messages between different processes is a pretty common pattern for distributing a process across many computers and our platform makes an extensive use of <a href="http://kr.github.com/beanstalkd/" target="_blank" onclick="pageTracker._trackPageview('/outgoing/kr.github.com/beanstalkd/?referer=');">Beanstalkd</a>, which is a fast, lightweight and robust server that does just that: let us implement working queues.</p>

<p>Monitoring the number of jobs available (ready, in the beanstalkd idiom), the number of workers consuming jobs, and the number of jobs that failed (buried) allows operators to get a pretty good idea of how the platform is performing and add or remove capacity when needed. This is particularly useful in our AWS-based environment, when you can add and remove capacity in no time.</p>

<p>For instance, when sending a new batch of invitations to beta users to test our product, we get significant spikes in our working queues, and may need to temporarily add new servers, and then shut them down when we have finished processing the queues.</p>

<p>The <a href="http://github.com/kr/beanstalkd/blob/v1.3/doc/protocol.txt?raw=true" onclick="pageTracker._trackPageview('/outgoing/github.com/kr/beanstalkd/blob/v1.3/doc/protocol.txt?raw=true&amp;referer=');">protocol</a> used by beanstalkd is pretty simple and provide ways to easily collect usage statistics about the server itself and every individual queue that you might have defined.</p>

<p>Another tool we use a lot at Silentale is <a href="http://www.cacti.net/" onclick="pageTracker._trackPageview('/outgoing/www.cacti.net/?referer=');">Cacti</a>. By default, Cacti allows to graph data collected through snmp-enabled servers pretty easily, so you can graph metrics about your servers such as network bandwidth, load average, and memory usage. But you are not limited to snmp only, you can also graph data collected through scripts. This is a bit more involved, as the <a href="http://code.google.com/p/mysql-cacti-templates/" onclick="pageTracker._trackPageview('/outgoing/code.google.com/p/mysql-cacti-templates/?referer=');">Mysql Cacti templates</a> for cacti demonstrates.</p>

<p>After spending a while looking for an equivalent project that would support beanstalkd and work in our environment, I went on writing my own cacti templates for monitoring our beanstalk queues &#8230;
<h2>available statistics</h2>
The beanstalk protocol provides a lot of different figures about what&#8217;s going on in the server both globally and by individual queue. For my little project, I chose to concentrate on the individual queues, as monitoring the number of new jobs in each queue is critical to our operations.</p>

<p>The beanstalk-client gem&#8217;s &#8216;tube_stats&#8217; method returns a hash containing a lot of different values, out of which the ones that we want to graph.
<ul>
    <li>current-jobs-buried</li>
    <li>current-jobs-delayed</li>
    <li>current-jobs-ready</li>
    <li>current-jobs-reserved</li>
    <li>current-jobs-urgent</li>
    <li> current-using</li>
    <li>current-waiting</li>
    <li>current-watching</li>
    <li>total-jobs</li>
</ul>
Collecting those involves writing a very short and simple ruby script:</p>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#!/usr/bin/env ruby</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'rubygems'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'beanstalk-client'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'trollop'</span>
&nbsp;
script_name = <span style="color:#0000FF; font-weight:bold;">__FILE__</span>.<span style="color:#CC0066; font-weight:bold;">split</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'/'</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">last</span>
opts = Trollop::options <span style="color:#9966CC; font-weight:bold;">do</span>
version <span style="color:#996600;">&quot;#{script_name} © 2009 Silentale S.A.&quot;</span>
banner <span style="color:#006600; font-weight:bold;">&amp;</span>lt;<span style="color:#006600; font-weight:bold;">&amp;</span>lt;<span style="color:#006600; font-weight:bold;">-</span>EOS
display statistics about a queue on a beanstalkd server
EOS
opt <span style="color:#ff3333; font-weight:bold;">:server</span>, <span style="color:#996600;">'beanstalk server address'</span>, <span style="color:#ff3333; font-weight:bold;">:type</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#ff3333; font-weight:bold;">:string</span>
opt <span style="color:#ff3333; font-weight:bold;">:port</span>, <span style="color:#996600;">'beanstalk server port (default: 11300)'</span>, <span style="color:#ff3333; font-weight:bold;">:type</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#ff3333; font-weight:bold;">:integer</span>
opt <span style="color:#ff3333; font-weight:bold;">:queue</span>, <span style="color:#996600;">'name of the beanstalk queue'</span>, <span style="color:#ff3333; font-weight:bold;">:type</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#ff3333; font-weight:bold;">:string</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
Trollop::die <span style="color:#ff3333; font-weight:bold;">:server</span>, <span style="color:#996600;">'is mandatory'</span> <span style="color:#9966CC; font-weight:bold;">unless</span> opts<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:server</span><span style="color:#006600; font-weight:bold;">&#93;</span>
Trollop::die <span style="color:#ff3333; font-weight:bold;">:queue</span>, <span style="color:#996600;">'is mandatory'</span> <span style="color:#9966CC; font-weight:bold;">unless</span> opts<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:queue</span><span style="color:#006600; font-weight:bold;">&#93;</span>
opts<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:port</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#006666;">11300</span> <span style="color:#9966CC; font-weight:bold;">unless</span> opts<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:port</span><span style="color:#006600; font-weight:bold;">&#93;</span>
&nbsp;
B = <span style="color:#6666ff; font-weight:bold;">Beanstalk::Connection</span>.<span style="color:#9900CC;">new</span> <span style="color:#996600;">&quot;#{opts[:server]}:#{opts[:port]}&quot;</span>
&nbsp;
ts=B.<span style="color:#9900CC;">stats_tube</span> opts<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:queue</span><span style="color:#006600; font-weight:bold;">&#93;</span>
&nbsp;
ts.<span style="color:#9900CC;">delete</span> <span style="color:#996600;">'name'</span>
&nbsp;
result = <span style="color:#996600;">''</span>
ts.<span style="color:#9900CC;">keys</span>.<span style="color:#9900CC;">sort</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>k<span style="color:#006600; font-weight:bold;">|</span>
result <span style="color:#006600; font-weight:bold;">&amp;</span>lt;<span style="color:#006600; font-weight:bold;">&amp;</span>lt; <span style="color:#996600;">&quot;#{k}:#{ts[k]} &quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">puts</span> result</pre></div></div>


<p>We just connect to the server, issue the <code>tube_stats</code> method, and dump the resulting hash, after having sorted the keys by names.
<h2>cacti configuration</h2>
The output of the script is pretty straightforward, a single line with <code>name: value</code> pairs, separated by spaces. Cacti can directly parse the output using what is called a data input method. Data input methods link external scripts with data fields that can be used in data templates, then those data templates can be used in graph templates.</p>

<p><em>One unfortunate problem with cacti is that everything has to be done using the GUI, involving a lot of clicks and typing. I didn&#8217;t find any way to circumvent this problem, using scripts or any automated tool. </em></p>

<p><em>If you managed to read this far and know of any such tool (or a pointer to cacti&#8217;s data model) so we can script such tasks, I will be more than happy to receive comments <img src='http://geeks.silentale.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> 
</em>
<h3>data input method</h3>
Using cacti&#8217;s console, defining a new data input method is straightforward. You just have to create the output fields one by one and map them to the output of the script.</p>

<div id="attachment_75" class="wp-caption alignleft" style="width: 310px"><a href="http://geeks.silentale.com/wp-content/uploads/2009/12/Image-12.PNG"><img class="size-medium wp-image-75" title="Image 12" src="http://geeks.silentale.com/wp-content/uploads/2009/12/Image-12-300x100.PNG" alt="Data input method screen" width="300" height="100" /></a><p class="wp-caption-text">Data input method screen</p></div>

<p>As you can see in this screenshot, you need to specify the full path to the data-collection script, specify input parameters between brackets (&lt; and &gt;) (here we define the <code>&lt;server&gt;</code>,<code>&lt;port&gt;</code> and <code>&lt;queue&gt;</code>), and then specify the resulting data field one by one.</p>

<p>Now we need to define a data template, that cacti will use to update the RRD archives and generate the graphs.
<h3>data template</h3>
The data template maps the output fields of the data input method to &#8220;data source items&#8221;, which are fields in the RRD archive that will store the collected data.</p>

<p>Those fields have to be carefully named, I have used a convention which I found in the MySQL cacti templates. A few letters in capitals, the same for every fields in the template, followed by the name of the parameter. It is extremely useful to do that, because cacti orders the field names alphabetically in the field selection dropdown list, and there can be <strong>many of them</strong> ! so remember to carefuly name your input fields in this screen !</p>

<p>In this screen you also tell cacti how to collect the input parameters for your script. This way, cacti will allow you to specify <strong>default</strong> values when you will create graphs, by associating hosts to the graph templates. Checking the box at the left of the input field will make cacti ask for the associated input value in the forms where you create graphs for hosts. Not checking the box will let cacti decide for you. Here, i&#8217;ve left the <code>host</code> field blank with the box unchecked to make sure cacti will automatically fill it with host name to which you are attaching a graph using the template.</p>

<div id="attachment_85" class="wp-caption alignnone" style="width: 310px"><a href="http://geeks.silentale.com/wp-content/uploads/2009/12/Image-131.PNG"><img class="size-medium wp-image-85" title="Image 13" src="http://geeks.silentale.com/wp-content/uploads/2009/12/Image-131-300x39.PNG" alt="Bottom of the &quot;data template&quot; screen" width="300" height="39" /></a><p class="wp-caption-text">Bottom of the &quot;data template&quot; screen</p></div>
<h3>graph template</h3>
Graph templates are pretty simple to define. You specify the list of fields that you want to display in the graphs, the graph style (colors, line size, etc &#8230;)</p>

<p>In our platform, we want new users, those that just registered, to see messages in their timeline as soon as possible, so we are using a nifty beanstalkd feature which allow us to mark some jobs &#8220;urgent&#8221; and make sure that those jobs will be handled before the others. Another very nice feature is the ability to delay the jobs for later handling. For instance, if our backend fails at parsing some kind of messages because of encoding issues or whatever. Instead of giving up, we decided to just delay those jobs for a while, get an alert, try to fix the problem in our backend and let the messages flow back in again. That&#8217;s the kind of things that beanstalkd and working queues can help you do. I just can&#8217;t figure the complexity of doing such things in a monolithic, old-school process !</p>

<p>Let&#8217;s define a graph that will display
<ul>
    <li> the number of jobs ready to be processed</li>
    <li> the number of jobs with the &#8220;urgent&#8221; flag</li>
    <li> the number of jobs which have been delayed</li>
    <li>the number of processes currently actively picking up jobs from the queue</li>
</ul>
Create a new graph template &#8230; Most of the default values will work ok, and again, take care of the naming convention here, your graphs will be much easier to pick up when linking them with hosts &#8230;</p>

<p><div id="attachment_86" class="wp-caption alignnone" style="width: 310px"><a href="http://geeks.silentale.com/wp-content/uploads/2009/12/Image-15.PNG"><img class="size-medium wp-image-86" title="Add graph template" src="http://geeks.silentale.com/wp-content/uploads/2009/12/Image-15-300x112.PNG" alt="Add graph template screenshot ..." width="300" height="112" /></a><p class="wp-caption-text">Add graph template screenshot ...</p></div>

<p>Then you&#8217;ll be presented with a screen where you will have to painfully enter the names of the fields you wish to graph, and you will quickly realise why I insisted on the naming convention for the data fields above &#8230; <img src='http://geeks.silentale.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>

<p><div id="attachment_87" class="wp-caption alignnone" style="width: 310px"><a href="http://geeks.silentale.com/wp-content/uploads/2009/12/Image-16.PNG"><img class="size-medium wp-image-87" title="Graph template " src="http://geeks.silentale.com/wp-content/uploads/2009/12/Image-16-300x138.PNG" alt="Graph template with data fields defined" width="300" height="138" /></a><p class="wp-caption-text">Graph template with data fields defined</p></div>
<h2>Almost there !!!!</h2>
And now, you just have to associate this graph template to the host running your beanstalk server. After a while, you&#8217;ll realize that cacti started collecting data and will display nice graphs showing the state of your beanstalk queues, thanks to rrdtool !</p>

<p>Not wanting to disclose too much about our operations, i have edited this screenshot to remove figures and hostnames, but you get the idea. The most interesting one is on the upper right corner, with the crawler queue, where we have a constant pool (green stripe on the lower part) of workers pulling crawling jobs (IMAP,POP,twitter, etc &#8230;) and posting every new message they&#8217;ve found in another queue whose job is to index them &#8230; One of my goals as an operator is to keep all these queues as flat as possible. The whitespace indicates that I need to add some crawling capacity, because some jobs are not performed on-time. They are ultimately, but not within the window of time that we allowed ourselves to operate &#8230; we are also currently working on some changes that should help reduce the spikes in that particular graph.</p>

<p><div id="attachment_93" class="wp-caption alignnone" style="width: 310px"><a href="http://geeks.silentale.com/wp-content/uploads/2009/12/Image-20.PNG"><img class="size-medium wp-image-93" title="Graph sample" src="http://geeks.silentale.com/wp-content/uploads/2009/12/Image-20-300x144.PNG" alt="resulting graphs in cacti's thumbnail view mode" width="300" height="144" /></a><p class="wp-caption-text">resulting graphs in cacti&#39;s thumbnail view mode</p></div>
<h2>What&#8217;s next ?</h2>
As you can see, defining custom templates for cacti is a really involved task. You spend a lot of time fiddling with settings and try/error/revert cycles. That&#8217;s why I plan to package my templates, make sure they are generic enough to work outside of our own environment and publish the result or contribute it to the MySQL cacti templates project. Stay tuned &#8230;</p>

<p>Having such monitoring in place is invaluable and has been absolutely necessary for us to keep our service online !</p>
]]></content:encoded>
			<wfw:commentRss>http://geeks.silentale.com/2009/12/08/monitoring-beanstalkd-with-cacti-creating-custom-cacti-templates/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://geeks.silentale.com/2009/12/08/monitoring-beanstalkd-with-cacti-creating-custom-cacti-templates/</feedburner:origLink></item>
		<item>
		<title>Lazy loading with Javascript and Firefox</title>
		<link>http://feedproxy.google.com/~r/SilentaleDevteam/~3/Nlq6BlAAwbU/</link>
		<comments>http://geeks.silentale.com/2009/11/03/lazy-loading-with-javascript-and-firefox/#comments</comments>
		<pubDate>Tue, 03 Nov 2009 15:59:18 +0000</pubDate>
		<dc:creator>Catalin</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[firefox]]></category>
		<category><![CDATA[lazy]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://geeks.silentale.com/?p=29</guid>
		<description><![CDATA[Lazy loading has been a hot topic highly discussed over the internet recently. The amount of bandwidth for the average user has grown tenfold during the last few years, but the web itself has evolved a lot, and so have the contents of the temp folder.

This has created a need to dynamically load a piece [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;"><a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fgeeks.silentale.com%2F2009%2F11%2F03%2Flazy-loading-with-javascript-and-firefox%2F" onclick="pageTracker._trackPageview('/outgoing/api.tweetmeme.com/share?url=http_3A_2F_2Fgeeks.silentale.com_2F2009_2F11_2F03_2Flazy-loading-with-javascript-and-firefox_2F&amp;referer=');"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fgeeks.silentale.com%2F2009%2F11%2F03%2Flazy-loading-with-javascript-and-firefox%2F" height="61" width="51" /></a></div><p>Lazy loading has been a hot topic <a href="http://www.insideria.com/2009/10/advantages-of-lazy-loading.html" onclick="pageTracker._trackPageview('/outgoing/www.insideria.com/2009/10/advantages-of-lazy-loading.html?referer=');">highly</a> <a href="http://forum.springsource.org/showthread.php?t=71622" onclick="pageTracker._trackPageview('/outgoing/forum.springsource.org/showthread.php?t=71622&amp;referer=');">discussed</a> over the internet recently. The amount of bandwidth for the average user has grown tenfold during the last few years, but the web itself has evolved a lot, and so have the contents of the temp folder.</p>

<p>This has created a need to dynamically load a piece of script (to make the host machine download it), instead of just forcing the user download everything from the very first load.</p>

<p><span id="more-29"></span></p>

<p>Javascript is a great language and its power has yet to be fully exploited by developers, but from some points of view it sometimes lacks basic capabilities, one of which is lazy loading.</p>

<p>While browsing the internet, I’ve discovered a lot of great articles on the matter, with examples and everything, but the solutions provided don’t apply at all times, like for example when developing Firefox extensions.</p>

<p>The main approach that seems to be popular among developers is inserting an extra “script tag” inside the head element of the page at some point in the code, by first extracting the head via a:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span>“head”<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span></pre></div></div>


<p>statement, creating a new text node via</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">document.<span style="color: #660066;">createTextNode</span></pre></div></div>


<p>(which contains the exact “&lt;script&gt;&lt;/script&gt;” declaration) and appending it via</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">document.<span style="color: #660066;">appendChild</span></pre></div></div>


<p>I won’t go into more detail on this solution, a <a href="http://www.google.com/search?q=lazy+loading+javascript&#038;ie=utf-8&#038;oe=utf-8&#038;aq=t" onclick="pageTracker._trackPageview('/outgoing/www.google.com/search?q=lazy+loading+javascript_038_ie=utf-8_038_oe=utf-8_038_aq=t&amp;referer=');">couple</a> of <a href="http://www.google.com/search?q=lazy+loading+web+pages&#038;ie=utf-8&#038;oe=utf-8&#038;aq=t" onclick="pageTracker._trackPageview('/outgoing/www.google.com/search?q=lazy+loading+web+pages_038_ie=utf-8_038_oe=utf-8_038_aq=t&amp;referer=');">searches</a> on the matter will sort anything you might have not understood so far.</p>

<p>While elegant, the approach doesn’t cut it in some cases.  For example, while developing the <a href="https://silentale.com/applications" onclick="pageTracker._trackPageview('/outgoing/silentale.com/applications?referer=');">Silentale Firefox Extension</a>, I was struggling to load jQuery “later” than usual, because loading it in the same place as all of my other scripts was breaking the Firefox UI. Well, the problem is that in an overlay file there’s no such thing as a “head” element to append children to, so I was stuck.</p>

<p>After almost a day of researching, I came across the following solution which works seamlessly everywhere:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> scriptLoader <span style="color: #339933;">=</span> Components.<span style="color: #660066;">classes</span><span style="color: #009900;">&#91;</span>“<span style="color: #339933;">@</span>mozilla.<span style="color: #660066;">org</span><span style="color: #339933;">/</span>moz<span style="color: #339933;">/</span>jssubscript<span style="color: #339933;">-</span>loader<span style="color: #339933;">;</span><span style="color: #CC0000;">1</span>”<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">getService</span><span style="color: #009900;">&#40;</span>Components.<span style="color: #660066;">interfaces</span>.<span style="color: #660066;">mozIJSubScriptLoader</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
scriptLoader.<span style="color: #660066;">loadSubScript</span><span style="color: #009900;">&#40;</span>path<span style="color: #339933;">,</span> context<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>


<p>This code obtains a reference to the actual Firefox subscript loader component inside the “scriptLoader” variable and calls the loadSubScript method.</p>

<p>The loadSubScript method takes two parameters: the first one is the string path to the javascript source file to load, and the second one is the context of where to load the script (by default the current document object). Using the second parameter is great for loading code inside a particular scope, so don’t miss out on it.</p>

<p>These two lines of code would have solved a lot of problems for me if I had known of their existence earlier. Hopefully they will do the same for you.</p>

<p>Have fun coding <img src='http://geeks.silentale.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://geeks.silentale.com/2009/11/03/lazy-loading-with-javascript-and-firefox/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://geeks.silentale.com/2009/11/03/lazy-loading-with-javascript-and-firefox/</feedburner:origLink></item>
		<item>
		<title>Class level inheritable attributes (@@ vs @)</title>
		<link>http://feedproxy.google.com/~r/SilentaleDevteam/~3/L4nkTPerMe8/</link>
		<comments>http://geeks.silentale.com/2009/07/23/class-level-inheritable-attributes-vs/#comments</comments>
		<pubDate>Thu, 23 Jul 2009 09:08:13 +0000</pubDate>
		<dc:creator>Nicolas</dc:creator>
				<category><![CDATA[Noob]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[inheritance]]></category>
		<category><![CDATA[variable]]></category>

		<guid isPermaLink="false">http://www.12noobs.com/?p=24</guid>
		<description><![CDATA[First, here is some Ruby code:


class Message
  @content = &#34;Welcome Alexandre and Catalin!&#34;
  @@date = Time.at&#40;0&#41;
&#160;
  def self.content
    @content
  end
&#160;
  def self.date
    @@date
  end
end
&#160;
class Email &#62; Message
end
&#160;
puts Message.content # =&#62; &#34;Welcome Alexandre and Catalin!&#34;
puts Email.content # =&#62; nil
&#160;
puts Message.date # =&#62; &#34;Thu Jan [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;"><a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fgeeks.silentale.com%2F2009%2F07%2F23%2Fclass-level-inheritable-attributes-vs%2F" onclick="pageTracker._trackPageview('/outgoing/api.tweetmeme.com/share?url=http_3A_2F_2Fgeeks.silentale.com_2F2009_2F07_2F23_2Fclass-level-inheritable-attributes-vs_2F&amp;referer=');"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fgeeks.silentale.com%2F2009%2F07%2F23%2Fclass-level-inheritable-attributes-vs%2F" height="61" width="51" /></a></div><p>First, here is some Ruby code:</p>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Message
  <span style="color:#0066ff; font-weight:bold;">@content</span> = <span style="color:#996600;">&quot;Welcome Alexandre and Catalin!&quot;</span>
  @@date = <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">at</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">content</span>
    <span style="color:#0066ff; font-weight:bold;">@content</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">date</span>
    @@date
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> Email <span style="color:#006600; font-weight:bold;">&gt;</span> Message
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">puts</span> Message.<span style="color:#9900CC;">content</span> <span style="color:#008000; font-style:italic;"># =&gt; &quot;Welcome Alexandre and Catalin!&quot;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> Email.<span style="color:#9900CC;">content</span> <span style="color:#008000; font-style:italic;"># =&gt; nil</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">puts</span> Message.<span style="color:#9900CC;">date</span> <span style="color:#008000; font-style:italic;"># =&gt; &quot;Thu Jan 01 01:00:00 +0100 1970&quot;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> Email.<span style="color:#9900CC;">date</span> <span style="color:#008000; font-style:italic;"># =&gt; &quot;Thu Jan 01 01:00:00 +0100 1970&quot;</span></pre></div></div>


<p>At first, when you see &#8220;@my<em>variable&#8221; in a class method (prefixed by &#8220;self&#8221;), you tell yourself that, as you are at the class level, using &#8220;@my</em>variable&#8221; and &#8220;@@my_variable&#8221; has the same behaviour.</p>

<p>But you saw in the previous code that it&#8217;s not true. In fact, it has to be not true. Why? Because updating &#8220;Email.date&#8221; will also update &#8220;Message.date&#8221;.</p>

<p>The excellent post <a href="http://railstips.org/2006/11/18/class-and-instance-variables-in-ruby" onclick="pageTracker._trackPageview('/outgoing/railstips.org/2006/11/18/class-and-instance-variables-in-ruby?referer=');">Class and Instance Variables In Ruby</a> by <a href="http://twitter.com/jnunemaker" onclick="pageTracker._trackPageview('/outgoing/twitter.com/jnunemaker?referer=');">John Nunemaker</a> details all of this perfectly. He suggests a module <strong>ClassLevelInheritableAttributes</strong> to avoid inheritance side effects when using class variables.</p>

<p>With this module you can inherit from class variables, but setting your own value in your subclass won&#8217;t update the value set in the superclass. Cool.</p>
]]></content:encoded>
			<wfw:commentRss>http://geeks.silentale.com/2009/07/23/class-level-inheritable-attributes-vs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://geeks.silentale.com/2009/07/23/class-level-inheritable-attributes-vs/</feedburner:origLink></item>
		<item>
		<title>require 'profile'</title>
		<link>http://feedproxy.google.com/~r/SilentaleDevteam/~3/Ke2Dgp0QzWo/</link>
		<comments>http://geeks.silentale.com/2009/02/16/require-profile/#comments</comments>
		<pubDate>Mon, 16 Feb 2009 10:39:43 +0000</pubDate>
		<dc:creator>Nicolas</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[conflict]]></category>
		<category><![CDATA[profiler]]></category>

		<guid isPermaLink="false">http://www.12noobs.com/?p=21</guid>
		<description><![CDATA[The Profile concept is pretty common in web applications. To define a Profile class, a Ruby developer usually creates a profile.rb file in its lib directory.


# profile.rb
class Profile
end


To use this class in a executable script, we can add the lib path in the Ruby load path and then require all files without having to always [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;"><a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fgeeks.silentale.com%2F2009%2F02%2F16%2Frequire-profile%2F" onclick="pageTracker._trackPageview('/outgoing/api.tweetmeme.com/share?url=http_3A_2F_2Fgeeks.silentale.com_2F2009_2F02_2F16_2Frequire-profile_2F&amp;referer=');"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fgeeks.silentale.com%2F2009%2F02%2F16%2Frequire-profile%2F" height="61" width="51" /></a></div><p>The <em>Profile</em> concept is pretty common in web applications. To define a Profile class, a Ruby developer usually creates a <em>profile.rb</em> file in its <em>lib</em> directory.</p>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># profile.rb</span>
<span style="color:#9966CC; font-weight:bold;">class</span> Profile
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>


<p>To use this class in a executable script, we can add the <em>lib</em> path in the Ruby load path and then require all files without having to always specify the absolute or relative path of the file we want to load.</p>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#!/usr/bin/env ruby</span>
<span style="color:#008000; font-style:italic;"># my_app</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Add the 'lib' directory in the Ruby load path</span>
$: <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">expand_path</span> <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">dirname</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">__FILE__</span><span style="color:#006600; font-weight:bold;">&#41;</span>, <span style="color:#996600;">'..'</span>, <span style="color:#996600;">'lib'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Load the Profile class</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'profile'</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Check that the Profile class has been loaded</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">'The Profile class is not loaded'</span> <span style="color:#9966CC; font-weight:bold;">unless</span> <span style="color:#9966CC; font-weight:bold;">defined</span>? Profile</pre></div></div>


<p>We end up with the following directory structure:</p>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">my_app<span style="color:#006600; font-weight:bold;">/</span>
  bin<span style="color:#006600; font-weight:bold;">/</span>
    my_app
  lib<span style="color:#006600; font-weight:bold;">/</span>
    profile.<span style="color:#9900CC;">rb</span></pre></div></div>


<p>Execute the <em>my_app</em> script and you&#8217;ll see this:</p>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">The Profile <span style="color:#9966CC; font-weight:bold;">class</span> is <span style="color:#9966CC; font-weight:bold;">not</span> loaded
  <span style="color:#006600; font-weight:bold;">%</span>   cumulative   <span style="color:#0000FF; font-weight:bold;">self</span>              <span style="color:#0000FF; font-weight:bold;">self</span>     total
 time   seconds   seconds    calls  ms<span style="color:#006600; font-weight:bold;">/</span>call  ms<span style="color:#006600; font-weight:bold;">/</span>call  name
  <span style="color:#006666;">0.00</span>     <span style="color:#006666;">0.00</span>      <span style="color:#006666;">0.00</span>        <span style="color:#006666;">2</span>     <span style="color:#006666;">0.00</span>     <span style="color:#006666;">0.00</span>  <span style="color:#CC00FF; font-weight:bold;">IO</span><span style="color:#008000; font-style:italic;">#write</span>
  <span style="color:#006666;">0.00</span>     <span style="color:#006666;">0.00</span>      <span style="color:#006666;">0.00</span>        <span style="color:#006666;">1</span>     <span style="color:#006666;">0.00</span>     <span style="color:#006666;">0.00</span>  <span style="color:#CC00FF; font-weight:bold;">Kernel</span>.<span style="color:#CC0066; font-weight:bold;">puts</span>
  <span style="color:#006666;">0.00</span>     <span style="color:#006666;">0.01</span>      <span style="color:#006666;">0.00</span>        <span style="color:#006666;">1</span>     <span style="color:#006666;">0.00</span>    <span style="color:#006666;">10.00</span>  <span style="color:#008000; font-style:italic;">#toplevel</span></pre></div></div>


<p>Not only our shiny new Profile class is not loaded, but also some profiling stuff are written to <em>STDERR</em>.</p>

<p>The problem is that a <em>profile.rb</em> file is already defined in the Ruby distribution. If you use the Ruby distribution provided by Leopard, check this file: <em>/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/profile.rb</em>.</p>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'profiler'</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">END</span> <span style="color:#006600; font-weight:bold;">&#123;</span>
  Profiler__::print_profile<span style="color:#006600; font-weight:bold;">&#40;</span>STDERR<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#006600; font-weight:bold;">&#125;</span>
Profiler__::start_profile</pre></div></div>


<p>It starts the Ruby profiler and shows the result at the end of the program -pretty cool BTW-.</p>

<p>To solve this problem, you have to insert you library directories at the beginning of the Ruby load path. That&#8217;s what Ruby gems do. Or you can make sure that you always require files by their complete path.</p>

<p>Make a choice for your whole project, because the <em>require</em> statement will load files twice if you do not always give it the same paths. Example:</p>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># Load the Profile class</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'profile'</span>
<span style="color:#008000; font-style:italic;"># The 'profile.rb' file is read a second time.</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'/home/marcel/code/my_project/lib/profile'</span></pre></div></div>


<p>Loading the same class multiple times can be dangerous if you use <em>alias<em>method</em>chain</em>. It will create an infinite loop.</p>
]]></content:encoded>
			<wfw:commentRss>http://geeks.silentale.com/2009/02/16/require-profile/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://geeks.silentale.com/2009/02/16/require-profile/</feedburner:origLink></item>
		<item>
		<title>Be cool with DRb, it's far from "scalable"</title>
		<link>http://feedproxy.google.com/~r/SilentaleDevteam/~3/J2UDNVV5A_0/</link>
		<comments>http://geeks.silentale.com/2008/10/05/be-cool-with-drb-its-far-from-scalable/#comments</comments>
		<pubDate>Sun, 05 Oct 2008 10:08:10 +0000</pubDate>
		<dc:creator>Nicolas</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[DRb]]></category>
		<category><![CDATA[scalability]]></category>

		<guid isPermaLink="false">http://www.12noobs.com/?p=18</guid>
		<description><![CDATA[When you begin to learn DRb, you quickly land on the famous Chad Fowler page, entitled &#8220;Intro to DRb&#8221;.

The &#8220;Concurrency&#8221; chapter is particularly interesting when you want to make a local resource available in the wild, allowing one request at a time on your resource.

So, with DRb, a dash of method_missing and a pinch of [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;"><a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fgeeks.silentale.com%2F2008%2F10%2F05%2Fbe-cool-with-drb-its-far-from-scalable%2F" onclick="pageTracker._trackPageview('/outgoing/api.tweetmeme.com/share?url=http_3A_2F_2Fgeeks.silentale.com_2F2008_2F10_2F05_2Fbe-cool-with-drb-its-far-from-scalable_2F&amp;referer=');"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fgeeks.silentale.com%2F2008%2F10%2F05%2Fbe-cool-with-drb-its-far-from-scalable%2F" height="61" width="51" /></a></div><p>When you begin to learn DRb, you quickly land on the famous Chad Fowler page, entitled <a href="http://chadfowler.com/ruby/drb.html" onclick="pageTracker._trackPageview('/outgoing/chadfowler.com/ruby/drb.html?referer=');">&#8220;Intro to DRb&#8221;</a>.</p>

<p>The &#8220;Concurrency&#8221; chapter is particularly interesting when you want to <a href="http://www.12noobs.com/2008/05/01/local-resource-available-in-the-wild-thanks-to-drb/" onclick="pageTracker._trackPageview('/outgoing/www.12noobs.com/2008/05/01/local-resource-available-in-the-wild-thanks-to-drb/?referer=');">make a local resource available in the wild</a>, allowing one request at a time on your resource.</p>

<p>So, with DRb, a dash of method_missing and a pinch of mutex, you have a perfect recipe to remotely access and protect your resource&#8230; but: DRb is not what we use to call a high availability entry point.</p>

<p>Let&#8217;s code the <em>proof</em>. Here is the code of the DRb server, it simulates a long and expensive task:</p>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'drb'</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> Server
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@i</span> = <span style="color:#006666;">0</span>
    <span style="color:#0066ff; font-weight:bold;">@mutex</span> = <span style="color:#CC00FF; font-weight:bold;">Mutex</span>.<span style="color:#9900CC;">new</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> method_missing<span style="color:#006600; font-weight:bold;">&#40;</span>name, <span style="color:#006600; font-weight:bold;">*</span>args<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@mutex</span>.<span style="color:#9900CC;">synchronize</span> <span style="color:#9966CC; font-weight:bold;">do</span>
      <span style="color:#0066ff; font-weight:bold;">@i</span> <span style="color:#006600; font-weight:bold;">+</span>= <span style="color:#006666;">1</span>
      <span style="color:#CC0066; font-weight:bold;">p</span> <span style="color:#0066ff; font-weight:bold;">@i</span>
      <span style="color:#CC0066; font-weight:bold;">sleep</span> <span style="color:#006666;">1</span> <span style="color:#008000; font-style:italic;"># You CPU works very hard here..</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#0066ff; font-weight:bold;">@i</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
server = DRb.<span style="color:#9900CC;">start_service</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;druby://:34100&quot;</span>, Server.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#CC0066; font-weight:bold;">p</span> <span style="color:#996600;">&quot;listening&quot;</span>
server.<span style="color:#9900CC;">thread</span>.<span style="color:#9900CC;">join</span></pre></div></div>


<p>You can now start the server via the <em>ruby server.rb</em> command and begin to code the client. It creates 100 processes, each one calling the DRb server and writing the response on the standard output:</p>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'drb'</span>
&nbsp;
client = DRbObject.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">nil</span>, <span style="color:#996600;">&quot;druby://:34100&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
pids = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
&nbsp;
100.<span style="color:#9900CC;">times</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  pids <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#CC0066; font-weight:bold;">fork</span> <span style="color:#006600; font-weight:bold;">&#123;</span>
  <span style="color:#CC0066; font-weight:bold;">p</span> <span style="color:#996600;">&quot;#{Time.now}: #{client.call}&quot;</span>
<span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">p</span> <span style="color:#996600;">&quot;#{Time.now}: created the 100 processes&quot;</span>
&nbsp;
pids.<span style="color:#9900CC;">each</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>pid<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#CC00FF; font-weight:bold;">Process</span>.<span style="color:#9900CC;">waitpid</span><span style="color:#006600; font-weight:bold;">&#40;</span>pid<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">p</span> <span style="color:#996600;">&quot;#{Time.now}: done&quot;</span></pre></div></div>


<p>Launch the client and see what happen:</p>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#996600;">&quot;Sat Oct 05 11:54:17 +0200 2008: 2&quot;</span>
<span style="color:#996600;">&quot;Sat Oct 05 11:54:17 +0200 2008: 3&quot;</span>
<span style="color:#996600;">&quot;Sat Oct 05 11:54:20 +0200 2008: created the 100 processes&quot;</span>
<span style="color:#996600;">&quot;Sat Oct 05 11:54:17 +0200 2008: 4&quot;</span>
<span style="color:#996600;">&quot;Sat Oct 05 11:54:17 +0200 2008: 5&quot;</span>
<span style="color:#996600;">&quot;Sat Oct 05 11:54:17 +0200 2008: 6&quot;</span>
<span style="color:#996600;">&quot;Sat Oct 05 11:54:17 +0200 2008: 7&quot;</span>
<span style="color:#996600;">&quot;Sat Oct 05 11:54:17 +0200 2008: 8&quot;</span>
<span style="color:#996600;">&quot;Sat Oct 05 11:54:17 +0200 2008: 9&quot;</span>
<span style="color:#996600;">&quot;Sat Oct 05 11:54:18 +0200 2008: 10&quot;</span>
.......
<span style="color:#996600;">&quot;Sat Oct 05 11:54:18 +0200 2008: 59&quot;</span>
<span style="color:#996600;">&quot;Sat Oct 05 11:54:18 +0200 2008: 60&quot;</span>
<span style="color:#996600;">&quot;Sat Oct 05 11:54:19 +0200 2008: 61&quot;</span>
<span style="color:#996600;">&quot;Sat Oct 05 11:54:19 +0200 2008: 61&quot;</span>
<span style="color:#6666ff; font-weight:bold;">DRb::DRbConnError</span>: druby:<span style="color:#006600; font-weight:bold;">//</span>:<span style="color:#006666;">34100</span> <span style="color:#006600; font-weight:bold;">-</span> <span style="color:#008000; font-style:italic;">#</span>
&nbsp;
method <span style="color:#CC0066; font-weight:bold;">open</span>	<span style="color:#9966CC; font-weight:bold;">in</span> drb.<span style="color:#9900CC;">rb</span> at line <span style="color:#006666;">736</span>
method each	<span style="color:#9966CC; font-weight:bold;">in</span> drb.<span style="color:#9900CC;">rb</span> at line <span style="color:#006666;">729</span>
method <span style="color:#CC0066; font-weight:bold;">open</span>	<span style="color:#9966CC; font-weight:bold;">in</span> drb.<span style="color:#9900CC;">rb</span> at line <span style="color:#006666;">729</span>
method initialize	<span style="color:#9966CC; font-weight:bold;">in</span> drb.<span style="color:#9900CC;">rb</span> at line <span style="color:#006666;">1189</span>
method new	<span style="color:#9966CC; font-weight:bold;">in</span> drb.<span style="color:#9900CC;">rb</span> at line <span style="color:#006666;">1169</span>
method <span style="color:#CC0066; font-weight:bold;">open</span>	<span style="color:#9966CC; font-weight:bold;">in</span> drb.<span style="color:#9900CC;">rb</span> at line <span style="color:#006666;">1169</span>
method method_missing	<span style="color:#9966CC; font-weight:bold;">in</span> drb.<span style="color:#9900CC;">rb</span> at line <span style="color:#006666;">1085</span>
method with_friend	<span style="color:#9966CC; font-weight:bold;">in</span> drb.<span style="color:#9900CC;">rb</span> at line <span style="color:#006666;">1103</span>
method method_missing	<span style="color:#9966CC; font-weight:bold;">in</span> drb.<span style="color:#9900CC;">rb</span> at line <span style="color:#006666;">1084</span>
at top level	<span style="color:#9966CC; font-weight:bold;">in</span> client.<span style="color:#9900CC;">rb</span> at line <span style="color:#006666;">9</span>
method <span style="color:#CC0066; font-weight:bold;">fork</span>	<span style="color:#9966CC; font-weight:bold;">in</span> client.<span style="color:#9900CC;">rb</span> at line <span style="color:#006666;">8</span>
at top level	<span style="color:#9966CC; font-weight:bold;">in</span> client.<span style="color:#9900CC;">rb</span> at line <span style="color:#006666;">8</span>
method times	<span style="color:#9966CC; font-weight:bold;">in</span> client.<span style="color:#9900CC;">rb</span> at line <span style="color:#006666;">7</span>
at top level	<span style="color:#9966CC; font-weight:bold;">in</span> client.<span style="color:#9900CC;">rb</span> at line <span style="color:#006666;">7</span></pre></div></div>


<p>Almost the 100 processes are created before the DRb server returned its first <em>calculation</em>. After several tries on a PowerBook and an EC2 instance, the DRb server <strong>rejects any new client after about 65 simultaneous requests</strong>.</p>

<p>But, it&#8217;s important to mention that the DRb server did not crash at all. You simply have to wait that it handles the remaining requests.</p>

<p>DRb is pretty good, allowing the Ruby developers to code remote services in no time.</p>

<p>If you need high availability services, your next step could be REST servers, distributed/dispatched thanks to <a href="http://haproxy.1wt.eu/" onclick="pageTracker._trackPageview('/outgoing/haproxy.1wt.eu/?referer=');">HAProxy</a> or <a href="http://nginx.net/" onclick="pageTracker._trackPageview('/outgoing/nginx.net/?referer=');">nginx</a>. And of course, you should also take a look at <a href="http://erlang.org/" onclick="pageTracker._trackPageview('/outgoing/erlang.org/?referer=');">Erlang</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://geeks.silentale.com/2008/10/05/be-cool-with-drb-its-far-from-scalable/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://geeks.silentale.com/2008/10/05/be-cool-with-drb-its-far-from-scalable/</feedburner:origLink></item>
		<item>
		<title>Beware of the Proxy design pattern -read method_missing-</title>
		<link>http://feedproxy.google.com/~r/SilentaleDevteam/~3/ypWOo51IGs8/</link>
		<comments>http://geeks.silentale.com/2008/08/23/beware-proxy-design-pattern-method_missing/#comments</comments>
		<pubDate>Sat, 23 Aug 2008 01:11:38 +0000</pubDate>
		<dc:creator>Nicolas</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[caller]]></category>
		<category><![CDATA[method_missing]]></category>
		<category><![CDATA[proxy]]></category>

		<guid isPermaLink="false">http://www.12noobs.com/?p=20</guid>
		<description><![CDATA[You probably read about how easy it was to implement the Proxy design pattern in Ruby.

Thanks to the Ruby method_missing method, you can pass messages to underlying objects. See the previous article Local resource available in the wild, thanks to DRb for a fully described example.

But there&#8217;s one caveat, you have to be very careful [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;"><a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fgeeks.silentale.com%2F2008%2F08%2F23%2Fbeware-proxy-design-pattern-method_missing%2F" onclick="pageTracker._trackPageview('/outgoing/api.tweetmeme.com/share?url=http_3A_2F_2Fgeeks.silentale.com_2F2008_2F08_2F23_2Fbeware-proxy-design-pattern-method_missing_2F&amp;referer=');"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fgeeks.silentale.com%2F2008%2F08%2F23%2Fbeware-proxy-design-pattern-method_missing%2F" height="61" width="51" /></a></div><p>You probably <a href="http://www.amazon.com/gp/redirect.html?ie=UTF8&#038;location=http%3A%2F%2Fwww.amazon.com%2FDesign-Patterns-Ruby-Addison-Wesley-Professional%2Fdp%2F0321490452%3Fie%3DUTF8%26s%3Dbooks%26qid%3D1219452626%26sr%3D8-1&#038;tag=12noobs-20&#038;linkCode=ur2&#038;camp=1789&#038;creative=9325" onclick="pageTracker._trackPageview('/outgoing/www.amazon.com/gp/redirect.html?ie=UTF8_038_location=http_3A_2F_2Fwww.amazon.com_2FDesign-Patterns-Ruby-Addison-Wesley-Professional_2Fdp_2F0321490452_3Fie_3DUTF8_26s_3Dbooks_26qid_3D1219452626_26sr_3D8-1_038_tag=12noobs-20_038_linkCode=ur2_038_camp=1789_038_creative=9325&amp;referer=');">read</a><img src="http://www.assoc-amazon.com/e/ir?t=12noobs-20&amp;l=ur2&amp;o=1" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" /> about how easy it was to implement the Proxy design pattern in Ruby.</p>

<p>Thanks to the Ruby <em>method_missing</em> method, you can pass messages to underlying objects. See the previous article <a href="/2008/05/01/local-resource-available-in-the-wild-thanks-to-drb/">Local resource available in the wild, thanks to DRb</a> for a fully described example.</p>

<p>But there&#8217;s one caveat, you have to be very careful when implementing your <em>method_missing</em> method.</p>

<p>Take this code for example:</p>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> method_missing<span style="color:#006600; font-weight:bold;">&#40;</span>name, <span style="color:#006600; font-weight:bold;">*</span>args, <span style="color:#006600; font-weight:bold;">&amp;</span>block<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#008000; font-style:italic;"># Get the first arg, which contain information about which underlying</span>
  <span style="color:#008000; font-style:italic;"># object to call.</span>
  id = arg<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># Call the corresponding underlying object with the first argument removed</span>
  my_underlying_objects<span style="color:#006600; font-weight:bold;">&#91;</span>id<span style="color:#006600; font-weight:bold;">&#93;</span>.__send__<span style="color:#006600; font-weight:bold;">&#40;</span>name, <span style="color:#006600; font-weight:bold;">*</span>args<span style="color:#006600; font-weight:bold;">&#91;</span>1..<span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>, <span style="color:#006600; font-weight:bold;">&amp;</span>block<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>


<p>If you execute this code, you&#8217;ll be stuck in an infinite loop. Why ? There&#8217;s a typo, one typo which will cause a <strong>segmentation fault</strong>. I wrote <em>arg[0]</em> instead of <em>args[0]</em>.</p>

<p>To detect this problem before it happens, we can take advantage of the <a href="http://www.ruby-doc.org/core/classes/Kernel.html#M005955" onclick="pageTracker._trackPageview('/outgoing/www.ruby-doc.org/core/classes/Kernel.html_M005955?referer=');">Kernel#caller</a> method. It generates the current execution stask. Here is how we can use it to detect that the current object is calling himself:</p>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> method_missing<span style="color:#006600; font-weight:bold;">&#40;</span>name, <span style="color:#006600; font-weight:bold;">*</span>args, <span style="color:#006600; font-weight:bold;">&amp;</span>block<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#008000; font-style:italic;"># Check that we're not calling 'method_missing' recursively</span>
  <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#CC0066; font-weight:bold;">caller</span>.<span style="color:#9900CC;">first</span>.<span style="color:#9966CC; font-weight:bold;">include</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">__FILE__</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#CC0066; font-weight:bold;">raise</span> <span style="color:#996600;">&quot;#{self.class} is calling itself -method #{name}-. Verify that you do not call a non existing method !!&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># Get the first arg, which contain information about which underlying</span>
  <span style="color:#008000; font-style:italic;"># object to call.</span>
  id = args<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># Call the corresponding underlying object with the first argument removed</span>
  my_underlying_objects<span style="color:#006600; font-weight:bold;">&#91;</span>id<span style="color:#006600; font-weight:bold;">&#93;</span>.__send__<span style="color:#006600; font-weight:bold;">&#40;</span>name, <span style="color:#006600; font-weight:bold;">*</span>args<span style="color:#006600; font-weight:bold;">&#91;</span>1..<span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>, <span style="color:#006600; font-weight:bold;">&amp;</span>block<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>


<p>That&#8217;s all, we just check that the caller method is not in the current <strong>file</strong>. If your <em>method_missing</em> code become more and more complex, especially if it includes some meta-programming tricks, you&#8217;ll feel A LOT safer!</p>

<p>One last thing: <a href="http://www.ruby-doc.org/core/classes/Kernel.html#M005955" onclick="pageTracker._trackPageview('/outgoing/www.ruby-doc.org/core/classes/Kernel.html_M005955?referer=');">Kernel#caller</a> is not what we could call a non-expensive method, you should only use it in development.</p>
]]></content:encoded>
			<wfw:commentRss>http://geeks.silentale.com/2008/08/23/beware-proxy-design-pattern-method_missing/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://geeks.silentale.com/2008/08/23/beware-proxy-design-pattern-method_missing/</feedburner:origLink></item>
		<item>
		<title>Go faster to your Home directory in the Terminal</title>
		<link>http://feedproxy.google.com/~r/SilentaleDevteam/~3/uZITjglSACk/</link>
		<comments>http://geeks.silentale.com/2008/06/03/go-faster-to-your-home-directory-in-the-terminal/#comments</comments>
		<pubDate>Tue, 03 Jun 2008 12:11:22 +0000</pubDate>
		<dc:creator>Nicolas</dc:creator>
				<category><![CDATA[Mac]]></category>
		<category><![CDATA[Terminal]]></category>

		<guid isPermaLink="false">http://www.12noobs.com/?p=16</guid>
		<description><![CDATA[In any Unix system, to go to your Home directory, you&#8217;d type this:


cd ~


The ~ character is hidden on Mac keyboards but like all other special characters, it is cleverly placed. You just have to push those keys:  n.

Why cleverly placed ? Think about the spanish ñ character. Try all key combinations with the [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;"><a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fgeeks.silentale.com%2F2008%2F06%2F03%2Fgo-faster-to-your-home-directory-in-the-terminal%2F" onclick="pageTracker._trackPageview('/outgoing/api.tweetmeme.com/share?url=http_3A_2F_2Fgeeks.silentale.com_2F2008_2F06_2F03_2Fgo-faster-to-your-home-directory-in-the-terminal_2F&amp;referer=');"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fgeeks.silentale.com%2F2008%2F06%2F03%2Fgo-faster-to-your-home-directory-in-the-terminal%2F" height="61" width="51" /></a></div><p>In any Unix system, to go to your Home directory, you&#8217;d type this:</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">cd</span> ~</pre></div></div>


<p>The <strong>~</strong> character is hidden on Mac keyboards but like all other <em>special</em> characters, it is cleverly placed. You just have to push those keys: <img src='http://www.danrodney.com/mac/img/menusym-option.gif' alt='Option' class='alignnone' /> <strong>n</strong>.</p>

<p>Why cleverly placed ? Think about the spanish <strong>ñ</strong> character. Try all key combinations with the <img src='http://www.danrodney.com/mac/img/menusym-option.gif' alt='Option' class='alignnone' /> with and without the <img src='http://www.danrodney.com/mac/img/menusym-shift.gif' alt='Shift' class='alignnone' /> key. You&#8217;ll be surprised. After several minutes, you&#8217;ll understand why each <em>special</em> character is placed on one specific character key.</p>

<p>But let&#8217;s get back to the subject of this post: to go to your Home directory, you don&#8217;t have to type the  <strong>~</strong> character, just type:</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">cd</span></pre></div></div>


<p>That&#8217;s it.</p>

<p>Now that you know everything about <em>special</em> characters, it&#8217;s time to learn the Mac OS X keyboard shortcuts. Feel free to visit the <a href="http://www.danrodney.com/mac/" onclick="pageTracker._trackPageview('/outgoing/www.danrodney.com/mac/?referer=');">Mac Central website</a>,  <em>your place for good, concise, Mac related hints</em>.</p>
]]></content:encoded>
			<wfw:commentRss>http://geeks.silentale.com/2008/06/03/go-faster-to-your-home-directory-in-the-terminal/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://geeks.silentale.com/2008/06/03/go-faster-to-your-home-directory-in-the-terminal/</feedburner:origLink></item>
	</channel>
</rss>
