<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	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/"
	>

<channel>
	<title>A Place for my head &#187; ruby</title>
	<atom:link href="http://gnufied.org/category/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://gnufied.org</link>
	<description>On Ruby, Rails, Concurrency and fiction</description>
	<lastBuildDate>Mon, 10 Oct 2011 06:49:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Parallel threads are still a myth in Ruby</title>
		<link>http://gnufied.org/2010/09/30/parallel-threads-are-still-a-myth-in-ruby/</link>
		<comments>http://gnufied.org/2010/09/30/parallel-threads-are-still-a-myth-in-ruby/#comments</comments>
		<pubDate>Thu, 30 Sep 2010 20:24:22 +0000</pubDate>
		<dc:creator>Hemant</dc:creator>
				<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[thread]]></category>

		<guid isPermaLink="false">http://gnufied.org/?p=167</guid>
		<description><![CDATA[In the beginning there was a giant lock. It wrapped all your requests and let only one run at  a time. Then it was removed in Rails 2.2, there was much to rejoice and some nay-sayers too. The world moved on, no one knew how many are actually using this new feature. Passenger app instance [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: center;"><a title="Giant Padlock by thedigitaliris, on Flickr" href="http://www.flickr.com/photos/lifethroughclaireslens/4646706703/"><img class="aligncenter" src="http://farm5.static.flickr.com/4067/4646706703_680744185f.jpg" alt="Giant Padlock" width="297" height="450" /></a></p>
<p style="text-align: left;">In the beginning there was a giant lock. It wrapped all your requests and let only one run at  a time. Then it was removed in Rails 2.2, there was much to <a href="http://blog.headius.com/2008/08/qa-what-thread-safe-rails-means.html">rejoice</a> and some <a href="http://m.onkey.org/2008/10/23/thread-safety-for-your-rails">nay-sayers</a> too. The world moved on, no one knew how many are actually using this new feature. Passenger app instance kept processing only one request a time, anyways. We did our bit of chest thumping and forgot about what good old m.on.key said.</p>
<p style="text-align: left;">But the debate seems to have been revived with Rubinius planning to remove GIL, JRuby already running threads parallely and finally MRI running native threads. On more serious note, I have been working with Ruby professionally for more than 4 years and I am astonished at anyone who is planning to run Ruby in a parallel environment. Igvita brought up that issue in his post (<a href="http://www.igvita.com/2008/11/13/concurrency-is-a-myth-in-ruby/">http://www.igvita.com/2008/11/13/concurrency-is-a-myth-in-ruby/</a>), but yehuda disagrees.</p>
<p style="text-align: left;">The problem isn&#8217;t whether underlying platform supports parallel native threads or not, but the problem is, does Ruby eco-system supports that?</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;">&quot;thread&quot;</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> Foo
  <span style="color:#9966CC; font-weight:bold;">def</span> hello
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Original hello&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
threads = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
threads <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#CC00FF; font-weight:bold;">Thread</span>.<span style="color:#9900CC;">new</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  <span style="color:#CC0066; font-weight:bold;">sleep</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  Foo.<span style="color:#9900CC;">class_eval</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    <span style="color:#9966CC; font-weight:bold;">def</span> hello
      <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Hello from modified thread #1 &quot;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
threads <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#CC00FF; font-weight:bold;">Thread</span>.<span style="color:#9900CC;">new</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  a = Foo.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  a.<span style="color:#9900CC;">hello</span>
  <span style="color:#CC0066; font-weight:bold;">sleep</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">3</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;After sleeping&quot;</span>
  a.<span style="color:#9900CC;">hello</span>
  b = Foo.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  b.<span style="color:#9900CC;">hello</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
threads.<span style="color:#9900CC;">each</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>t<span style="color:#006600; font-weight:bold;">|</span> t.<span style="color:#9900CC;">join</span> <span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>Above code runs as expected, but ruby runtime offers no guarantees AFAIK about visibility of modifications in class objects. Lets take another 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> synchronize<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>methods<span style="color:#006600; font-weight:bold;">&#41;</span>
    options = methods.<span style="color:#9900CC;">extract_options</span>!
    <span style="color:#9966CC; font-weight:bold;">unless</span> options.<span style="color:#9900CC;">is_a</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">Hash</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&amp;&amp;</span> with = options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:with</span><span style="color:#006600; font-weight:bold;">&#93;</span>
      <span style="color:#CC0066; font-weight:bold;">raise</span> <span style="color:#CC00FF; font-weight:bold;">ArgumentError</span>, <span style="color:#996600;">&quot;Synchronization needs a mutex. Supply an options hash with a :with key as the last argument (e.g. synchronize :hello, :with =&gt; :@mutex).&quot;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    methods.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>method<span style="color:#006600; font-weight:bold;">|</span>
      aliased_method, punctuation = method.<span style="color:#9900CC;">to_s</span>.<span style="color:#CC0066; font-weight:bold;">sub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</span>?!=<span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>$<span style="color:#006600; font-weight:bold;">/</span>, <span style="color:#996600;">''</span><span style="color:#006600; font-weight:bold;">&#41;</span>, $1
&nbsp;
      <span style="color:#9966CC; font-weight:bold;">if</span> method_defined?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;#{aliased_method}_without_synchronization#{punctuation}&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#CC0066; font-weight:bold;">raise</span> <span style="color:#CC00FF; font-weight:bold;">ArgumentError</span>, <span style="color:#996600;">&quot;#{method} is already synchronized. Double synchronization is not currently supported.&quot;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
      module_eval<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&lt;&lt;-</span>EOS, <span style="color:#0000FF; font-weight:bold;">__FILE__</span>, <span style="color:#0000FF; font-weight:bold;">__LINE__</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#008000; font-style:italic;">#{aliased_method}_with_synchronization#{punctuation}(*args, &amp;block)   </span>
          <span style="color:#008000; font-style:italic;">#{with}.synchronize do </span>
            <span style="color:#008000; font-style:italic;">#{aliased_method}_without_synchronization#{punctuation}(*args, &amp;block)</span>
          <span style="color:#9966CC; font-weight:bold;">end</span>                                                     
        <span style="color:#9966CC; font-weight:bold;">end</span>
      EOS
&nbsp;
      alias_method_chain method, <span style="color:#ff3333; font-weight:bold;">:synchronization</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>The idea is, you don&#8217;t need to start a synchronize block in your method, if you want entire method body to be wrapped inside synchronized block, rather than that you can specify entire contract at class level. Neat!<br />
Except not, how does it work when classes are getting reloaded in development mode? Will this metaprogrammtically created synchronize hold? The answer will vary from Ruby to Ruby to implementation. In parallel environment, it will not. </p>
<p>The problem I am trying to drive at is, Ruby&#8217;s memory model makes no guarantees about class state. It&#8217;s a problem, no one talks about. </p>
<p>Lets take another example of &#8220;thread safe&#8221; code, from ActiveRecord connection pool (<a href="http://github.com/rails/rails/blob/master/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb">http://github.com/rails/rails/blob/master/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb</a>).<br />
Anyone closely reading the code can find few thread problems with the code. For example, &#8220;@reserved_connections&#8221; seems be read and written by multiple threads without bother. It may work fine in Ruby 1.8 and 1.9, but the code is definitely not thread safe, if multiple threads are allowed to run parallel. A solution would be perhaps to use, concurrent hash (<a href="http://stackoverflow.com/questions/1080993/pure-ruby-concurrent-hash">http://stackoverflow.com/questions/1080993/pure-ruby-concurrent-hash</a>).</p>
<p>Where are we getting with this? In my opinion, until Ruby runtime makes guarantees about class state, module state ,  core threading primitives (Queue, Monitors, MonitorMixin, ConditionVariable) get exhaustively tested, concurrent collections are added; Ruby isn&#8217;t ready for parallel threads. In my professional experience, I had several problems with ruby&#8217;s default primitives. I was listening to a talk by ThoughWorks folks, who were using JRuby, single VM multiple threads for running rails for Mingle, their problems were simply too hard to catch and hard to debug. </p>
<p>In the meanwhile, Ruby community should focus on getting evented and co-operative threading proper. Fiber is a good start. </p>
<p><i> Thanks to folks in #ruby-pro for reviewing the post. Specially to James Tucker(raggi) </i></p>
]]></content:encoded>
			<wfw:commentRss>http://gnufied.org/2010/09/30/parallel-threads-are-still-a-myth-in-ruby/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Debug RESTful calls for fun and profit</title>
		<link>http://gnufied.org/2010/08/06/debug-restful-calls-for-fun-and-profit/</link>
		<comments>http://gnufied.org/2010/08/06/debug-restful-calls-for-fun-and-profit/#comments</comments>
		<pubDate>Sat, 07 Aug 2010 03:34:22 +0000</pubDate>
		<dc:creator>Hemant</dc:creator>
				<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://gnufied.org/?p=157</guid>
		<description><![CDATA[When using ActiveResource it can be quite useful to print Webservice calls going out and XML responses coming in. It allows me to debug things, it allows me to create useful Httpmocks for testing code that uses the resource. Somewhat unobtrusive way of doing this is, stick following code to some library file: module ActiveResource [...]]]></description>
			<content:encoded><![CDATA[<p>When using ActiveResource it can be quite useful to print Webservice calls going out and XML responses coming in. It allows me to debug things, it allows me to create useful Httpmocks for testing code that uses the resource.</p>
<p>Somewhat unobtrusive way of doing this is, stick following code to some library file:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">module</span> ActiveResource
  <span style="color:#9966CC; font-weight:bold;">class</span> Connection
    alias_method <span style="color:#ff3333; font-weight:bold;">:old_request</span>, <span style="color:#ff3333; font-weight:bold;">:request</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> request<span style="color:#006600; font-weight:bold;">&#40;</span>method, path, <span style="color:#006600; font-weight:bold;">*</span> arguments<span style="color:#006600; font-weight:bold;">&#41;</span>
      response = old_request<span style="color:#006600; font-weight:bold;">&#40;</span>method, path, <span style="color:#006600; font-weight:bold;">*</span> arguments<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span>response <span style="color:#006600; font-weight:bold;">&amp;&amp;</span> response.<span style="color:#9900CC;">code</span>.<span style="color:#9900CC;">to_i</span> == <span style="color:#006666;">200</span> <span style="color:#006600; font-weight:bold;">&amp;&amp;</span> APP_CONFIG<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'debug_resource_call'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#CC0066; font-weight:bold;">puts</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;********** method is #{method} and path is #{path} ********** &quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#CC0066; font-weight:bold;">puts</span> response.<span style="color:#9900CC;">body</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
      response
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>And off I can go with <a href="http://robots.thoughtbot.com/post/159809120/activeresource-and-testing"> Httpmock </a></p>
]]></content:encoded>
			<wfw:commentRss>http://gnufied.org/2010/08/06/debug-restful-calls-for-fun-and-profit/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Autotest and add_exception method</title>
		<link>http://gnufied.org/2009/01/25/autotest-and-add_exception-method/</link>
		<comments>http://gnufied.org/2009/01/25/autotest-and-add_exception-method/#comments</comments>
		<pubDate>Sun, 25 Jan 2009 18:21:01 +0000</pubDate>
		<dc:creator>Hemant</dc:creator>
				<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://gnufied.org/?p=127</guid>
		<description><![CDATA[Stuff floating on intrawebs for adding more files to be ignored using &#8220;add_exception&#8221; may not work because by the time &#8220;run&#8221; hook gets a chance to run, files to be ignored regexp is already compiled. The alternative is to use &#8220;initialize&#8221; hook like this: 1 2 3 Autotest.add_hook :initialize do &#124;at&#124; at.add_exception&#40;/^&#40;coverage&#124;\.git&#41;/&#41; end This is [...]]]></description>
			<content:encoded><![CDATA[<p>Stuff floating on intrawebs for adding more files to be ignored using &#8220;add_exception&#8221; may not work because by the time &#8220;run&#8221; hook gets a chance to run, files to be ignored regexp is already compiled. The alternative is to use &#8220;initialize&#8221; hook like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">Autotest.<span style="color:#9900CC;">add_hook</span> <span style="color:#ff3333; font-weight:bold;">:initialize</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>at<span style="color:#006600; font-weight:bold;">|</span>
  at.<span style="color:#9900CC;">add_exception</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>^<span style="color:#006600; font-weight:bold;">&#40;</span>coverage<span style="color:#006600; font-weight:bold;">|</span>\.<span style="color:#9900CC;">git</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>This is with 3.11.0 of ZenTest and YMMV.</p>
]]></content:encoded>
			<wfw:commentRss>http://gnufied.org/2009/01/25/autotest-and-add_exception-method/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Detect if daemon is really running</title>
		<link>http://gnufied.org/2008/11/02/detect-if-daemon-is-really-running/</link>
		<comments>http://gnufied.org/2008/11/02/detect-if-daemon-is-really-running/#comments</comments>
		<pubDate>Sun, 02 Nov 2008 07:55:13 +0000</pubDate>
		<dc:creator>Hemant</dc:creator>
				<category><![CDATA[backgroundrb]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://gnufied.org/?p=106</guid>
		<description><![CDATA[You know the story, how your daemon die unexpectedly (mongrel,thin,BackgrounDRb) and on restart they complain about existing pid file (and assume its running). For BackgrounDRb we solved this irksome problem in following way: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 def really_running? pid begin [...]]]></description>
			<content:encoded><![CDATA[<p>You know the story, how your daemon die unexpectedly (mongrel,thin,BackgrounDRb) and on restart they complain about existing pid file (and assume its running). For BackgrounDRb we solved this irksome problem in following way:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> really_running? pid
  <span style="color:#9966CC; font-weight:bold;">begin</span>
    <span style="color:#CC00FF; font-weight:bold;">Process</span>.<span style="color:#9900CC;">kill</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">0</span>,pid<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0000FF; font-weight:bold;">true</span>
  <span style="color:#9966CC; font-weight:bold;">rescue</span> <span style="color:#CC00FF; font-weight:bold;">Errno</span>::ESRCH
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;pid file exists but process doesn't seem to be running restarting now&quot;</span>
    <span style="color:#0000FF; font-weight:bold;">false</span>
  <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;">def</span> try_restart
  pid = <span style="color:#0000FF; font-weight:bold;">nil</span>
  pid = <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>PID_FILE, <span style="color:#996600;">&quot;r&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>pid_handle<span style="color:#006600; font-weight:bold;">|</span> pid_handle.<span style="color:#CC0066; font-weight:bold;">gets</span>.<span style="color:#9900CC;">strip</span>.<span style="color:#CC0066; font-weight:bold;">chomp</span>.<span style="color:#9900CC;">to_i</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">if</span> really_running? pid
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;pid file already exists, exiting...&quot;</span>
    <span style="color:#CC0066; font-weight:bold;">exit</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">1</span><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></pre></td></tr></table></div>

<p>Simple, try sending signal &#8220;0&#8243; and if process responds to it, its alive otherwise pid file is stale and daemon can be safely restarted. There could be many more ways to solve this problem, for example to grep output of &#8220;ps aux&#8221;.</p>
]]></content:encoded>
			<wfw:commentRss>http://gnufied.org/2008/11/02/detect-if-daemon-is-really-running/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rails debugging warmhole</title>
		<link>http://gnufied.org/2008/08/20/rails-debugging-warmhole/</link>
		<comments>http://gnufied.org/2008/08/20/rails-debugging-warmhole/#comments</comments>
		<pubDate>Wed, 20 Aug 2008 07:35:42 +0000</pubDate>
		<dc:creator>Hemant</dc:creator>
				<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://gnufied.org/?p=95</guid>
		<description><![CDATA[Since, we started to scale to multi machine clustering, our rails application was showing one weird problem. We were generating Stock Market Technical charts and caching them in memcache (with 1 hour ttl). During testing we found that, if requests gets routed to mongrel cluster running on other machine, charts aren&#8217;t appearing, which was weird [...]]]></description>
			<content:encoded><![CDATA[<p>Since, we started to scale to multi machine clustering, our rails application was showing one weird problem. We were generating Stock Market Technical charts and caching them in memcache (with 1 hour ttl). During testing we found that, if requests gets routed to mongrel cluster running on other machine, charts aren&#8217;t appearing, which was weird because we had memcache cluster configured properly in &#8220;production.rb&#8221; file:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">memcache_options = <span style="color:#006600; font-weight:bold;">&#123;</span>
  <span style="color:#ff3333; font-weight:bold;">:c_threshold</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">10</span>_000,
  <span style="color:#ff3333; font-weight:bold;">:compression</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span>,
  <span style="color:#ff3333; font-weight:bold;">:debug</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">false</span>,
  <span style="color:#ff3333; font-weight:bold;">:namespace</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'foobar'</span>
  <span style="color:#ff3333; font-weight:bold;">:readonly</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">false</span>,
  <span style="color:#ff3333; font-weight:bold;">:urlencode</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">false</span>
<span style="color:#006600; font-weight:bold;">&#125;</span>
CACHE = MemCache.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>memcache_options<span style="color:#006600; font-weight:bold;">&#41;</span>
CACHE.<span style="color:#9900CC;">servers</span> = SIMPLE_CONFIG_FILE<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;memcache_servers&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span></pre></td></tr></table></div>

<p>Where SIMPLE_CONFIG_FILE['memcache_servers'] contains list of memcache clusters participating in clustering. After, debugging for few hours(*gasp*) and turning on verbose logging on all participating memcache clusters, I found that, trusty <a href="http://errtheblog.com/posts/57-kickin-ass-w-cachefu"> CacheFu </a>, was replacing the CACHE constant with following code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">silence_warnings <span style="color:#9966CC; font-weight:bold;">do</span>
  <span style="color:#CC00FF; font-weight:bold;">Object</span>.<span style="color:#9900CC;">const_set</span> <span style="color:#ff3333; font-weight:bold;">:CACHE</span>, memcache_klass.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>config<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#CC00FF; font-weight:bold;">Object</span>.<span style="color:#9900CC;">const_set</span> <span style="color:#ff3333; font-weight:bold;">:SESSION_CACHE</span>, memcache_klass.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>config<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">if</span> config<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:session_servers</span><span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
CACHE.<span style="color:#9900CC;">servers</span> = <span style="color:#CC0066; font-weight:bold;">Array</span><span style="color:#006600; font-weight:bold;">&#40;</span>config.<span style="color:#9900CC;">delete</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:servers</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
SESSION_CACHE.<span style="color:#9900CC;">servers</span> = <span style="color:#CC0066; font-weight:bold;">Array</span><span style="color:#006600; font-weight:bold;">&#40;</span>config<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:session_servers</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">if</span> config<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:session_servers</span><span style="color:#006600; font-weight:bold;">&#93;</span></pre></td></tr></table></div>

<p>Now this deal is real (and sucks). After couple of minutes of hacking, I took out cache_fu config file out of svn,wrote code to generate it on the fly during deployment. Now, pigs can fly.</p>
]]></content:encoded>
			<wfw:commentRss>http://gnufied.org/2008/08/20/rails-debugging-warmhole/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Making Ruby Bacon play with Mocha</title>
		<link>http://gnufied.org/2008/06/12/making-ruby-bacon-play-with-mocha/</link>
		<comments>http://gnufied.org/2008/06/12/making-ruby-bacon-play-with-mocha/#comments</comments>
		<pubDate>Thu, 12 Jun 2008 21:01:24 +0000</pubDate>
		<dc:creator>Hemant</dc:creator>
				<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://gnufied.org/2008/06/12/making-ruby-bacon-play-with-mocha/</guid>
		<description><![CDATA[This post is not about pork and coffee. So, stay clear, if google has landed you here thinking I am going to describe some sort of recipe for making nice mocha coffee with chunky bacons. Its about using shiny new testing library called Bacon by Chris. Mocha is of course, venerable mocking library for Ruby [...]]]></description>
			<content:encoded><![CDATA[<p>This post is not about pork and coffee. So, stay clear, if google has landed you here thinking I am going to describe some sort of recipe for making nice mocha coffee with chunky bacons.</p>
<p>Its about using shiny new testing library called <a href="http://bacon.rubyforge.org" target="_blank">Bacon</a> by Chris. <a href="http://mocha.rubyforge.org" target="_blank">Mocha</a> is of course, venerable mocking library for Ruby and Rails.Here is a tiny bit of code that will make you started with bacon and mocha:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">&quot;rubygems&quot;</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">&quot;bacon&quot;</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">&quot;mocha/standalone&quot;</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">&quot;mocha/object&quot;</span>
<span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#6666ff; font-weight:bold;">Bacon::Context</span>
  <span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#6666ff; font-weight:bold;">Mocha::Standalone</span>
  alias_method <span style="color:#ff3333; font-weight:bold;">:old_it</span>,:it
  <span style="color:#9966CC; font-weight:bold;">def</span> it description,<span style="color:#006600; font-weight:bold;">&amp;</span>block
    mocha_setup
    old_it<span style="color:#006600; font-weight:bold;">&#40;</span>description,<span style="color:#006600; font-weight:bold;">&amp;</span>block<span style="color:#006600; font-weight:bold;">&#41;</span>
    mocha_verify
    mocha_teardown
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>Thats it. Happy baking.</p>
]]></content:encoded>
			<wfw:commentRss>http://gnufied.org/2008/06/12/making-ruby-bacon-play-with-mocha/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Unthreaded threads of hobbiton</title>
		<link>http://gnufied.org/2008/06/12/unthreaded-threads-of-hobbiton/</link>
		<comments>http://gnufied.org/2008/06/12/unthreaded-threads-of-hobbiton/#comments</comments>
		<pubDate>Thu, 12 Jun 2008 19:46:20 +0000</pubDate>
		<dc:creator>Hemant</dc:creator>
				<category><![CDATA[backgroundrb]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://gnufied.org/2008/06/12/unthreaded-threads-of-hobbiton/</guid>
		<description><![CDATA[Update: With 1.0.4 release, this method has been removed. It was introduced as a workaround for thread unsafe register_status, but its no longer required, since result caching is anyway threadsafe in this version. You know the story too well, in your BackgrounDRb worker, you want to run 10 tasks concurrently using thread pool and collect [...]]]></description>
			<content:encoded><![CDATA[<p><b>Update:</b> With 1.0.4 release, this method has been removed. It was introduced as a workaround for thread unsafe register_status, but its no longer required, since result caching is anyway threadsafe in this version.</p>
<p>You know the story too well, in your BackgrounDRb worker, you want to run 10 tasks concurrently using thread pool and collect the results back in a instance variable and return it. Now, threads are funny little beasts and simplest of things can easily go out of hand. For example, one of BackgrounDRb users wrote something like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">pages = <span style="color:#CC0066; font-weight:bold;">Array</span>.<span style="color:#9900CC;">new</span>
pages_to_scrape.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>url<span style="color:#006600; font-weight:bold;">|</span>
  thread_pool.<span style="color:#9900CC;">defer</span><span style="color:#006600; font-weight:bold;">&#40;</span>url<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>url<span style="color:#006600; font-weight:bold;">|</span>
    <span style="color:#9966CC; font-weight:bold;">begin</span>
      <span style="color:#008000; font-style:italic;"># model object performs the scraping</span>
      page = ScrapedPage.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>page.<span style="color:#9900CC;">url</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      pages <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> page
    <span style="color:#9966CC; font-weight:bold;">rescue</span>
      logger.<span style="color:#9900CC;">info</span> <span style="color:#996600;">&quot;page scrape failed&quot;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#0000FF; font-weight:bold;">return</span> pages</pre></td></tr></table></div>

<p>There are many things thats wrong with above code, and one of them is, it modifies a shared variable without acquiring a lock. Remember anything inside thread_pool.defer happens inside a separate thread and hence should be thread safe.</p>
<p>Another use case, will be, say you don&#8217;t want to spawn gazillions of workers and rather within one worker, you want to process requests from n users and save the results back using &#8220;register_status&#8221;  with user_id as identifier. thread_pool.defer is for fire and forget kind of jobs and using register_status within block supplied to thread_pool.defer is dangerous.</p>
<p>Enter thread_pool.fetch_parallely(args,request_proc,response_proc). Lets have an example going. Inside one of your workers:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">  <span style="color:#9966CC; font-weight:bold;">def</span> barbar args
    request_proc = <span style="color:#CC0066; font-weight:bold;">lambda</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>foo<span style="color:#006600; font-weight:bold;">|</span>
      <span style="color:#CC0066; font-weight:bold;">sleep</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#996600;">&quot;Hello #{foo}&quot;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    callback = <span style="color:#CC0066; font-weight:bold;">lambda</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>result<span style="color:#006600; font-weight:bold;">|</span>
      register_status<span style="color:#006600; font-weight:bold;">&#40;</span>result<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
    thread_pool.<span style="color:#9900CC;">fetch_parallely</span><span style="color:#006600; font-weight:bold;">&#40;</span>args,request_proc,callback<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>First argument to fetch_parallely is some data argument that will be simply passed to request_proc. Note that, its necessary and you should not rely on the fact that a closure captures the local scope. Within threads, it could be dangerous. Now, last return value of request_proc will be passed as an argument to callback proc, when request_proc finishes execution and is ready with result.</p>
<p>The difference here is, although request_proc runs within new thread, calback proc gets executed within main thread and hence need not be thread safe. You can use pretty much do anything you want within callback proc. </p>
<p>This is available in git version of BackgrounDRb. Here is the link on, how to install git version of BackgrounDRb.</p>
<p><a href="http://gnufied.org/2008/05/21/bleeding-edge-version-of-backgroundrb-for-better-memory-usage/">http://gnufied.org/2008/05/21/bleeding-edge-version-of-backgroundrb-for-better-memory-usage/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://gnufied.org/2008/06/12/unthreaded-threads-of-hobbiton/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Your Comet Server In Scala</title>
		<link>http://gnufied.org/2008/05/21/your-comet-server-in-scala/</link>
		<comments>http://gnufied.org/2008/05/21/your-comet-server-in-scala/#comments</comments>
		<pubDate>Wed, 21 May 2008 18:13:47 +0000</pubDate>
		<dc:creator>Hemant</dc:creator>
				<category><![CDATA[ruby]]></category>
		<category><![CDATA[scala]]></category>

		<guid isPermaLink="false">http://gnufied.org/2008/05/21/your-comet-server-in-scala/</guid>
		<description><![CDATA[I thought it will be cool to display real time stock market streaming ticks in our marketsimplified application. I needed wee bit of flash code that opens an XML socket to our comet server,accepts data and invokes corresponding javascript function in browser for displaying streaming data. I took the flash code from Juggernaut project and [...]]]></description>
			<content:encoded><![CDATA[<p>I thought it will be cool to display real time stock market streaming ticks in our <a href="http://www.marketsimplified.com">marketsimplified</a> application.<br />
<a href="http://gnufied.org/wp-content/uploads/2008/05/watchlist1.png" title="Small Watchlist"><img src="http://gnufied.org/wp-content/uploads/2008/05/watchlist1.png" alt="Small Watchlist" /></a></p>
<p>I needed wee bit of flash code that opens an XML socket to our comet server,accepts data and invokes corresponding javascript function in browser for displaying streaming data. I took the flash code from Juggernaut project and compiled it to a swf. Now, we needed a Comet Server. The initial version I wrote in Ruby using EventMachine. It was pretty darn good, you can plugin and stream how many type of data you want, you just need to inherit a class and you were done. But it was slow and was unable to handle influx of stock market ticks. We had to restart the damn comet server everyday.</p>
<p>So I proceeded to port Comet Server in Scala. Before choosing Scala, I played with Erlang and D. My attempts at learning Erlang and using it for our Comet Server were serious. Why I gave up Erlang was mostly because:</p>
<ul>
<li> String handling and near absence of it. Yes sure one can write a recursive descent parser, but for small string manipulation, its an overkill. Our internal data exchange protocol is line oriented and the parers that I wrote used string manipulation.</li>
<li> I dunno, if many will agree, but Erlang is hard. Yeah, probably not for simple &#8220;hello world&#8221; applications, but on whole you need to turn your head quite a bit. I wasn&#8217;t sure, if my colleagues will buy into it.</li>
<li> Even if you ignore above two critireas, I believe Erlang needs quite different Eco System. Our application is already built around open source technologies, such as &#8211; Mysql, MemCache, Rails, Hibernate, Ruby, Python, Nginx, Mongrel and controlled by YAML files. Hunting Erlang libraries for mysql, memcache, yaml, json and making them work seemed like too much work.</li>
</ul>
<p>Anyways, that was my decision so get over it. I briefly flirted with D. I wasn&#8217;t very happy with libraries, their installation procedure and their API. I started with Scala long ago, playing it with now and then. But after my Erlang tryst fizzled out, I decided to look into Scala seriously. There were no decent Network programming libraries for Scala. Sure, I could have used Mina, but I wanted a more Scalasque library, which helps in me translating my EventMachine code to Scala and hence I wrote <a href="http://eventfax.googlecode.com">Eventfax</a>. ( Code in public svn is a bit stale, our corporate svns have latest code, which I will be publish soon )</p>
<p>For example a EchoServer using Eventfax library will look like:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">scala.actors._</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">scala.actors.Actor._</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">eventfax.core._</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">eventfax.protocol._</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// define a reactor core</span>
<span style="color: #000000; font-weight: bold;">class</span> EchoReactor<span style="color: #009900;">&#40;</span>starterBlock<span style="color: #339933;">:</span> ReactorCore <span style="color: #339933;">=&gt;</span> Unit<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">extends</span> ReactorCore<span style="color: #009900;">&#40;</span>starterBlock<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  def check_for_actor_messages <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// define a factor class</span>
object EchoServerFactory <span style="color: #000000; font-weight: bold;">extends</span> ConnectionFactory <span style="color: #009900;">&#123;</span>
  def create_connection<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span> <span style="color: #003399;">Connection</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">new</span> EchoServer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> EchoServer <span style="color: #000000; font-weight: bold;">extends</span> <span style="color: #003399;">Connection</span> <span style="color: #009900;">&#123;</span>
  val buftok<span style="color: #339933;">:</span> LineProtocol <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> LineProtocol<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;[<span style="color: #000099; font-weight: bold;">\n</span>]&quot;</span><span style="color: #009900;">&#41;</span>
  val masterActor<span style="color: #339933;">:</span> EchoReactor <span style="color: #339933;">=</span> reactor.<span style="color: #006633;">asInstanceOf</span><span style="color: #009900;">&#91;</span>EchoReactor<span style="color: #009900;">&#93;</span>
  def receive_data<span style="color: #009900;">&#40;</span>p_data<span style="color: #339933;">:</span> FaxData<span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
    buftok.<span style="color: #006633;">extract</span><span style="color: #009900;">&#40;</span>p_data<span style="color: #009900;">&#41;</span>
    buftok.<span style="color: #000000; font-weight: bold;">foreach</span><span style="color: #009900;">&#40;</span>str_data <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#123;</span>
      dispatch_request<span style="color: #009900;">&#40;</span>str_data<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  def dispatch_request<span style="color: #009900;">&#40;</span>str_data<span style="color: #339933;">:</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
    val t_str <span style="color: #339933;">=</span> str_data.<span style="color: #006633;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>t_str.<span style="color: #006633;">isEmpty</span><span style="color: #009900;">&#41;</span>
      send_data<span style="color: #009900;">&#40;</span>str_data<span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  def on_write_error<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span> close_connection <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">case</span> ex<span style="color: #339933;">:</span> <span style="color: #003399;">IOException</span> <span style="color: #339933;">=&gt;</span> println<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;error&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
  def unbind<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
object EchoServerRunner <span style="color: #009900;">&#123;</span>
  def main<span style="color: #009900;">&#40;</span>args<span style="color: #339933;">:</span> <span style="color: #003399;">Array</span><span style="color: #009900;">&#91;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">new</span> EchoReactor<span style="color: #009900;">&#40;</span>t_reactor <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#123;</span>
      t_reactor.<span style="color: #006633;">start_server</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;localhost&quot;</span>,<span style="color: #cc66cc;">8700</span>,EchoServerFactory<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Also, I had to port our protocol parsing libraries to Scala, which proved trivial. After a week of effort, our watchlist is powered by a Comet Server written Scala. I used Scala Specs for testing, buildr for compiling and packaging.</p>
<p>I am yet to get buildr working properly with Scala specs, but I will perhaps look into that later. Getting Memcache,Mysql, YAML or JSON working with Scala was trivial. Although, Json parser of Scala is one cane full of worms. Every new scala release seems to introduce new bugs in it. I had to spend quite sometime in getting around them.</p>
]]></content:encoded>
			<wfw:commentRss>http://gnufied.org/2008/05/21/your-comet-server-in-scala/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Bleeding Edge Version of BackgrounDRb for better memory usage</title>
		<link>http://gnufied.org/2008/05/21/bleeding-edge-version-of-backgroundrb-for-better-memory-usage/</link>
		<comments>http://gnufied.org/2008/05/21/bleeding-edge-version-of-backgroundrb-for-better-memory-usage/#comments</comments>
		<pubDate>Wed, 21 May 2008 04:58:17 +0000</pubDate>
		<dc:creator>Hemant</dc:creator>
				<category><![CDATA[backgroundrb]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://gnufied.org/2008/05/21/bleeding-edge-version-of-backgroundrb-for-better-memory-usage/</guid>
		<description><![CDATA[Update: Do not forget to get rid of autogenerated backgroundrb etcetera files that were generated during last run of rake backgroundrb:setup. A plain fork() is evil under Ruby and hence there are some issues with memory usage of BackgrounDRb. I have made some changes to packet library so as it uses fork and exec rather [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Update:</strong> Do not forget to get rid of autogenerated backgroundrb etcetera files that were generated during last run of rake backgroundrb:setup.</p>
<p>A plain fork() is evil under Ruby and hence there are some issues with memory usage of BackgrounDRb. I have made some changes to packet library so as it uses fork and exec rather than just fork for better memory usage. However, there were quite bit of changes and BackgrounDRb is affected by them. You can try bleeding edge version as following.Also you need rspec for building packet gem.</p>
<p>clone the packet git repo:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">git</span> clone <span style="color: #c20cb9; font-weight: bold;">git</span>:<span style="color: #000000; font-weight: bold;">//</span>github.com<span style="color: #000000; font-weight: bold;">/</span>gnufied<span style="color: #000000; font-weight: bold;">/</span>packet.git
<span style="color: #7a0874; font-weight: bold;">cd</span> packet;rake gem
<span style="color: #7a0874; font-weight: bold;">cd</span> pkg; <span style="color: #c20cb9; font-weight: bold;">sudo</span> gem <span style="color: #c20cb9; font-weight: bold;">install</span> <span style="color: #660033;">--local</span> packet-0.1.6.gem</pre></div></div>

<p>Go to your vendor/plugins directory of your rails directory and remove or<br />
backup older version of backgroundrb plugin and backup related config<br />
file as well.</p>
<p>from vendor/plugins directory:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">git</span> clone <span style="color: #c20cb9; font-weight: bold;">git</span>:<span style="color: #000000; font-weight: bold;">//</span>github.com<span style="color: #000000; font-weight: bold;">/</span>gnufied<span style="color: #000000; font-weight: bold;">/</span>backgroundrb.git
<span style="color: #7a0874; font-weight: bold;">cd</span> RAILS_ROOT
rake backgroundrb:setup
.<span style="color: #000000; font-weight: bold;">/</span>script<span style="color: #000000; font-weight: bold;">/</span>backgroundrb start</pre></div></div>

<p>Let me know,how it goes.</p>
]]></content:encoded>
			<wfw:commentRss>http://gnufied.org/2008/05/21/bleeding-edge-version-of-backgroundrb-for-better-memory-usage/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>Tips for budding Ruby hacker</title>
		<link>http://gnufied.org/2008/02/24/tips-for-budding-ruby-hacker/</link>
		<comments>http://gnufied.org/2008/02/24/tips-for-budding-ruby-hacker/#comments</comments>
		<pubDate>Sun, 24 Feb 2008 20:26:42 +0000</pubDate>
		<dc:creator>Hemant</dc:creator>
				<category><![CDATA[rails]]></category>
		<category><![CDATA[rant]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://gnufied.org/2008/02/24/tips-for-budding-ruby-hacker/</guid>
		<description><![CDATA[I am no expert in Ruby, but overtime I have accumulated some thoughts that may help you in writing better Ruby code. Always create a directory hierarchy for your library/application. Such as: &#124;__ bin &#124;__ lib &#124;__ tests &#124;__ yaml_specs If you are not writing a library and rather an executable application. Then, have a [...]]]></description>
			<content:encoded><![CDATA[<p>I am no expert in Ruby, but overtime I have accumulated some thoughts that may help you in writing better Ruby code. </p>
<ul>
<li> Always create a directory hierarchy for your library/application. Such as:

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">   <span style="color: #000000; font-weight: bold;">|</span>__ bin
   <span style="color: #000000; font-weight: bold;">|</span>__ lib
   <span style="color: #000000; font-weight: bold;">|</span>__ tests
   <span style="color: #000000; font-weight: bold;">|</span>__ yaml_specs</pre></div></div>

</li>
</li>
<p> If you are not writing a library and rather an executable application. Then, have a separate file that loads/requires required libraries and does some basic stuff. For example, I have a boot.rb in my Comet server that looks like:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">&quot;rubygems&quot;</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">&quot;eventmachine&quot;</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">&quot;buftok&quot;</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">&quot;sequel/mysql&quot;</span>
PUSH_SERVER_PATH = <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">expand_path</span><span style="color:#006600; font-weight:bold;">&#40;</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:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;lib&quot;</span>,<span style="color:#996600;">&quot;channels&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">each</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span> $:.<span style="color:#9900CC;">unshift</span><span style="color:#006600; font-weight:bold;">&#40;</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>PUSH_SERVER_PATH,x<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">&quot;push_server&quot;</span></pre></td></tr></table></div>

<p>  Why? Because such a file can come handy when you are writing test_helper for your applications. There, you can simply require above boot.rb, so as you don&#8217;t have to copy stuff back and forth if your required libs change. </li>
<li> If your project hierarchy is like above and you are writing an library not an application, don&#8217;t make the mistake of putting all your files in lib directory straightaway. Rather have a setup like:

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">  Root
  <span style="color: #000000; font-weight: bold;">|</span>__ bin
  <span style="color: #000000; font-weight: bold;">|</span>__ lib
  <span style="color: #000000; font-weight: bold;">|</span>__ lib<span style="color: #000000; font-weight: bold;">/</span>packet.rb
  <span style="color: #000000; font-weight: bold;">|</span>__ lib<span style="color: #000000; font-weight: bold;">/</span>packet<span style="color: #000000; font-weight: bold;">/</span>other files go here</pre></div></div>

<p>  And use relative requires in &#8220;packet.rb&#8221; file, like:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">$:.<span style="color:#9900CC;">unshift</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:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">unless</span>
  $:.<span style="color:#9966CC; font-weight:bold;">include</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:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">||</span> $:.<span style="color:#9966CC; font-weight:bold;">include</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">expand_path</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:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">&quot;packet/packet_parser&quot;</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">&quot;packet/packet_meta_pimp&quot;</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">&quot;packet/packet_core&quot;</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">&quot;packet/packet_master&quot;</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">&quot;packet/packet_connection&quot;</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">&quot;packet/packet_worker&quot;</span>
&nbsp;
PACKET_APP = <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">expand_path</span><span style="color:#996600;">'../'</span> <span style="color:#9966CC; font-weight:bold;">unless</span> <span style="color:#9966CC; font-weight:bold;">defined</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>PACKET_APP<span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">module</span> Packet
  VERSION=<span style="color:#996600;">'0.1.4'</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p> It would be helpful in avoiding package name collisions that otherwise your users will report.
</li>
<li>  Once <a href="http://chneukirchen.org/blog/">chris2 </a> mentioned on #ruby-lang, you shouldn&#8217;t be overly clever with test cases. Don&#8217;t try to be too DRY in your test cases. </li>
<li> Write code that can be <em> easilly </em> tested. What the fuck that means? When I started with Ruby and was doing Network programming. I used to write methods like ughh, that always manipulated state through instance variables. I either used threads or EventMachine. One of the issues with EventMachine is, code written usually relies on state machine and hence it can be notoriously difficult to unit test, because most of the time your methods are working according to the state of instance of variables. That was bad. Try to write code in more functional way, where methods take some parameters and return some values based on arguments. You should minimize methods with side effects as much as possible. This will make your code more readable and easily <em>testable </em></li>
<li> Read code of some good libraries, such as <a href="http://ramaze.net/">Ramaze </a>, Rake, standard library. </li>
<li> Use <a href="https://rubyforge.org/projects/fastri/">FastRi</a> rather than Ri. If possible, generate your set of documentation using rdoc on Ruby source code. I spend time just looking through methods, classes just for fun. However, I don&#8217;t like the default default RDoc template, use Jamis RDoc template, if you like Rails documentation. Often for gems installed on your machine, you can use gem server or gem_server to view their documentation.</li>
<li> #ruby-lang on freenode is generally a good place to shoot general Ruby questions. Be polite, don&#8217;t repeat and you will get your answers </li>
<li> Avoid monkey patching core classes if your code is a library and will go with third party code. </li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://gnufied.org/2008/02/24/tips-for-budding-ruby-hacker/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

